Qt/mingw32 undefined reference errors... unable to link a .lib - c++

I am new to Qt and have one error I am unable to fix.
I have a bunch of windows (VS2005) static library file (.lib). And I am testing if they work well with Qt. So I took the most simple library that I have. (Called MessageBuffer).
So I added MessageBuffer.h to the main.cpp, and added the location of those file in the INCLUDEPATH of the .pro.
Until then everything seem fine, I can use the class and Qt IDE show all method and everything. So to me it look like it found the .h file.
Now I added the MessageBuffer.lib (VS2005/Debug build) in the .pro like this:
LIBS += E:/SharedLibrary/lib/MessageBufferd.lib
I have also tried the following:
win32:LIBS += E:/SharedLibrary/lib/MessageBufferd.lib
LIBS += -LE:/SharedLibrary/lib -lMessageBufferd
win32:LIBS += -LE:/SharedLibrary/lib -lMessageBufferd
Here is the content of my .pro file:
QT += opengl
TARGET = SilverEye
TEMPLATE = app
INCLUDEPATH += E:/SharedLibrary/MessageBuffer
SOURCES += main.cpp \
silvereye.cpp
HEADERS += silvereye.h
FORMS += silvereye.ui
OTHER_FILES +=
win32:LIBS += E:/SharedLibrary/lib/MessageBufferd.lib
They all give me the same errors: (and I get the same even if I don't include the .lib)
Running build steps for project SilverEye...
Configuration unchanged, skipping QMake step.
Starting: C:/Qt/2009.03/mingw/bin/mingw32-make.exe -w
mingw32-make: Entering directory `C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye'
C:/Qt/2009.03/mingw/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory `C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye'
g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -mthreads -Wl -Wl,-subsystem,windows -o debug\SilverEye.exe debug/main.o debug/silvereye.o debug/moc_silvereye.o -L"c:\Qt\2009.03\qt\lib" -lopengl32 -lglu32 -lgdi32 -luser32 -lmingw32 -lqtmaind E:/SharedLibrary/lib/MessageBufferd.lib -lQtOpenGLd4 -lQtGuid4 -lQtCored4
mingw32-make[1]: Leaving directory `C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye'
mingw32-make: Leaving directory `C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye'
debug/main.o: In function `Z5qMainiPPc':
C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye/main.cpp:12: undefined reference to `MessageBuffer::MessageBuffer()'
C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye/main.cpp:13: undefined reference to `MessageBuffer::Append(char*, int)'
C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye/main.cpp:17: undefined reference to `MessageBuffer::~MessageBuffer()'
C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye/main.cpp:17: undefined reference to `MessageBuffer::~MessageBuffer()'
collect2: ld returned 1 exit status
mingw32-make[1]: *** [debug\SilverEye.exe] Error 1
mingw32-make: *** [debug] Error 2
Exited with code 2.
Error while building project SilverEye
When executing build step 'Make'
Can anyone help please?

Based on the question Use libraries compiled with visual studio in an application compiled by g++ (mingw) and the MSDN forum post I can't mix VC & GCC it does not appear you can link a gcc application with visual c++ compiled libraries.
The solution would be to recompile everything with the same compiler.

The MinGW FAQ discusses this problem and offers a solution:
Create a definition file using reimp (for lib files) or pexports (for dll files).
Remove the underscore prefixes from the stdcall functions.
Use dlltool to convert the MSVC library into a MinGW library with the new definition.
That didn’t work. We finally removed the ordinals from the function names, which caused it to compile. But the program wouldn’t run because it couldn’t find the linked functions in the DLL. Finally, after consulting the MSDN documentation for definition files, we changed the build instructions:
Create a definition file using reimp.
For each stdcall function (formatted as _name#ordinal) add a line name = _name#ordinal, allowing MinGW to map its stdcall naming convention to that of MSVC.
Use dlltool to convert the MSVC library into a MinGW library with the new definition.
It worked! To compile the project you must simply:
Download and install the Qt/Windows package, which includes MinGW.
Download reimp and drop it into the MinGW/bin folder.
Download the development packages for the third-party libraries and point an environment variable to that location.
Build the project with the usual qmake/make commands.
Taken from:
http://blog.outofhanwell.com/2006/05/01/linking-msvc-libraries-with-mingw-projects/

I assume that you have used the MessageBuffer library in another application with problems. The error looks like it either cannot find the library or the MessageBuffer class is not being exported.
Have you tried putting -l on front of the library in the pro file?
win32:LIBS += -lE:/SharedLibrary/lib/MessageBufferd.lib
See my other answer. I added the other answer because I didn't want to make this answer any more messy than it already was.
Tried so far:
Not a typo, d is appended to the library
Using the lib extension is correct as seen in the output

Related

"Fatal Error C1083: Cannot open include file" in QtCreator

So I am working on a C++ Project in QtCreator which worked fine all the time. The project includes some external libraries for instance DDS solutions, but also it includes a library called "SimConnect". I worked with the library before and everything worked fine, but out of nowhere I am suddenly not able to compile my project anymore.
I am using Qt5.6.0 with a MSVC13 32bit compiler.
C:\dev\simNET\application\P3DHook.h:13: Fehler: C1083: Cannot open include file: 'SimConnect.h': No such file or directory
I know this is a compiler problem, so I rechecked the .pro file to see if it adds the include and lib paths to that library correctly. In my eyes this looks fine (yes the SimConnect.h file actually is in the specified path, I also tried giving it an absolute path but that did not solve the problem either):
win32: LIBS += -L$$PWD/'../../_programme/P3Dv3_SDK/Utilities/SimConnect SDK/lib/' -lSimConnect
INCLUDEPATH += $$PWD/'../../_programme/P3Dv3_SDK/Utilities/SimConnect SDK/Inc'
DEPENDPATH += $$PWD/'../../_programme/P3Dv3_SDK/Utilities/SimConnect SDK/Inc'
win32:!win32-g++: PRE_TARGETDEPS += $$PWD/'../../_programme/P3Dv3_SDK/Utilities/SimConnect SDK/lib/SimConnect.lib'
else:win32-g++: PRE_TARGETDEPS += $$PWD/'../../_programme/P3Dv3_SDK/Utilities/SimConnect SDK/lib/libSimConnect.a'
I also tried to manually re-run qmake and I deleted the whole build folder, cleaned the project and tried to rebuild it. Same error.
So next I took a look into the compile console, and spotted the part where the error occurs:
DNDEBUG -I..\..\..\simNET\plugins\CreatorPlugin -I. -I..\..\..\simNET\application -I..\..\..\_programme\FastRTPS1.5\include -I..\..\..\..\Qt\5.6\msvc2013\include -I..\..\..\..\Qt\5.6\msvc2013\include\QtWidgets -I..\..\..\..\Qt\5.6\msvc2013\include\QtGui -I..\..\..\..\Qt\5.6\msvc2013\include\QtANGLE -I..\..\..\..\Qt\5.6\msvc2013\include\QtCore -Irelease -I..\..\..\..\Qt\5.6\msvc2013\mkspecs\win32-msvc2013 -Forelease\ #C:\Users\crolk\AppData\Local\Temp\CreatorPluginPublisher.obj.2256.47.jom
CreatorPluginPublisher.cpp
C:\Qt\5.6\msvc2013\bin\moc.exe -DUNICODE -DWIN32 -DQT_NO_DEBUG -DQT_PLUGIN -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DNDEBUG -D_MSC_VER=1800 -D_WIN32 -IC:/Qt/5.6/msvc2013/mkspecs/win32-msvc2013 -IC:/dev/simNET/plugins/CreatorPlugin -IC:/dev/simNET/application -IC:/dev/_programme/FastRTPS1.5/include -IC:/Qt/5.6/msvc2013/include -IC:/Qt/5.6/msvc2013/include/QtWidgets -IC:/Qt/5.6/msvc2013/include/QtGui -IC:/Qt/5.6/msvc2013/include/QtANGLE -IC:/Qt/5.6/msvc2013/include/QtCore -I. ..\..\..\simNET\plugins\CreatorPlugin\CreatorPluginPublisher.h -o release\moc_CreatorPluginPublisher.cpp
..\..\..\simNET\application\P3DHook.h(13) : fatal error C1083: Cannot open include file: 'SimConnect.h': No such file or directory
It looks like it adds other include paths (such as the one for my DDS library) but it simply won't add the SimConnect one, no matter how often I rerun qmake.
Has anyone an idea what the problem could be?
EDIT:
I noticed that the build actually does not even fail. It successfully builds my project and creates the executable. For some reason QtCreator will still think the build failed and will show me that error. Running my executable manually without QtCreator works fine...
Solution was the following:
My application consists of 3 subprojects: the main gui application + 2 plugins, which include some main gui's source code. Now the error occured in the source code of the main application, but exactly that source code happened to be included by one of the plugins. In that plugins .pro file, there was no path given to the external SimConnect library though, which was the problem.
So... If you include source code from another of your projects, make sure to also include all the dependencies needed in that subproject. Because otherwise it might lead to a misunderstandable error message.
Maybe your include paths have some unrecognizable symbols.
What is your absolute paths?

g++ error linking functions from static library

I have a Qt5 C++ program and I'm trying to link to a static library. The static library is senselock/libsenseEIV.a (relative to the main.cpp file). When I compile I see the output below:
^
g++ -Wl,-rpath,/opt/Qt/5.7/gcc_64/lib -o test1 main.o -Lsenselock/libsenseEIV.a -L/opt/Qt/5.7/gcc_64/lib -lQt5Core -lpthread
main.o: In function `test1()':
/test1/main.cpp:31: undefined reference to `S4Enum'
/test1/main.cpp:58: undefined reference to `S4Enum'
/test1/main.cpp:71: undefined reference to `S4Open'
/test1/main.cpp:83: undefined reference to `S4Control'
/test1/main.cpp:102: undefined reference to `S4Close'
collect2: error: ld returned 1 exit status
make: *** [Makefile:216: test1] Error 1
11:55:27: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project test1 (kit: Desktop Qt 5.7.1 GCC 64bit)
When executing step "Make"
and in my .pro file I have
LIBS += -Lsenselock/libsenseEIV.a
in case that matters. Can someone explain how to fix this error? The undefined reference errors all relate to functions located in that libsenseEIV.a library.
I don't understand if the compiler can't find the .a file or there's some other error.
UPDATE: I tried this syntax
LIBS += -Lsenselock -lsenseEIV
but it generates error
/usr/bin/ld: cannot find -lsenseEIV
Am I using the wrong library name? If so how would I find the name? (assuming this is compiled into the .a file)
This part of your command line is wrong:
-Lsenselock/libsenseEIV.a
Should be:
senselock/libsenseEIV.a
(The -Lfoo/bar.a tells linker to search directory foo/bar.a/ for libraries, which is not at all what you want.)
No -l prefix?
You can specify linking with libsenseEIV.a in the following (mostly equivalent) ways:
senselock/libsenseEIV.a
-Lsenselock -lsenseEIV
-Lsenselock -l:libsenseEIV.a
You're using the linker flags wrong, you should specify the library path after -L and the library name after -l. In other words you need to have both in the assignment to LIBS variable in your .pro file.
After all it turns out that you can use LIBS += -L$$PWD/senselock/ -lsenseEIV for your case

Getting exe when building dll using libtool

I'm trying to compile .dll of this project on windows using MinGW. Everything goes ok except one point: output is the .exe file - not .dll.
Here is the notification, that libtool gave me:
*** Since this library must not contain undefined symbols,
*** because either the platform does not support them or
*** it was explicitly requested with -no-undefined,
*** libtool will only create a static version of it.
Well, I've not found anywhere in project option -no-undefined and that's why I wonder what symbols(and how??) should be fixed to get needed .dll?
Maybe there are some issues with linker? I dont know where to change($LIBRARY_PATH = /c/mingw already) it and that's why during make this warning pops up:
*** Warning: linker path does not have real file for library -lz.
*** I have the capability to make that library automatically link in when
*** you link to this library. But I can only do this if you have a
*** shared version of the library, which you do not appear to have
*** because I did check the linker path looking for a file starting
*** with libz and none of the candidates passed a file format test
*** using a file magic. Last file checked: c:/Strawberry/c/bin/../lib/gcc/x86_64
-w64-mingw32/4.8.3/../../..//libz.a
*** The inter-library dependencies that have been dropped here will be
*** automatically added whenever a program is linked with this library
*** or is declared to -dlopen it.
Looks like your program need zlib, but c:/Strawberry/c/bin/../lib/gcc/x86_64
-w64-mingw32/4.8.3/../../..//libz.a is not valid (different target architecture - x86 vs x86_64?). You need to get a real zlib, compile for the same architecture and point your program to it.
you can use a .cmd file with following content
::source code file path
set src_path=source.cpp
::output file path
set bin_path=yourlib.dll
::library you need
set libs=-lmingw32 -lkernel32 -lshell32 -luser32 -lwsock32 -lws2_32 -lole32 -loleaut32
::windows subsystem
set sub_sys=-mwindows
::cpp version used by g++
set cpp_ver=-std=c++1y
::jump to g++ path
cd "%your_mingw32_folder_path%\bin\"
::run g++ with args
g++.exe "%src_path%" -o "%bin_path%" -Wall -W -w -static %sub_sys% %libs% %cpp_ver%
pause

Qt Creator can't find library

I'm using ubuntu(32 bit) on virtualbox. I am trying to add a the rtlsdr library but Qt creator can't find the rtlsdr library.
The library is located in /usr/local/lib/librtlsdr.so and I have tried adding the following to my .pro file
LIBS += -L/usr/local/lib -lrtlsdr
INCLUDEPATH +=usr/local/lib
But I get the following error:
/home/luis/qt-bbb/gcc-linaro-arm-linux-gnueabihf-4.7-2013.04-20130415_linux/bin/arm-linux-gnueabihf-g++ -Wl,-rpath-link=/home/luis/qt-bbb/tslib/lib -Wl,-rpath,/opt/qt-arm/lib -o qt-test main.o mainwindow.o moc_mainwindow.o -L/home/luis/qt-bbb/tslib/lib -L/opt/qt-arm/lib -L/home/luis/qt-arm/rtl-sdr/build/src/ -lrtlsdr -lQtGui -L/opt/qt-arm/lib -L/home/luis/qt-bbb/tslib/lib -lQtNetwork -lQtCore -lpthread
/home/luis/qt-bbb/gcc-linaro-arm-linux-gnueabihf-4.7-2013.04-20130415_linux/bin/../lib/gcc/arm-linux-gnueabihf/4.7.3/../../../../arm-linux-gnueabihf/bin/ld: *skipping incompatible /home/luis/qt-arm/rtl-sdr/build/src//librtlsdr.so when searching for -lrtlsdr*
/home/luis/qt-bbb/gcc-linaro-arm-linux-gnueabihf-4.7-2013.04-20130415_linux/bin/../lib/gcc/arm-linux-gnueabihf/4.7.3/../../../../arm-linux-gnueabihf/bin/ld: *skipping incompatible /home/luis/qt-arm/rtl-sdr/build/src//librtlsdr.a when searching for -lrtlsdr*
Makefile:105: recipe for target 'qt-test' failed
/home/luis/qt-bbb/gcc-linaro-arm-linux-gnueabihf-4.7-2013.04-20130415_linux/bin/../lib/gcc/arm-linux-gnueabihf/4.7.3/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lrtlsdr
collect2: error: ld returned 1 exit status
make: *** [qt-test] Error 1
I'm using ubuntu 32bit to cross compile for my beaglebone running debian. Deploying on my beaglebone works ok and I verified the libraries are 32 bit so I don't know why it can't find them or why it's giving me “skipping incompatible” message. I also tried adding them by right clicking on the .pro->add library but still can't find them.
skipping incompatible tells you the library cannot be linked. Probably compiled with the wrong compiler?
– Sebastian Lange
Ok i figured out my problem, the library was built for x86 and not arm. Thanks for the hints! – luismbedder

C++ NetBeans Linking External Sources and .so Files

I am writing a C++ program in Linux with NetBeans. I am having difficulty setting it up to use external sources/shared objects. I have the .so files from the compiled external package and the source files that go with it.
So far I have:
specified for the project to include all the source and header file directories (under Project properties->Build->C++ Compiler)
specified the .so files that correspond to the external source code (under Project properties->Build-Linker)
When I try to declare an object defined in the external sources NetBeans does not give me any syntax errors and even auto-completes the object name for me. However, when I build the program I get an error saying "undefined reference to" that object.
Am I doing something horribly wrong?
EDIT:
In response to quamrana's question, this is one of the output lines in the console when it attempts to build.
g++ -o dist/Debug/GNU-Linux-x86/JAUSTester build/Debug/GNU-Linux-x86/MainScreen.o build/Debug/GNU-Linux-x86/main.o build/Debug/GNU-Linux-x86/moc_MainScreen.o -L/usr/lib -Wl,-rpath /usr/local/lib/active /usr/local/lib/active/libcxutils.so -Wl,-rpath /usr/local/lib/active/libjauscore.so -Wl,-rpath /usr/local/lib/active/libjausextras.so -Wl,-rpath /usr/local/lib/active/libjausmobility.so -Wl,-rpath /usr/local/lib/active/libtinyxml.so -lQtGui -lQtCore -lpthread
The .so files I want to include are the ones specified there in /usr/local/lib/active/.