Qt Serial Port + Database [duplicate] - database

I added a class IcecastServer to my QT-project, added the header-file to the pro file and added some code. Everytime I compile it the following errors occur:
release/icecastserver.o:icecastserver.cpp:(.text+0x39): undefined reference to _imp___ZN10QTcpServerC1EP7QObject'
release/icecastserver.o:icecastserver.cpp:(.text+0x50): undefined reference toimpZN12QHostAddressC1ENS_14SpecialAddressE'
release/icecastserver.o:icecastserver.cpp:(.text+0x68): undefined reference to _imp___ZN10QTcpServer6listenERK12QHostAddresst'
release/icecastserver.o:icecastserver.cpp:(.text+0x73): undefined reference to_imp_ZN12QHostAddressD1Ev'
release/icecastserver.o:icecastserver.cpp:(.text+0x9d): undefined reference to _imp___ZNK10QTcpServer11errorStringEv'
release/icecastserver.o:icecastserver.cpp:(.text+0x3d4): undefined reference toimpZN12QHostAddressD1Ev'
release/icecastserver.o:icecastserver.cpp:(.text+0x4bd): undefined reference to _imp___ZN10QTcpServerC1EP7QObject'
release/icecastserver.o:icecastserver.cpp:(.text+0x4d4): undefined reference to_imp_ZN12QHostAddressC1ENS_14SpecialAddressE'
release/icecastserver.o:icecastserver.cpp:(.text+0x4ec): undefined reference to _imp___ZN10QTcpServer6listenERK12QHostAddresst'
release/icecastserver.o:icecastserver.cpp:(.text+0x4f7): undefined reference toimpZN12QHostAddressD1Ev'
release/icecastserver.o:icecastserver.cpp:(.text+0x521): undefined reference to _imp___ZNK10QTcpServer11errorStringEv'
release/icecastserver.o:icecastserver.cpp:(.text+0x858): undefined reference to_imp_ZN12QHostAddressD1Ev'
What am I doing wrong?
This is the header-file:
#ifndef ICECASTSERVER_H
#define ICECASTSERVER_H
#include <QObject>
QT_BEGIN_NAMESPACE
class QTcpServer;
QT_END_NAMESPACE
class IcecastServer : public QObject
{
Q_OBJECT
public:
explicit IcecastServer(QObject *parent = 0);
signals:
public slots:
private:
QTcpServer *tcpServer;
};
#endif // ICECASTSERVER_H
This is the source-file:
#include "icecastserver.h"
#include "QDebug"
#include <QtNetwork/QTcpServer>
#include <QtGui>
IcecastServer::IcecastServer(QObject *parent) :
QObject(parent)
{
tcpServer = new QTcpServer(this);
//tcpServer->listen(QHostAddress::Any,8000);
if (!tcpServer->listen()){
QMessageBox::critical(NULL, tr("Fortune Server"), tr("Unable to start the server: %1.").arg(tcpServer->errorString()));
return;
}
}

First you need to #include <QHostAddress> somewhere assuming that commented out line is what caused the problem.
You may also to check some project settings to see if you have all the correct inputs.
Edit: More detail
QtNetwork requires QT network lib inputs. Assuming once again that you are using QtCreator as not much info was provided then this means in your .pro file you need to have a line like so:
QT += network
Then make sure you include the right headers for the objects you are using before using them. If you still get undefined references, linking errors, etc try a QMake and rebuild. If it still persists you probably have errors in your code in addition to QT usage errors and should validate that your methods and objects being used were properly declared.
Final Edit: Glad that worked ...
When you create a new project in QTCreator there is a step in the wizard where you can check off the various QT libraries you want to include which will add these .pro lines and inputs for you. This is QT version of inputting additional lib files and by default they will be statically linked I believe. If you want to dynamically link with shared objects or dll then there is some extra configuration steps.

Are you running the moc tool over your header file? Are you subsequently compiling the output from the moc tool?

Related

Undefined reference ld error when using Windows <bluetoothapis.h>

I am new to programming and want to work with the Windows BluetoothApi.h library in C. I've written smaller programs that reference header files I've created, but none of the APIs given by windows.
I am attempting to return information from a local bluetooth speaker to a terminal session on my PC. I've been referencing the BluetoothFindFirstRadio and BLUETOOTH_FIND_RADIO_PARAM documentation, as well as some posts on Stack to see some viable examples. I believe I'm close to being able to compile but I keep getting an error about an undefined reference to the functions I'm calling that I do believe are in the BluetoothAPI.h header file.
From what I've seen, again on Stack, it seems that it's possible that "there is not enough space left at \user\tmp"?
or
Looking at the documentation for ld, it may be possible I need to try to compile using a different command altogther?
PS C:\scripts\C_Lang\Bluetooth> gcc bluetest.c -o test
C:\Users\Ryan\AppData\Local\Temp\cce0FxKH.o:bluetest.c:(.text+0x2b): undefined reference to `BluetoothFindFirstRadio#8'
C:\Users\Ryan\AppData\Local\Temp\cce0FxKH.o:bluetest.c:(.text+0x48): undefined reference to `BluetoothFindRadioClose#4'
collect2.exe: error: ld returned 1 exit status
Code is below:
#include <stdio.h>
#include <string.h>
#include <Windows.h> //not sure if needed
#include <Ws2bth.h> //not sure if needed
#include <bthsdpdef.h>
#include bluetoothapis.h>
//#include <bluetoothleapis.h>
#pragma comment(lib, "Bthprops.lib");
int main(void)
{
BLUETOOTH_FIND_RADIO_PARAMS btfrp; // structure
btfrp.dwSize = sizeof(btfrp); // creating space in memory for parameters?
HANDLE hRadio; // not sure what a handle is, something similar to a pointer?
HBLUETOOTH_RADIO_FIND hFind = BluetoothFindFirstRadio(&btfrp, &hRadio);
// BluetoothGetDeviceInfo(hRadio, &pbtdi);
printf("Bluetooth test!");
BluetoothFindRadioClose(hFind);
return 0;
}
It seems that my issue was not 100% my code, but about how I was attempting to compile my code. After looking further into the documentation I read the line, "Link only to Bthproprs.lib, and avoid linking to Ilprops.lib." So, I don't fully understand why I would need to link, when I have a #pragma comment(lib, "Bthprops.lib"); but that is most likely due to my own ignorance. I did notice the answer on this post which helped clear up my ignorance of HOW to link the Bthproprs.lib library. So, my code didn't change, but my compile did, gcc bluetest.c -o test -lbthprops.
Now, to return something actually useful.

Include Files in Segger Embedded Studio

I am just starting out with Segger Embedded Studio. Right now trying to call a function of a included header file. The file seems to be included since it shows up in the dependencies. For now I simply included the header and C-File in the project directory.
The included Header- and C-Files are simply:
//##### Header-File ######
#ifndef TEST_H_
#define TEST_H_
void printText(void);
#endif //TEST_H_
and:
//###### C-File #########
#include <test.h>
#include <stdio.h>
#include <stdlib.h>
void printText(void)
{
printf("Hello");
}
But when I try to call the printText Function in my main I get the error:
"Undefined Symbol: printText".
Why is the function not recognized?
"Undefined symbol" is a linker error. You are not linking the object code containing the definition of printText().
It is not an issue with the header file; including a header file does not cause the associated code to be linked - that is just the declaration so the compiler knows what the interface looks like. It is the linker than combines the separately compiled object code to form a program. You have not told the linker to use the object code containing printText(), and you have not told your IDE project to compile it to generate that object code.
The project tree clearly shows that only main.c is included in your project; you need to add the C file containing printText() too.
The concept of separate compilation and linking is what you need to grasp here.
Thank you Clifford for your answer. You are right I had some miss-conceptions about which files will be linked while building the project. In the special case of segger embedded studio there are, as I know by now, two ways to reference extern files.
Adding the files to the sources files folder is straight forward, but must be done separately for each project you want to use the respective files.
For frequently used files it is beneficial to create a dedicated library solution. Such a library can then be imported to any solution by choosing "add existing project". This will add all files of the library to your current solution (and show them in the project-tree). Now click the tab project -> dependencies. Your library should show up here. By acitivating the check box the linker will compile the referenced project upon builing your solution, allowing for the usage of your library functions.
Adding and linking library-project to a current solution

VSCode not importing functions from other files in C language

I am going through a C course and got to the point where we #include "myfile.h"
Good news : VSCode finds "myfile.h" and can pull variables from it, such as int myvar=10;
Bad News : VSCode does not seem to identify function definition in the "myfile.c", so extern in myfunction() is seen but not defined.
This results in the following
int i = myfunction();
Compilation Error: undefined reference to 'myfunction';
How can I get VSCode to recognize and use "myfile.c"?
Answer found in ::
undefined reference error in VScode
Basically, I had to compile all my files at once. This required me going into the tasks.json file and modifying it from ${file} to ${workspaceFolder}\*.c
This is better explained in ::
https://code.visualstudio.com/docs/cpp/config-linux

Using D2XX of FTDI with C

I'm trying to use ftd2xx.lib with lcc-win32 compiler but the linker fails. I used CDM v2.12.14 but also previous FTDI driverd give the same errors. I tried to compile on Windows XP and Windows7; same results.
If I link the static library I will get these kind of errors:
File ftd2xx.lib contains unknown section .text$mn.
.text section assumed
File ftd2xx.lib contains unknown section .data$r.
.data section assumed
.data$r: undefined reference to '??_7type_info##6B#'
.text$mn: undefined reference to '__imp__SetupDiGetClassDevsA#16'
.text$mn: undefined reference to '__imp__SetupDiEnumDeviceInterfaces#20'
If I link the dynamic ftd2xx.lib (with ftd2xx.dll placed in the same dir of .exe file) I will get this:
Error e:\c\ftdi_cnt\main.c 11 undefined reference to __imp__FT_Open
(Note that main.c used for this test contains just a single FT_Open() call).
Is there anyone that was able to run ftd2xx.lib with lcc-win32 compiler or that is currently using lcc-win32 and can make a quick test? Here is the latest FTDI driver. Thank you.
Finally it works!
Here is what I did:
1) Ian suggested I should use "Utils --> Import foreign library"; as a matter of fact this is the same of running from the command line:
pedump /EXP ftd2xx.lib >ftd2xx.exp
buildlib ftd2xx.exp ftd2xxy.lib
I've already tried that using for ftd2xx.lib the dynamic .lib but without success.
2) I ran pedump again, this time using the static .lib and I got a warning message about unknown dll name, so I edited the first line of ftd2xx.exp adding that name (ftd2xx.dll).
3) I defined FTD2XX_STATIC before including ftd2xx.h
4) I linked the new library ftd2xxy.lib
Note that you can't use "Utils --> Import foreign library" because you need to modify ftd2xx.exp before re-building the library.
I hope this could be helpful for others.

Typedefs included, but not functions

I'm writing some code that uses a C library provided by MATLAB (to extract data from *.mat files). In my IDE (Code::Blocks), I've included the folder containing the necessary "mat.h", which is on a network drive. My code recognises types defined in mat.h when I do this, but whenever I call functions from the file I get an "undefined reference" error. This is the same case for the example code MathWorks provides. What sort of problem usually causes this?
#include "mat.h"
int main (void) {
MATFile *pmat; // Compiles only when compiler is told to search in mat.h directory
pmat = matOpen("example_filename", "r"); // Never compiles
return 0;
}
Thanks!
Cameron
"undefined reference" is normally a linker error. It's not a problem of a header file. You need to tell the linker to link MATLAB's library (or a dedicated object) to your program.
No idea how this is done in Code::Blocks though. In the Code:Blocks documentation it is described here.
Have you checked the contents of mat.h? Does it declare matOpen()? Also, does the error occur when compiling or linking? If it's during the link phase, you probably need to reference the library that contains the implementation of matOpen() (a .lib in Windows, or .a in Unix). The .h file only declares the function.

Resources