Compiling standalone Qt application using MinGW - c++

I am trying to build a standalone Qt app without any DLLs needed. I recompiled Qt 5.4.1 statically. When I compile and run an application, it doesn't require any Qt DLLs, but it requires libgcc_s_dw2-1.dll instead. I have also edited my mkspecs before configuring and building Qt, I edited these values:
QMAKE_CFLAGS = -pipe -fno-keep-inline-dllexport -static -static-libgcc
QMAKE_CXXFLAGS = -pipe -fno-keep-inline-dllexport -static -static-libgcc -static-libstdc++
(added -static -static-libstdc++ and -static-libgcc)
I also added a QMAKESPECS environment variable.
When I build something using Qt, I can always see this options in the output, so I am sure that the mkspecs are applying.
When I build a non-Qt program with these options (-static -static-libgcc -static-libstdc++), it doesn't need any DLLs when I run it.
Can somebody help me?
I use Qt 5.4.1 and MinGW-w64 4.9.2

I solved my problem now. The problem was that although I edited the variable QMAKE_CXXFLAGS, it was still linking the standard libraries dynamically when linking the application itself, because it doesn't use this variable in the final step of the compilation. I only edited the mkspecs again and added the -static option to the variable QMAKE_LIBS and it works now, I have a standalone Qt application.

Related

Error loading SDL2 shared libraries while executing program on another pc

I'm trying to compile a program i made using SDL2 to work on others computers (or testing VM in this case).
I've been compiling it with what i think are the correct flags, e.g. g++ main.cpp -o main -lSDL2, however when i try executing it on another Ubuntu installation i get this error.
error while loading shared libraries: libSDL2-2.0.so.0: cannot open shared object file: No such file or directory
From my understanding it's not a problem in my compiling but with how i expect it to work on another Linux installation; I've cross-compiled (using mingw32) and tested it (using a freshly installed VM) on Windows adding the correct dlls with the exe (seems to work fine) and I was expecting for it to work in a similar fashion.
What's the standard in this cases? Should i write a setup scripts to install the library dependencies on the target machine? Is there another way I'm not aware of? I've never released an application for Linux (nor Windows) and I'm struggling to find the resources to do things "the right way".
Thanks for everyone suggestions, I ended up settling for the easy way, compiling the "easy to install" libraries dynamically e.g.-lSDL2 and the others statically (checked the licenses and it should be fine) like so:
g++ main.cpp -o main -Wl,-Bdynamic -lSDL2 -lSDL2_image -lSDL2_ttf -Wl,-Bstatic -lSDL2_gfx -static-libgcc -static-libstdc++
I'll put in my documentation how to install the required SDL2 libraries.
I am not sure how familiar you are with pkg-config, but the output for sdl2 is this:
-D_REENTRANT -I/usr/include/SDL2 -lSDL2
This was found from running this:
pkg-config --cflags --libs sdl2
Basically, you need to point to where SDL2 is located BEFORE you actually link to it.
The tool pkg-config is designed to tell you the information you need when you want to link to a package in Linux. You were linking with the library, but you forgot to tell GCC where the library is located.
If you want to compile you code, try this:
g++ main.cpp -o runme `pkg-config --cflags --libs sdl2`
This will automatically grab all of the flags that you need to compile with SDL2 included.
Oh, and you should note, ORDER MATTERS WHEN ADDING FLAGS AND LIBRARIES!!!
There are many questions on SO where the order of compiler options caused all of the problems. Do not be like those people. I suggest you search SO for more info on that.

Qt creator not seeing Mac Frameworks

I am using Qt 5.6 with Qt Creator 4.3 on macOS Sierra 10.12.
I am building a Qt based application that uses OpenGL and OpenCL.
Software or system updates somewhere appear to have broken Qt Creators' ability to find the headers for OpenGL and OpenCL headers.
I have tried reinstalling Qt, Reinstalling Xcode and the Mac SDK and none of these options make it work.
The weird thing is my app compiles. QtCreator just cannot find the headers itself - this makes code navigation somewhat more complex.
So lets take OpenCL. I include it like this:
#ifdef __APPLE__
#include <OpenCL/cl.h>
#include <OpenCL/opencl.h>
#endif
My app.pro file for qmake has the following inside it:
macx:QMAKE_LFLAGS += -framework OpenCL
And with this it seems to compile. However the two OpenCL #include above in QtCreator are yellow underlined with a tooltip saying "OpenCL/cl.h: No such file or directory".
Now I can see that it compiles successfully as qmake is working out where the frameworks are and appending the paths successfully:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -c -pipe -stdlib=libc++ -g -std=gnu++11 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -mmacosx-version-min=10.7 -Wall -W -fPIC -DQT_DEPRECATED_WARNINGS -DQT_QML_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/Users/me/Projects/app -I. -I/Users/me/Qt5.6.2/5.6/clang_64/lib/QtWidgets.framework/Headers -I/Users/me/Qt5.6.2/5.6/clang_64/lib/QtGui.framework/Headers -I/Users/me/Qt5.6.2/5.6/clang_64/lib/QtCore.framework/Headers -I. -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/OpenGL.framework/Headers -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/AGL.framework/Headers -I. -I/Users/me/Qt5.6.2/5.6/clang_64/mkspecs/macx-clang -F/Users/me/Qt5.6.2/5.6/clang_64/lib -o main.o /Users/me/Projects/app/main.cpp
Now if I navigate to the following directory the headers are there and present:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/OpenCL.framework/Versions/A
So I cannot see what is missing.
I have tried to "teach" Qt Creator about this framework directory with combinations of the following, but I havent found something that will work:
macx:QMAKE_LFLAGS += -framework OpenCL
macx:INCLUDEPATH += /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/OpenCL.framework/Versions/A/Headers
macx:QMAKE_LFLAGS += -F/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks
macx:LIBS += -framework OpenCL
I'm struggling to find information about how QtCreator find the system frameworks on a mac and how i get it to start finding them again. Any help would be massively appreciated.
For anybody that has the same problem the answer was actually rather simple in the end.
When you setup the kit in Qt creator (which tells it what compiler and debugger to use amongst other things) you need to be careful with the compiler used. I had two copies of clang on my machine, one was installed in /usr/bin (the system one) and the other was installed inside the XCode app bundle. My kit was using the Xcode installed one as thats where the libraries live. While this compiled just fine, Qt creator for some reason does not hook up the paths to libraries properly. Simply switching this over to using the one in /usr/bin solved it.
Note that i did not explicitly ask it to use the one in the Xcode app bundle, thats just the one that was automatically selected when the project was first opened in Qt creator. So you might have to adjust its default behaviour in order to fix things.

CMake / g++, library not being linked in OpenFOAM application

I am trying to compile a custom OpenFOAM application. My build procedure is with CMake (though I'm not sure this has anything to do with my current problem).
For those familiar with OpenFOAM, this is the pisoFoam application, and the problem library is the incompressibleLESModels.so library.
The project builds without any problems. And runs until it needs to make use of the IncompressibleLESModels library. At this point, the app claims not to know anything about that library and stops.
I have included the incompressibleLESModels library in my TARGET_LINK_LIBRARIES within the CMake script (along with all the other necessary libraries).
OpenFOAM allows the user to link in libraries at run-time via an input file. This method works fine (i.e., I can get the app to dynamically load in the incompressibleLESModels lib and run). But I would rather not rely on this method. And the standard OpenFOAM apps don't do this.
When I run ldd on my executable, the incompressibleLESModels library is clearly not in the list of libraries.
So it is as if the linker detects that the library is not needed during the link phase and chooses not to link it in. From what I understand, this may be due to definitions passed to gcc, particularly add-needed, or as-needed and no-as-needed.
I am adding the following definitions via the ADD_DEFINITIONS command in CMake:
-DWM_DP -m64 -Dlinux64 -Wall -Wextra -Wno-unused-parameter -Wold-style-cast
-Wnon-virtual-dtor -O3 -DNoRepository -ftemplate-depth-100 -fPIC -Xlinker
--add-needed -Xlinker --no-as-needed
Any ideas are greatly appreciated.
Kind regards, Madeleine
Since you are using cmake, for linking directives instead of using add_definitions, use target_link_libraries
target_link_libraries(<targetname> "-Wl,--no-as-needed")
target_link_libraries(<targetname> <libraries that you want to link even if apparently not necessary>)
target_link_libraries(<targetname> "-Wl,--add-needed")
target_link_libraries(<targetname> <libraries that you want to link according to the "default" criteria>)
Reference: http://www.cmake.org/cmake/help/v2.8.11/cmake.html#command:target_link_libraries
Also, for -fPIC and the like it's advisable that you use variables like CMAKE_CXX_FLAGS, and that you append flags to it.
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
Note: using list and append will not work with this, as normally lists elements are separated with semicolons. You can have flags depending on the type of build, for example CMAKE_CXX_FLAGS_DEBUG and CMAKE_CXX_FLAGS_RELEASE.
http://www.cmake.org/cmake/help/v2.8.11/cmake.html#variable:CMAKE_LANG_FLAGS_DEBUG
http://www.cmake.org/cmake/help/v2.8.11/cmake.html#variable:CMAKE_LANG_FLAGS_RELEASE

Errors in QT headers using clang

Questions:
My question is thus: How do I build my QT project without turning off warnings altogether (or having to sort through a million purposeless ones to find my own)? Can I suppress warnings for just the QT headers?
Details:
Issue
A number of months ago, I started a QT project in QT-Creator. At the time I was using gcc 4.6. After a bit other priorities asserted themselves and I found myself without time to work on the project until now. In the interim I switched to using clang. When I configured my QT project to use clang -- which project compiled without warnings in g++ -- it generated some 263 warnings all within the QT headers themselves. Mostly sign-conversion and unreachable-code.
Attempts
To try and get around this I added -isystem /path/to/QT/include/dir based on this entry in the Clang User Manual, but it did not seem to affect anything. Though I am not certain, I think it is because my code #include's the QT headers by name, not by directory. While the solution to that might be to manually list every single QT header used (have not tried), it would mean I would have to update it every time I upgraded QT or used a new header. Surely there is a better solution.
As requested here is the actual compile command being executed:
clang++ -c -pipe -Qunused-arguments -Weverything -cxx-isystem /path/to/qt/4.8.3/include/ -g -D_REENTRANT -DQT_NO_KEYWORDS -DQT_SHARED -I/path/to/qt/x86_64/4.8.3/mkspecs/unsupported/linux-clang -I. -I.moc -I.ui -I/path/to/qt/4.8.3/include/ -o .obj/main.o main.cpp
Specs
I am using:
Linux 3.2.0-40-generic #64-Ubuntu SMP x86_64 GNU/Linux
Though others on my team use Windows
QT creator 2.6.2
QT 4.8.3
clang version 3.2 (trunk 165250) (llvm/trunk 165249)
Target: x86_64-unknown-linux-gnu
I will answer my own question because, as it turns out, it is a specific environmental quirk in this case.
I have two copies of the QT libraries on my dev machine, one system-wide and one project specific (included in the VCS). The project libraries do not have qmake included, so I used my system qmake, which appended a different path than I was including in my -isystem specifications. To solve this, I added
QMAKE_INCDIR_QT =
to qmake.conf (in qt/mkspecs/unsupported/linux-clang/)
Since someone else on the project had fanangled qmake into using the project libraries everywhere else.
For those who stumble upon this question with a more general problem than the author.
Try inserting:
LIBS_USED_FOR_QT = QtCore QtSql QtMultimediaWidgets QtSensors QtSvg QtXml QtPrintSupport QtWidgets QtQuick QtQml QtPositioning QtGui QtWebKitWidgets
for(somelib, $$list($$LIBS_USED_FOR_QT)) {
QMAKE_CXXFLAGS += -isystem $$(QTDIR)/lib/$${somelib}.framework/Versions/5/Headers/
QMAKE_CXXFLAGS += -isystem $$(QTDIR)/lib/$${somelib}.framework/Headers/
}
in your .pro file.
Addtionally avoid includes like #include <QtCore/QtCore>
writing #include <QtCore> instead
This tamed qt quite efficiently for me.
see also this source

Help on build using g++ on Windows

There is a small project C++ (it has win32 code) that I need to build. It already has its Makefile. I was told to use MinGW. I have never used it before. I downloaded and installed the latest MinGW installer.
Then, I opened the MinGW shell and did make. The exe file was created. But when I try to run it I get libgcc_s_dw2-1.dll is missing! Why do I get this error? Shouldn't the exe be self-contained and run anywhere?
UPDATE
Here's more information, from the Makefile:
CC = g++
CCOPTIONS=-DWINDOWS -DFORCEINLINE -DMINGW -DSRTP_SUPPORT -D__EXPORT= -D_WIN32_WINNT=0x0501 -DNOMVS
setup.exe: setup.o common.o
$(CC) -ggdb -g -O0 -o $# setup.o common.o -mno-cygwin -mwindows -lwsock32 -lws2_32 -lwinmm -lgdi32 -lcomctl32 -lmapi32 -lVfw32
Whether or not the exe should be self-contained depends on how you built it. We need to see the commands that were executed, or post the makefile. But that DLL does not seem to be part of the current version of MinGW. Also, please clarify if you are actually doing this under cygwin, or if you added the tag by mistake.
Edit: A bit of googling seems like it has to do with the horrible "official" MinGW installation. Remove it, and download the Twilight Dragon build from http://tdm-gcc.tdragon.net and then rebuild completely. The "official" build is cr*p anyway - I don't know why anyone uses it.
If what you want to do is a stand alone application with MinGW, you should add -static to the linking options.
On a side note, if you're making a Win32 application, add the -mwindows option to the C++ compiler so it doesn't open a console together with your main window.