I want to provide options to the linker when building a static library using qmake. Say I'd want to get verbose linker output when building with MSVC. The project file looks as follows:
# mylib.pro
TEMPLATE = lib
TARGET = mylib
CONFIG += staticlib
QT += core
win32-msvc*: QMAKE_LFLAGS += /VERBOSE
unix: QMAKE_LFLAGS += -v
That's the entire project file. It should result in an empty static library with no objects in it.
Setting neither QMAKE_LFLAGS nor QMAKE_LFLAGS_STATIC_LIB nor LIBS has any effect on the linker. Nothing set in those variables even makes it to the Makefile. If QMAKE_LFLAGS worked, I'd expect to see /VERBOSE or -v passed to the linker on the command line, as appropriate for given platform.
It doesn't matter what makefile generator is used, this behavior seems to be consistent. The two platforms of interest are.
qmake -spec win32-msvc2008
qmake -spec macx-llvm
Due to cross-platform nature of qmake, you can test it on any platform where you happen to have Qt installed. This reproduces on qmake from both Qt 4.8.4 and 5.1.1. The msvc version given in the mkspec doesn't matter.
In staticlib projects, the LFLAGS are not passed to the linker. In fact, there's no documented way to pass such flags.
The solution is generator-dependent.
For msvc_nmake, LIBFLAGS are passed to the linker instead. To get verbose output, you might add
QMAKE_LIBFLAGS += /VERBOSE
To verify that it works, on any system, you can invoke qmake -spec win32-msvc2008; the particular msvc version doesn't matter.
For unixmake, AR is used to invoke the linker, so you have to add the flags to QMAKE_AR. To get verbose output, you might add
QMAKE_AR += -v
To verify, invoke qmake -spec macx-llvm; any other unix spec should work as well.
Related
I am statically linking an QT application, but for some reason when i open the executable i am getting errors for missing dlls.
Basically i am linking the following libs:
### LINKER FLAGS ###
LDFLAGS = -LC:/Qt/5.15.0/mingw81_32/lib
LDLIBS = -lQt5Widgets -lQt5Gui -lQt5Core
Why am i getting errors for missing dlls when i am statically linking everything?
The compilation and linking passes correctly, the executable is generated also..i have no idea why it needs dlls.
EDIT:
Missing dlls:
libgcc_s_dw2-1.dll,
libstdc++-6.dll,
Qt5Code.dll,
Qt5Widgets.dll
To elaborate on CristiFati's reply, you need a line like this in your project file:
QMAKE_LFLAGS += -static-libstdc++ -static-libgcc
though I'd prefer:
QMAKE_LFLAGS_RELEASE += -static-libstdc++ -static-libgcc
Note that if you want a truly standalone, self-contained binary, you'll need to build yourself a static Qt library. That's kind of a big deal, and unfortunately not as well documented as most of Qt-land is.
I have a c program, that if I build it in the shell with this command:
gcc -o simpledemo -fpic -fsigned-char -DPLATFORM_LINUX -Iinclude/ simpledemo.c ../AcapelaLibs/libbabile.a -lstdc++
it compiles, runs, and produces the expected output.
gcc info:
/usr/bin/gcc
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4
However, if I compile it with Qt Creator (Qt 5.5.1), it compiles and runs but a NULL value is returned from a function of the library I use.
Qt uses the following g++:
/usr/bin/g++
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4
I think it might be something with the compiler flags. This is what I'm doing in Qt to match the shell command:
in .pro file:
QMAKE_CXXFLAGS += -DPLATFORM_LINUX
INCLUDEPATH += $$PWD/include/
I think the INCLUDEPATH is OK because I can access files there from within my code (they are recognizable).
And note that in the shell compilation I use a library ../AcapelaLibs/libbabile.a, so in Qt I click Add Library... and choose that same library, and Qt adds it to the .pro file. Also here I can access library functions from within my code.
The problem is that a function from libbabile.a returns NULL if I compile and run from Qt (or compile in Qt and run from shell).
What's the difference between the 2 methods that makes one of them succeed and the other fail?
Library function behaves differently if compiled in Qt Creator. I
think it might be something with the compiler flags.
What compiler flags are different from used in command line?
You can examine Qt Creator compiler output and get all the compiler options specified from there. Then you can compare those with expected set of options and deliberately add or remove those options in your project .pro file:
# C++ flags
QMAKE_CXXFLAGS += -opt1 -opt2 # add
QMAKE_CXXFLAGS -= -opt1 -opt2 # remove
QMAKE_CXXFLAGS_RELEASE += -opt1 -opt2 # add
QMAKE_CXXFLAGS_RELEASE -= -opt1 -opt2 # remove
QMAKE_CXXFLAGS_DEBUG += -opt1 -opt2 # add
QMAKE_CXXFLAGS_DEBUG -= -opt1 -opt2 # remove
# C flags, slightly different macro
QMAKE_CFLAGS += -opt1 -opt2 # mind add/remove/debug/release
For both debug and release modes and separately.
I'm writing a small C++ program (with a GUI) with Qt Creator and compiling with MinGW. Everything works fine when I compile the project in debug mode but as soon as I move to release mode I get compiler errors:
undefined reference to 'std::out_of_range::~out_of_range()' thread.cpp
When I click on the error I also get:
File not found: thread.cpp
I have looked through my Boost installation and found thread.cpp and it should be on the include path for my project.
Any ideas?
EDIT: Here is my .pro file:
#-------------------------------------------------
#
# Project created by QtCreator 2012-08-10T12:09:39
#
#-------------------------------------------------
QT += core gui
TARGET = GeneDropWin
TEMPLATE = app
SOURCES += main.cpp \
genedrop.cpp \
mainbody.cpp \
biofunctions.cpp \
fileio.cpp \
settings.cpp
HEADERS += genedrop.h \
geneclasses.h \
paramclass.h \
mainbody.h \
biofunctions.h \
fileio.h \
geneclasses.h \
settings.h
FORMS += genedrop.ui \
settings.ui
#Stuff I've added
INCLUDEPATH += "C:\\Program Files\\boost_1_50_0"
LIBS += -L"C:\\Program Files\\boost_1_50_0\\stage\\lib" -lboost_thread-mgw46-mt-1_50 -lboost_system-mgw46-mt-1_50 -lboost_date_time-mgw46-mt-1_50 -lboost_chrono-mgw46-mt-1_50
CONFIG += static \
release
RESOURCES += \
NIABLogo.qrc
Ok, the problem is fixed but perhaps not completely understood. I fixed it by switching compiler to MSVC and changing the syntax for the linker options (e.g. -lboost_thread-mgw46-mt-1_50 -> -llibboost_thread-vc100-mt-1_50), compiles without an issue now. I will put forward my reasoning as to what I think may have been the problem but would appreciate a better answer if wrong:
Although I thought I had built the Boost libraries with MinGW when looking through the installation I found a number of folders mentioning msvc instead (e.g. ...boost\bin.v2\libs\date_time\build_msvc-10.0) which suggested to me that I had built it with MSVC.
The linker error claimed not to be able to find files associated with thread.cpp.
Looking at the thread folder of the build directory all the .obj and .lib files had msvc-10.0 folders in their path.
Thus I think that the MinGW compiler was looking for boost objects within a non-existent MinGW folder and so was failing. The fact that it worked under debug mode I guess relates to a less-constrained search for files.
I have a project in Qt which consists of the set of projects. Projects is building via MinGW and it does not create any .cov file.
I installed bullseye and enabled coverage build but it still does not create *.con file if I try to set path variable like
C:\Program Files\BullseyeCoverage\bin;%PATH%
the build is constantly failing I always get
gcc: CreateProcess: No such file or directory
The file to build whole project is following :
d:\QtSDK\Desktop\Qt\4.7.4\mingw\bin\qmake.exe Simulator.pro -r -spec win32-g++ "CONFIG+=release"
PATH=D:\QtSDK\mingw\bin\;%PATH%
D:\QtSDK\mingw\bin\mingw32-make -f ./Makefile
How can I measure code coverage? Please help.
If somebody interested in this question - I already figured it out.
Here is a solution -
To set bullseye interceptors before your gcc compiler just replace variables
QMAKE_CC = %Bullseye gcc path%
QMAKE_CXX = %Bullseye g++ path%
This variable should be defined in *.pro file or in gcc config file, which is located in
%QTSDKPath%\Desktop\Qt\4.7.4\mingw\mkspecs\win32-g++\ // if you are using gcc from win32
You also need to set environment variable:
BULLSEYE_PATH=%PATH TO REAL COMPILER%
How to change compiler (GCC) in Qt? I've installed GCC 4.4 and 4.6. At the momment Qt uses 4.4 but I'd like it to use 4.6. How to do it?
In the build sequence it may have a qmake command like qmake YourProject.pro -r -spec linux-g++-64 the choice of the tool chain is done in the spec file here linux-g++-64. Your will find this file in path-to-the-sdk/qt/mkspecs/linux-g++-64 (you get the concept right?)... If you open the spec file you will see that it includes the linux spec and the g++ spec.
One solution is to copy the g++ spec file and rename it g++-4.6 for example edit it and change :
QMAKE_CC = gcc
QMAKE_CXX = g++
to :
QMAKE_CC = gcc-4.6
QMAKE_CXX = g++-4.6
Idem for the linux-g++-64 it can be copied to linux-g++-4.6-64 and modify the include(...) command to include your new g++-4.6 file.
Finally build your project with qmake YourProject.pro -r -spec linux-g++-4.6-64.
I hope it's clear :) ...
I realise I am very late for the party but on Linux it is as simple as follows:
qmake -makefile <your-project.pro> -spec linux-clang
On my system, all sorts of different mkspecs are available at:
/usr/lib/x86_64-linux-gnu/qt5/mkspecs
Running make CC=my-custom-gcc CXX=my-custom-g++ LINK=my-custom-g++ seems to do the trick for me. But it might not be 100% safe (i.e. I wouldn't be surprised if running qmake with Linux specs and then specifying mingw32 compilers would fail).
If you are using Qt Creator 2.2.0, you can try Tools > Options > Tool Chains and then Add > MinGW.