undefined reference to library function in Qtcreator - qt

I have to use a library under Linux. It's a .so compiled with gcc.
I added on my .pro :
INCLUDEPATH += mypath/include
LIBS += -L/mypath/lib/ -lmyLib
but, i get the following error :
undefined reference to `init_glove(char*, char*)'
I don't understand why i get this message. My library is linked and the header file is founded. I read that it could be a problem between the compiler used for my library, and the one used by Qt, but both seem to be gcc, so...
Any ideas ?

If the problem was the g++/gcc issue, then in your QT project simply do this
extern "C"
{
#include "mylib.h"
}

Actually, Qt was compiling with g++, so I had to change de compiler of my library : switching it from gcc to g++.

Related

Linker error with CMake

I'm using JetBrains CLion for pure C (C ANSI) development, I know it's target is C++, but my company works only with C and
CLion uses only CMake as build system.
My system is a Debian Jessie system and sqlite3 and libsqlite3-dev are installed.
I'm trying to build a simple sqlite3 project like this:
#include <sqlite3.h>
#include <stdio.h>
int main() {
sqlite3 *sqlConnection;
int ret;
ret = sqlite3_open("database/path.db, &sqlConnection);
if (ret) {
printf("Ups ... can't open %d", ret);
}
do_some_queries(sqlConnection);
return 0;
}
The automatic generated CMakeLists.txt is the follwing.
cmake_minimum_required(VERSION 3.3)
project(Project)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp )
add_executable(Project ${SOURCE_FILES})
When build, either through Clion, either through command line, I get linker errors:
...
undefined reference to `sqlite3_prepare_v2'
undefined reference to `sqlite3_column_int'
undefined reference to `sqlite3_open'
...
I know I must point out to CMake where sqlite3 is, but I can't find a way of doing this.
"find_package" and "find_library" may do it, but I can't find how.
I've also found this Cmake file, but could not used successfully.
So, how do I integrate sqlite3 with Cmake ?
You need to add the path to sqlite header file to your include path. Then link the sqlite library using target_link_libraries:
https://cmake.org/cmake/help/v3.3/command/target_link_libraries.html
I've founded a workaround, but it's not the correct way of doing.
Since on Debian the "gcc main.c -lsqlite3" works (with libsqlite3-dev installed), passing the -lsqlite3 flag to the linker do the trick.
so I've changed
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
to
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lsqlite3")
And it worked.

Integrating QNX Qt libraries in a Qt project

Would anyone know how to include/use the QNX Qt libraries such as QtQnxCar2, QPPS, QPlayer in a Qt project? I think this involves including a package in Qt or may be linking the library in the .pro file, just can't figure out what it is though.
QNX has lot of documentation about the libraries, but nothing about how to actually include and use them in a Qt project.
I am specifically looking for how to use the Qpps namespace in my project. For example, I've linked the qpps library as below in my .pro file:
LIBS += -L/home/me/qnx660/target/qnx6/armle-v7/qtcar/lib/ -lqpps
INCLUDEPATH += /home/me/qnx660/target/qnx6/armle-v7/qtcar
DEPENDPATH += /home/me/qnx660/target/qnx6/armle-v7/qtcar
Here's the code in mainwindow.cpp,
#include <QDebug>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
using namespace QPps;
Here's the build error:
mainwindow.cpp:5:17: error: 'QPps' is not a namespace-name
mainwindow.cpp:5:21: error: expected namespace-name before ';' token
cc: /home/me/qnx660/host/linux/x86/usr/lib/gcc/arm-unknown-nto-qnx6.6.0eabi/4.7.3/cc1plus error 1
make: *** [mainwindow.o] Error 1
Toolchain:
Qt Creator 3.3.2
Qt 5.2.0
QNX Car 2.1
QNX SDP 6.6
Ubuntu
14.04
EDIT
The question is changed completely! Instead of linking problem, described earlier, now a compiler error is described.
You have to include the header-file first, where the namespace is declared.
e.g:
#include <qpps/changeset.h>
Also, pay attention, that the directory of qpps-include files (headers, .h-files) are defined in the line:
INCLUDEPATH += <Path to your qpps includes >
ORIGINAL ANSWER
If QNX libraries apply to the same rules as other UNIX libraries, you could use them by including this line in your Qt project file (*.pro)
LIBS += -L/Path/to/mylib/ -lmylib
Actually QNX doesn't have to provide any info, how to use their libs in Qt.
The appropriate manual is the QMAKE-manual.

dbus - undefined reference to

This topic is related with following link . Temporarily I solved problem with headers: I just modified all include paths. When I try to call some dbus function, for example dbus_error_init(&err); I get a linker error: undefined reference to 'dbus_error_init'. libdbus-1.so is placed in /usr/lib/x86_64-linux-gnu so I added to pro file (I use Qt Creator) following line:
LIBS += -L/usr/lib/x86_64-linux-gnu -llibdbus-1
but I get follwoing info: cannot find -llibdbus-1 How to fix that ?
Usually it's just -ldbus-1 or -ldbus. You can also try adding CONFIG += dbus in your .pro file to enable the Qt DBUS classes, which if I remember rightly detects the DBUS library as well, as long as Qt was built with support for it.

Conflict between qDebug and SDL

I work on Mac OS 10.8.5 and Qt 5.1.1 with among other things, qdebug class which work well with some projects and doesn't compile with some others. I've already tried to re-install Qt which didn't change a thing.
It is certainly due to the use of SDL library (?!) but it used to work fine. Here's a part of the compilation output :
In file included from ../../Stage/Joker/tests/SDLTest/main.cpp:7:
In file included from /Applications/QT/5.1.1/clang_64/include/QtCore/QDebug:1:
In file included from /Applications/QT/5.1.1/clang_64/include/QtCore/qdebug.h:46:
In file included from /Library/Frameworks/QtCore.framework/Headers/qhash.h:46:
/Library/Frameworks/QtCore.framework/Headers/qchar.h:76:5: error: unknown type name 'QT_ASCII_CAST_WARN_CONSTRUCTOR'
QT_ASCII_CAST_WARN_CONSTRUCTOR QChar(char c);
^
If needed, I can provide more output or unworking Qt classes.
I build my code with Qt => Build => Run
Unfortunatly, I'm unable to reproduce the code on an other computer.
Here's the pro file (which work well with all my co-workers, except me) :
TEMPLATE = app
SOURCES += main.cpp
OBJECTIVE_HEADERS += ../../libs/PhGraphic/SDLMain.h
OBJECTIVE_SOURCES += ../../libs/PhGraphic/SDLMain.m
LIBS += -framework Cocoa
QMAKE_CXXFLAGS += -F/Library/Frameworks
QMAKE_OBJECTIVE_CFLAGS += -F/Library/Frameworks
LIBS += -F/Library/Frameworks
LIBS += -framework SDL -framework SDL_image -framework SDL_ttf
I'd once had this problem after installing a software: it created a QtCore.framework in the /Library/Framework folder that overrided the QtCore of your current Qt kit (due to QMAKE_CXXFLAGS += -F/Library/Frameworks).
It's qDebug, and there are two overloads:
a stream-returning function
qDebug() << "foo";
a printf-style function
qDebug("foo");
You need to provide a minimum reproducible test case for us to help you.
How are you building your code? Are you using qmake to produce the Makefile? We'd need to see the .pro file at the minimum, and please minimize it by removing useless boilerplate comments etc.

Accessing NI-VISA from Qt C++ 4.7

I am developing a Windows (7) application using Qt (4.7.0) to call some methods in a DLL (NI visa32.dll) to communicate with instruments through the GPIB port. The manufacturer's header file is also available (visa.h).
In the project file, I tried adding the path and library reference to the original places where the files are located at as:
INCLUDEPATH += "C:/Program Files/National Instruments/Shared/CVI/Include"
LIBS += "C:/Windows/System32/visa32.dll"
but, I get the compilation error:
collect2: ld returned 1 exit status
Following the instructions in Importing a DLL into Qt, I created a "visa.a" from the "visa32.dll", and copied them to a subfolder "visa/lib", and added the path and library to the project file:
INCLUDEPATH += visa/include
LIBS += -Lvisa/lib
LIBS += -lvisa.a
I tried also with -lvisa or -lvisa.dll, but in all the cases I get also another compilation error saying that the -lvisa, -lvisa.a or -lvisa.dll is not found. I edited the original header file "visa.h", and prefixed with Q_DECL_IMPORT every object in the file, and also made sure that the extern "C" statement be present.
I include the reference to the header file in the application as:
#include "visa.h"
and note that the compiler does recognize the referenced objects belonging to the visa.h file.
Any help to solve this compilation error will be greatly appreciated.
I also tried with Visual C++ (2010) following the instructions of DLL References in Visual C++. In this case, I do not get any compilation error, but linking errors. For example:
AgiE364X.obj: error LNK2019: unresolved external symbol
"extern "C" long __stdcall viClose(unsigned long)"
being viClose a called method in NI-VISA.
I would prefer to use Qt C++ instead of Visual C++, though.
Thanks in advance.
I'm not sure since I'm new to Qt myself but here are some guesses: I noticed you have a space in INCLUDEPATH string. Also, LIBS += -Lvisa/lib has a capitol L (is this correct?). I'm not saying this is your problem but perhaps worth a try. The last thing that comes to mind is that if you're using Qt Creator only partially type #include "visa.h" and see if auto complete can complete it for you. If it can, than prob the rest is OK. Trigger auto-complete with Ctrl+Space. I've noticed that when I mess up the paths auto-complete is a good gauge for this sort of thing.
There are two types of library of VISA provided by NI: the one is compiled by borland c++, the other is compiled by microsoft vc++.
As for Qt, you can use Qt which should be compiled by MSVC, to link the visa32.lib which is compiled by MSVC too. And it MUST link .lib instead of .dll, The MSVC library of VISA is in "C:\Program Files\IVI Foundation\VISA\WinNT\lib\msc" by default.
The include directory is C:\Program Files\IVI Foundation\VISA\WinNT\include by default.
In fact, the above information can be known from VISA example after you install the device driver.

Resources