Relink objects from most basic Qt application - Linker error - c++

Try first if the simple demo app (the console project template) compiles with qmake command line:
SET PATH=%PATH%;C:\Qt\Qt5.9.1\5.9.1\mingw53_32\bin;C:\Qt\Qt5.9.1\Tools\mingw530_32\bin
C:\Qt\Qt5.9.1\5.9.1\mingw53_32\bin\qmake.exe -makefile C:\Software\test.pro -spec win32-g++
mingw32-make.exe
pause
Now trying to relink the application:
C:\Qt\Qt5.9.1\Tools\mingw530_32\bin\ld -o test release\main.o -LC:\Qt\Qt5.9.1\5.9.1\mingw53_32\bin -lQt5Core --verbose > ld_dump.txt
pause
Errors
main.o undefined reference to _Unwind_Resume
main.o undefined reference to __gxx_personality_v0
For comparison a slightly more complex app with QtWidgets prodcues even more errors (of course I added the missing Qt libs in the linker bat file)
moc_widget.o undefined reference to strcmp
moc_widget.o QObject undefined reference to vtable for __xyyabiv1:: __class_type_info
...
Googling error 1+2 they say it may be caused by different compilers. But I am using the same tool.
The pro file looks like
QT += core
QT -= gui
CONFIG += c++11
TARGET = test
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
Do I maybe need to pass some kind of c++11 flag to the linker ld?

I think ld needs a lot more information (e.g. it doesn't know anything about stdc++). Why don't you link using g++, instead? A line like this should work:
C:\Qt\Qt5.9.1\Tools\mingw530_32\bin\g++.exe \
-Wl,-rpath,C:\Qt\Qt5.9.1\5.9.1\mingw53_32\bin \
-o test release\main.o -LC:\Qt\Qt5.9.1\5.9.1\mingw53_32\bin -lQt5Core

Related

Using Crypto++ static library in a QT project

I have built cryptopp statically on my system it passes all tests too. These are the warning though I get during tests
WARNING: CRYPTOPP_NO_UNALIGNED_DATA_ACCESS is not defined in config.h.
WARNING: CRYPTOPP_INIT_PRIORITY is not defined in config.h.
WARNING: CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 is defined in config.h.
WARNING: You should make these changes in config.h, and not CXXFLAGS.
WARNING: You can 'mv config.recommend config.h', but it breaks versioning.
WARNING: See http://cryptopp.com/wiki/config.h for more details.
I now link this in my QT project file as
TEMPLATE = app
LIBS += -L/usr/lib/libcryptopp.a
#LIBS += -lcryptopp
CONFIG += console c++11
CONFIG += staticlib
SOURCES += main.cpp \
hashdata.cpp
HEADERS += \
hashdata.hpp
But when I compile this I get all undefined errors.
hashdata.o: In function `hashdata::hashfunction(std::string)':
hashdata.cpp:(.text+0x1fb): undefined reference to `CryptoPP::Algorithm::Algorithm(bool)'
hashdata.cpp:(.text+0x270): undefined reference to `CryptoPP::SHA512::InitState(unsigned long long*)'
hashdata.cpp:(.text+0x29a): undefined reference to `CryptoPP::Algorithm::Algorithm(bool)'
hashdata.cpp:(.text+0x2a1): undefined reference to `vtable for CryptoPP::StringSinkTemplate<std::string>'
hashdata.cpp:(.text+0x30b): undefined reference to `CryptoPP::Filter::Filter(CryptoPP::BufferedTransformation*)'
hashdata.cpp:(.text+0x312): undefined reference to `vtable for CryptoPP::Grouper'
hashdata.cpp:(.text+0x35e): undefined reference to `CryptoPP::Filter::Detach(CryptoPP::BufferedTransformation*)'
hashdata.cpp:(.text+0x375): undefined reference to `CryptoPP::Filter::Filter(CryptoPP::BufferedTransformation*)'
hashdata.cpp:(.text+0x37c): undefined reference to `vtable for CryptoPP::BaseN_Encoder'
hashdata.cpp:(.text+0x3d3): undefined reference to `CryptoPP::Filter::Detach(CryptoPP::BufferedTransformation*)'
hashdata.cpp:(.text+0x3e5): undefined reference to `CryptoPP::ProxyFilter::ProxyFilter(CryptoPP::BufferedTransformation*, unsigned long, unsigned long, CryptoPP::BufferedTransformation*)'
hashdata.cpp:(.text+0x3ec): undefined reference to `vtable for CryptoPP::HexEncoder'
hashdata.cpp:(.text+0x452): undefined reference to `vtable for CryptoPP::AlgorithmParametersTemplate<int>'
hashdata.cpp:(.text+0x4af): undefined reference to `vtable for CryptoPP::AlgorithmParametersTemplate<CryptoPP::ConstByteArrayParameter>'
...
I have seen a similar problem previously when I searched in google, but the solution isn't clear. Could it be because of C++11 flags ?
I have built cryptopp statically on my system it passes all tests too.
These are the warning though I get during tests
WARNING: CRYPTOPP_NO_UNALIGNED_DATA_ACCESS is not defined in config.h.
WARNING: CRYPTOPP_INIT_PRIORITY is not defined in config.h. WARNING:
CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 is defined in config.h.
WARNING: You should make these changes in config.h, and not CXXFLAGS.
WARNING: You can 'mv config.recommend config.h', but it breaks
versioning. WARNING: See http://cryptopp.com/wiki/config.h for more
details.
I can comment on this warning. You should perform the steps it says:
mv config.recommend config.h
config.recommend puts the library is a better configuration by completely avoiding known undefined behavior that could not be removed without breaking versioning. Since you don't appear to have versioning issues (like say, Fedora or Debian), then you can perform the move.
I now link this in my QT project file as
TEMPLATE = app
LIBS += -L/usr/lib/libcryptopp.a
#LIBS += -lcryptopp
CONFIG += console c++11
...
When you build Crypto++, you should use the same compiler and flags for the library and app. I suggest the following.
Crypto++:
# Be sure to 'mv config.recommend config.h'
export CXXFAGS="-DNDEBUG -g2 -O3 -std=c++11"
make static dynamic test
Qt App
# main.pro file
QMAKE_CXXFLAGS += -DNDEBUG -g2 -O3
Also see GNUmakefile | Building the Library on the Crypto++ wiki.
hashdata.o: In function `hashdata::hashfunction(std::string)':
hashdata.cpp:(.text+0x1fb): undefined reference to `CryptoPP::Algorithm::Algorithm(bool)'
hashdata.cpp:(.text+0x270): undefined reference to `CryptoPP::SHA512::InitState(unsigned long long*)'
...
These are coming from source (*.cpp) files. I'm guessing (and its purely a guess) one of two problems:
C++03 vs C++11 is causing missing symbols
QT Creator is not using libcryptopp.a
Use nm to inspect the symbols. Something like the following (the ' T " tells you its defined and in the text section):
$ nm libcryptopp.a 2>/dev/null | c++filt | \
grep 'Algorithm::Algorithm(bool)' | grep ' T '
0000000000000060 T CryptoPP::Algorithm::Algorithm(bool)
0000000000000070 T CryptoPP::Algorithm::Algorithm(bool)
If the symbols are present by QT Creator is not finding the Crypto++ library, then see something like Adding external library into Qt Creator project.
From Comments:
-lcryptopp works, but I don't know why -L/usr/lib/libcryptopp.a doesn't. ... Because if a person had both static and dynamic libraries, I still don't know how to force linking static ones.
An archive, like libcryptopp.a, is a collection of object files. You add it to OBJECTS, not LIBS, so you want something like:
# main.pro file
OBJECTS += /usr/lib/libcryptopp.a
You use -L to specify a library path to a linker. It does not make much sense to -L/usr/lib/libcryptopp.a since its used for paths.
Additional note is that when both the static and dynamic libs were present it was automatically linking the dynamic lib. Do you know how to force static linking ?
On Linux, you can force static linking by either (1) -Bstatic -lcryptopp; or (2) directly specifying /usr/lib/libcryptopp.a. The Crypto++ test program uses method (2):
g++ main.cpp /usr/lib/libcryptopp.a -o main.exe
On OS X, the linker always links to the dynamic object. It even does so on iOS, where userland is usually not allowed to load dynamic objects. To avoid dynamic linking, either (1) move or rename the *.dylib; or (2) directly specifying /usr/lib/libcryptopp.a. The Crypto++ test program uses method (2):
g++ main.cpp /usr/lib/libcryptopp.a -o main.exe

Undefined reference to boost libraries in QT creator on Windows

I am trying to run a QT project on Windows that I have developed on Linux Ubuntu. Unfortunately, I cannot manage it to properly link the boost library. Here is a detailed description of the problem.
I downloaded MinGW from https://nuwen.net/mingw.html, version 13.5, such that the gcc version 5.3.0 matches the MinGW version of QT. The reason to choose this version of MinGW is that it contains the boost libraries for compression such as zlib. From the command line I compile and run my program without any problems:
g++ -std=c++11 -w -IC:/MinGW/include -LC:/MinGW/lib bAya.cpp A.o B.o C.o -o baya -lboost_iostreams -lz -lbz2
where A,B, C are my pre-compiled object files.
In QT creator I selected the MinGW compiler (C:\MinGW_53\MinGW\bin\g++.exe) and created a new default kit with the compiler.
My .pro file looks as follows:
MAKE_CXXFLAGS += -std=c++11
INCLUDEPATH += C:/MinGW_53/MinGW/include \
C:/Users/kuzk/Documents/src
LIBS += -LC:/MinGW_53/MinGW/lib/mylib \
-lboost_system \
-lboost_iostreams \
-lz \
-lbz2
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
SOURCES += main.cpp\
mainwindow.cpp \
../src/A.cpp \
../src/B.cpp \
../src/C.cpp
HEADERS += mainwindow.h \
../src/A.h \
../src/B.h \
../src/C.h
The folder C:/MinGW_53/MinGW/lib/mylib contains my .a files such as libboost_iostreams.a
When I build it, I get many undefined reference errors such as
error: undefined reference to `boost::iostreams::zlib::default_strategy'
Interestingly, if C:/MinGW_53/MinGW/lib/mylib contains also libstdc++.a I also get linking errors to std::cout, std::ifstream etc.
I am currently stuck and I will be very helpful for any feedback.
Best,
Konstantin
Adding the library path for boost_iostreams:
-LC:/MinGW/lib \
after the line:
LIBS += -LC:/MinGW_53/MinGW/lib/mylib \
should fix it.
At the end I was able to resolve the problem. Here is what I did. I am using QT 5.7 on Windows 10.
I compiled boost using QT's console as described here: http://cpp-qt-mac-win.blogspot.co.uk/2011/10/qt-boost-for-beginners-step-by-step.html
Then I created a new project and added the paths to boost and the necessary libraries, as described in my question.
An important note. I tried to update the paths in the existing project, by running qmake and then rebuilding the project. I was getting the very same errors as I was getting with the old paths to "standard" boost. Apparently, something was cached and the changes didn't do anything. However, if I give a wrong path to a library it complained. So, QT has very confusing behavior. If someone can explain it, it will be helpful.

Using Magick++ in Qt Creator

I am creating a Qt widget with a backend representation I wrote separately. The backend uses Magick++, and I can get it to compile from the command line:
g++ -c ../SpriteCreator/WriteGIF.cpp sprite.cpp -I ../SpriteCreator/ Magick++-config --cxxflags --cppflags Magick++-config --ldflags --libs -O2
but when I try to compile the project Qt Creator it tells me
/home/tpope/obeyYourThirst/qtSpriteEditor/backend/sprite.cpp:15: error: Magick++.h: No such file or directory
#include < Magick++.h>
I added the path for Magick++.h to the INCLUDEPATH, but now it has an error similar to this:
/home/tpope/obeyYourThirst/qtSpriteEditor/backend/sprite.cpp:66: error: undefined reference to `Magick::InitializeMagick(char const*)'
for every use of a Magick function. It seems to be not including the library. How do I do that in Qt Creator?
Since the .pro files in Qt Creator is (as far as I can tell) used to generate a Makefile, we can use make's ability to run a program on the shell and save its output.
I have added:
QMAKE_CXXFLAGS += $(shell Magick++-config --cppflags --cxxflags)
and
LIBS += $(shell Magick++-config --ldflags --libs)
to my .pro file, and I was able to add:
#include <Magick++.h>
to my program without a compile error, then compile a simple example (Just put out an animated .gif with a couple of colored pixels)
This obviously makes things like cross-compiling a little tricky (I don't know how to reference foreign libraries), but that's a problem for another time.

Compile OpenGL application from Windows on Linux

My friend made OpenGL graphic engine, but he is working on Windows. I want to compile project with it.
I installed all required libs with headers, but now problem is with linking (project in Code::Blocks). I found paths for /usr/lib/libSOIL.a and /usr/local/lib/libglfw3.a, but what about:
C:\Program Files (x86)\CodeBlocks\MinGW\lib\libopengl32.a
C:\Program Files (x86)\CodeBlocks\MinGW\lib\assimp_debug-dll_win32\assimp.lib
Also, what I must modify in project file to compile it? It requires: glfw3, glm, gl3w.h, assimp, SOIL (this is what I get from .hpp files). I installed all headers (downloaded sources and make && make install)...
I tried to compile it from terminal with g++, but I don't know switches for libraries.
Current situation:
$ g++ Camera.o Entity.o Frustum.o gl3w.o Light.o Material.o Mesh.o Model.o ModelPart.o Shader.o Texture.o Utilities.o ../main.o -o main -L/usr/local/lib/libglfw3.a -lX11 -lXext -lXt -lSM -lGLU -lglut -lSOIL
/usr/bin/ld: gl3w.o: undefined reference to symbol 'glXGetProcAddress'
/usr/bin/ld: note: 'glXGetProcAddress' is defined in DSO /usr/lib/nvidia-313-updates/libGL.so.1 so try adding it to the linker command line
/usr/lib/nvidia-313-updates/libGL.so.1: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
(i added too much libraries to command line, I know)
EDIT
Added -lGL and -ldl and some problems comes out. Now, I'll trying compile it with makefile...
libopengl32 -> libGL.a
assimp -> libassimp.a ?
You gotta provide the Makefile you're compiling it with.

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

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