How to compile staticly a Qt 5 application? - c++

I would like to compile staticly my Qt 5 application. I used this link :
Qt static linking and deployment
The problem is that I don't know where is the "configure" file or how to generate it ?
ps: the old option to add "CONFIG = static" in the .pro file doesn't work with Qt5

You have to first compile the whole Qt library statically. Then, use that configuration in your projects. Then, your application will be statically compiled.
Qt (when using qmake) takes the compilation configuration from its qmakespec, which is defined during compilation of the Qt library. This includes all the parameters that are used by default.
Keep in mind that this has a learning curve. You have to try and fail a few times. It'll cost you some time to get this right. That link I provided should make this effort easier.

The problem is that I don't know where is the "configure" file or how to generate it ?
QMake make use of several type of files:
.pro
.pri
.prf
The most common is the .pro used for pro-jects. You can find/create it at the root of your project directory.
Creating a QtCreator project will automatically generate one. Be aware that there is also the qbs alternative.
the old option to add "CONFIG = static" in the .pro file doesn't work with Qt5
CONFIG *= static still works, are you sure about any other issue somewhere else?
CONFIG = static will override any previous value, using the * will append the new value without deleting previous configurations. I suggest you to use 'message( $$CONFIG)' to ensure the content is correct.

Related

Qt Release DLL Error

I have a project in Qt Creator and am trying to compile a static release. To do so, I have added "static" to my "CONFIG" option in my .pro file.
After rebuilding all the files, I get a folder named "release" with an executable and a few other files inside of it. When trying to execute the generated file, I get an error that reads as such:
"The procedure entry point __cxa_throw_bad_array_new_length could not be located in the dynamic link library C:\Qt\5.5\mingw492_32\bin\QtCore.dll"
This error message remains whether I use mingw 5.5.0 or 5.4.2 to compile the files.
Using dependency walker and coping the "correct" QT dll files also does not resolve the problem.
What I know already: This error happens to people who copy the wrong QTCore.dll to their project folder. However, since I am not copying any .dll files, I don't know how to use this information to my advantage.
In conclusion, my question is: How do I stop this error from occurring? Moreover, is there a better way to statically compile a qt application?
To build static release of your application you basically need two things:
1) add
CONFIG += static
in your .pro file (you did it) and don't copy any Qt dlls.
2) you need to build static Qt
https://wiki.qt.io/Building_a_static_Qt_for_Windows_using_MinGW
By default Qt is installed prepared for dynamic linking, this is why you need to build static Qt on your own.
You may want to look also at this great Q&A:
Qt static linking and deployment
but it deals mainly with Qt4. Idea is the same.
After you will build static Qt you will need to rebuild your application. And don't copy any Qt dlls.

Can a project created with qmake be compiled without using qmake?

I have some source code that was built with qmake. Compiling it is supposed to be done with qmake qmakefile.pro.
I'd like to compile it without using qmake. Is that even possible? What would the steps be?
Edit: Here's the .pro file.
Yes, you can compile files without qmake.
You need to get any specific compiler settings from qmake and convert them to the other compiler as necessary.
Or you can convert the contents of the qmake.pro file into a makefile or your IDE's project settings.
If the qmake.pro is not a human readable text file, you will have to launch qmake and retrieve the settings. Otherwise search for a conversion tool.
While it is technically possible to translate the .pro file into something else, I would not advise it.
Why?
QMake does a lot of work behind the scenes.
In the case of the .pro file you referenced (http://pastebin.com/viv204Fv), QMake is invoking meta-tools (uic, moc) to create the code for the UI and adding build rules to compile it. You will need to manually code those in your build script.
-. QMake also solves issues of choosing the right compiler and linker options (including the right include files). You will need to determine them yourself and add them to your build script.
The project is already using Qt, so why not use Qt's build script generating tool, QMake? If you have installed a Qt development environment, you should already have qmake installed.

QMediaPlayer, QSound class - unable to #Include files (files are there)

I have been trying to play a simple audio file using:
http://qt-project.org/doc/qt-5/qmediaplayer.html and
http://qt-project.org/doc/qt-5/QSound.html
I am using Qt 5.3 but the problem is, when I type #include QMediaPlayer or QSound, QtCreator keeps underlining them with red because it can't find them. The thing is, the files are there. They are located in QtMultimedia/ and I have seen them. When I try to prefix QtMultimedia/QMediaPlayer or QtMultimedia/qmediaplayer.h it still can't find them.
If I type #include "QtMultimedia/" the auto-complete drop down in the text editor only shows QtMultimedia/QtMultimediaDepends. Normally you get a list of all the files in the folder but not with this one.
Looks like there is something wrong with the paths. How do I fix them? I am using a Windows machine.
Edit: I have included project settings - see screenshots.
I think, after you modified your .pro file, You didn't run the qmake.
build-> run qmake
qmake will generate the makefile you need to build the src code.
if you only change the .pro file, but didn't run it. Nothing is changed.
and that is reason, you create a new project and it is working.
Ok I figured it out. It's a problem with my Qt version.
My Qt 5.3 version is a custom compiled one. When I select the included version that came with the installer, QSound is found. When I change the kit to the custom compiled one, the file cannot be found. I must have forgotten to include some sort of multimedia option to the configure script during compilation.
Now the question is what is the missing option, I will need to do more reading...
Of course, comments and suggestions are welcome :)
You have to add the word 'multimedia' in your .pro file:
QT += core GUI
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets multimedia

Configuring Qt Creator with executable and DLL project

I am new to QT Creator coming from Visual Studio. I have a session with two projects in it. One is a DLL with some classes that I intend to use for other purposes. The other is an executable console app that uses some of the classes from the DLL.
I currently have these two projects side by side in QT Creator. I can include the header files from the DLL in my EXE project using relative paths "../MyPrject/header.h". But how do I get QT Creator to link and then copy the DLL into the executable debug folder for debugging.
Am I doing this all wrong? Is there a better way? If it includes adding code to the .pro file, please include a link so that I can learn more.
You should make some dependencies between this projects.
opening both projects - you have done.
on editor view, right click on exe-project and select add library...
follow creator hints to add it.
2nd option: you can make subprojects. follow QtCreator: Creating Projects from documentation (help view in Qt Creator)
GwyenBleidD provided a good starting point for including DLLs.
I however, have made a habit out of modifying the .pro file directly here and honestly I prefer to modify the .pro file in the event that something goes haywire.
Suppose I wanted to use the winsock DLL.
In the .pro file, I'd first specify the .dll's corresponding .lib file:
# WinSock2 library (ws2_32.lib file)
LIBS += -lws2_32
# Path to the WinSock2 library
LIBS += -L"c:/mylibraries/"
Additionally, you'll need to specify the include path to the header files here:
INCLUDEPATH += "c:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/INCLUDE"
Thirdly, in my code I'd have to make sure to include the headers for it:
// I ASSUME it'll be found under something like the
// Visual Studio/VC/INCLUDE directory mentioned above.
#include <winsock2.h>
Lastly, you need to ensure that your application can find the .dll file, typically pointed to using the %PATH% environment variable.
With regards to your setup, I'd make sure that your sub-projects are configured so that the library compiles FIRST (obviously). And then ensure that the LIBS variable in your .Pro project points correctly to your .lib destination according to the build configuration (debug|release).
Qt's PRO (qmake) isn't as terrible as some make it out to be. Just give it a solid half-hour to an hour and you'll get the hang of it. I assume though that you have a solid understanding of libs and DLLs and what not.
http://qt-project.org/doc/qt-5.0/qtdoc/qmake-manual.html
The right way is to switch on CMake based project and keep exe and dll within one root project. The main benefit of this decision is IDE independent approach: you can use Qt Creator, CLion, Visual Studio without any changes in project definition. As the start point consider to see the example project https://github.com/anatoly-spb/cmake_exe_dll

Qt on Mac: add an existing UI file without rebuilding the whole project

I have a Qt project in XCode on Mac. I want to add an existing .UI file to it. When I do that by listing in the .pri file, the XCode project itself is rebuilt by qmake, and all my post-qmake settings are lost. Is there a way to insert a UI file without listing in the .pri? It's doable in a very straightforward way on Visual Studio...
There are two custom steps in the target - both call make over files in the project bundle (xcodeproj). The first one invokes qmake. The second invokes the Qt proprocessors - uic, moc. So the answer is - comment out or delete the first step, modify the makefile of the second one by hand. Add generated files (once they're generated at least once) to the project as necessary.