How to create executable file for a Qt Application? - c++

I've been searching on the internet for some useful and clear information about this, it's annoying that such a trivial thing is so hard to do.
What I need is to create an executable file for my Qt project, in order to run it in other computers. My project runs nicely in the Qt Creator, but when I try to run the executable created in the release folder I get errors about some dll missing, and though I have put all of them in the same directory of my .exe I now got some errors about entrypoints in procedures inside the dll (QtCore4.dll).
The solution looks like to be a static linking, I've found many information about it, but none of them really helped me. Here is what I have tried so far:
Using the executable created in the release folder as I said above.
Static Linking, following this tutorial (and many others, all almost the same thing): http://doc.qt.io/qt-4.8/deployment-windows.html but I get the following error during the "configure -static": NMAKE : fatal error U1077: '"c:\Arquivos de programas\Microsoft Visual Studio 9.
0\VC\BIN\cl.EXE"' : return code '0x2'
Stop.
Building qmake failed, return code 2
For that I couldn't find any solution, so here I am, just trying to create an executable file for my Qt project, hope you guys can give me some light here.

Basically you have to look for mingw subfolder deep into qt tree, where qt utilities reside, and copy needed dll's.
These are the steps I follow, based upon QT 4.7.4, for packaging the application with correct shared libraries.
Let's say you've installed QT under c:\qtsdk
Open your project, and compile it in release mode.
Go to this directory: C:\QtSDK\Desktop\Qt\4.7.4\mingw\bin -- it contains all shared libraries. Debug libraries end with a "d" -- frex, QtCore.dll is release version, while QtCoreD.dll is debug version
Copy at least these files into your release directory (where your .exe lies)
mingwm10.dll
libgcc_s_dw2-1.dll
QtCore4.dll
QtGui4.dll
I just built, tested and deployed a dummy project this way.

Do these steps:
1- Open qt command-line window.
2- Go to the directory which .exe file exists (using cd command).
3- Run windeployqt.exe --quick . command
4- Run your exe file and enjoy it!
This video is great

Here is what I did and I got it to work after some time;after making sure that your program builds inside Qtcreator,go to the directory that contains your exe and double click on it.(I'm using windows and don't know if it is the same on other platforms).A window should popup telling you which .dll is missing.Go to the directory where Qt is installed and type the exact name in the search bar(top-right in Windows 7) and the dll should show up in search results.Right click and choose open file location.Copy the file where your exe is located .Double click on the exe again.If another .dll is missing repeat the same thing until none is missing.In the end your program should run.
Hope this helps.

So you downloaded Qt SDK and you want to link Qt libraries statically into you application, right?
First of all, if you link statically, you don't need any Qt*.dll to be present anywhere because it is built into the application. So that alone means that your application is built using dynamic linking.
From my experience, that safest way to get it all working is:
Download Qt sources, build and install them with nmake ( using Visual Studio Command Prompt from Menu Start -> Application -> Visual Studio 2008 ). Just remember that the size of the statically linked application is dependent on the size of the *.libs and these are dependent on the pre-build configuration, for example, you can minimize the size of you application by turning off some features ( this is an example config for MacOSX, just watch out for the ones specific for Windows and add proper plaftorm - see configure.exe --help for details ):
./configure -arch x86 -platform macx-g++ --universal -cocoa -debug-and-release -opensource -static -fast -no-accessibility -no-sql-sqlite -no-qt3support -no-opengl -no-openvg -qt-zlib -no-gif -no-libmng -qt-libmng -no-libtiff -qt-libjpeg -no-3dnow -sse -no-sse2 -no-openssl -no-dbus -no-phonon -no-multimedia -no-audio-backend -webkit -no-script -no-scripttools -no-declarative -nomake demos -nomake examples -no-exceptions -no-accessibility -confirm-license
Install, setup and use Visual Studio Add-In for you Qt project (here ).
Good Luck! :)

I share with you my solution. On my qt creator 4.2.1, I did these steps:
1) - I clicked on release and it generated a folder in my project directory like that: C:\Users\Samir\Documents\Qt-Projects\build-Calculator-Desktop_Qt_5_8_0_MinGW_32bit-Release.
2) - I opened the terminal qt 5.8 for desktop and I navigated to my project directory where the .exe located.
3) - Finally, I ran the command line windeployqt.exe --quick .. This command allowed me to generate the libraries which were missing.
And now everything works fine!

Related

Qt static build missing windows plugin

I've compiled Qt 5.9.3 to static library using
configure -static -release -platform win32-msvc
I'm compiling my application and it compiles and links successfully but when I start it it fails with
This application failed to start because it could not find or load the Qt platform plugin "windows"
The problem is clearly that it fails to find the qwindows plugin which is now a .lib and not a .dll
I tried adding qwindows.lib to the linker input files but that did not have any effect.
Is there a known fix for this issue?
May be you linked another Qt lib. If so, your application will require windows plugin.
Use tools like dependcy walker http://dependencywalker.com/ find out what dll your application depends on.
Tips: I used to installed anaconda, and build qt from source, my application linked qt lib of anaconda, so it require windows plugin and never find the dll.

How to run qmake from the static Qt

For making a very simple Qt app "installable" on other systems, I'm using Qt Installer Framework following this link.
In bottom, in Setting up Qt Installer Framework, number 1 orders to have Qt Installer Framework source code. I downloaded it from here. (qt-installer-framework-opensource-2.0.1-src.zip)
Now I don't understand the next instruction there :(
It says:
2- Build the tools by running the "qmake" from the static Qt, followed by "make" or "nmake".
My question is, first what does it mean?
And from what path?
I don't know how to do it:(
qmake comes with the qt-framework and is a make file generator. (an alternative to cmake).
You call qmake on a .pro file from your project. This .pro file have to contain your source code files project dependencies and more.
Consider that you have a project folder with your source code and the .pro file. Then you call the qmake command in this folder. qmake is an program itself, which you can find in the bin folder of your qt-installation. If qmake is in your path variable, you go to the terminal, navigate to the specific folder and just write:
qmake
After that qmake will create a makefile. Then you could call makeand your program will be build.
I hope my answer helps you. You can learn more about qmake on the website of Qt. Here is also a very good qmake tutorial: klick
edit:
how to call make on windows:
download and install cygwin from
http://www.cygwin.com/setup-x86.exe - 32 bit installer or
http://www.cygwin.com/setup-x86_64.exe - 64 bit installer.
then start the Cygwin terminal (Cygwin.bat) and navigate to your folder and call
make
and that will build your qt-installer!

QT missing dll after deploy

I've copied all of the dlls from QT that were required, and my application works fine on my Windows server machine.
However when trying to run it on a Windows 7 box i get the following message:
This application failed to start because it could not find or load he
Qt platform plugin "windows".
Reinstallning the application may fix this problem.
Any ideas what I'm missing here?
I'd scratched my head over this some time ago. It turned out that this was caused not by missing qwindows.dll, but rather one of libEGL.dll or libGLESv2.dll. This was tricky, because dependency walker does not show those libs as direct dependencies.
If you want to test on your dev machine, whether your app has all required libs, fire up console issue SET PATH=, cd to your app directory and run it.
This is complete list of dlls that my app is using (Qt 5.2 / QtQuick app only, rest is C++). QtQuick is nice but the size of Qt dependencies is a bit scary:
icu*.dll - depending on whether you've compiled with ICU
libEGL.dll
libGLESv2.dll
Qt5Core.dll
Qt5Gui.dll
Qt5Network.dll
Qt5Qml.dll
Qt5Quick.dll
Qt5Widgets.dll
Widely used solution is put all necessary libraris in the folder of application.
What are libraries application need?
Run application and see error message:
The program can't start because <Library name> is missing from your computer.
Try reinstalling the program to fix this problem
Library set is depended from Qt version. Run several times application and each time copy required lib you found what is neeeded for application.
In my case (Qt 5.2.1) there are
icudt51.dll,
icuin51.dll,
icuuc51.dll,
libgcc_s_dw2-1.dll,
libstdc++-6.dll,
libwinpthread-1.dll,
Qt5Core.dll,
Qt5Gui.dll,
Qt5Widgets.dll.
All libs you can found in your Qt install folder. But don't use libraries from Tools\QtCreator folder, because QtCreator has another version of these libraries!
In case of error:
This application failed to start because it could not find or load he Qt platform plugin windows. Reinstallning the application may fix this problem.
You should create folder platforms and copy qwindows.dll into it.
If you still got error you should create qt.conf file in application's folder with content:
[Paths]
Plugins=plugins
This solution is described in https://qt-project.org/forums/viewthread/37265
More information about qt.conf you can find at http://qt-project.org/doc/qt-5/qt-conf.html
In latest versions of Qt you can find deploy tool (since 5.2). This tool find necessary libraries for application and copy into application folder. You can run it something like this:
call c:\Qt\QtX.Y.Z\X.Y.Z\mingw48_32\bin\qtenv2.bat
cd /d "c:\path\to\your\application\folder"
windeployqt.exe your_application.exe
Generally it works well. But I notice that some libraries are not copied, but you can found by method is descibed at beggining of post. More useful information you can find at
http://qt-project.org/doc/qt-5/windows-deployment.html
In rare cases yo can got this error if some library is missing but not appear in error message above. Example: Qt 5.1.1: Application failed to start because platform plugin "windows" is missing
I wasn't in this situation, so I can't tell more.

Statically linked app with Qt gives error: Failed to load platform plugin "windows"

I have built Qt 5.0.1 statically in VS 2010 under Windows 7 x64. The configuration parameters were
configure -debug-and-release -opensource -confirm-license -platform win32-msvc2010 -nomake examples -nomake tests -no-webkit -static
and I performed the build with jom with following parameters:
jom -j 4
The build process was successful and I can find all the libs and link my app with Qt statically. Now the problem is that when I try to run the application, it gives me an error
Module: 5.0.1
File: kernel\qguiapplication.cpp
Line: 781
"Failed to load platform plugin "windows". Available platforms are:"
But below this error, it does not show up any platforms. I encountered the same error while linking with Qt dynamically. I got rid of that by placing dependency DLLs (qt5core.dll, qt5gui.dll etc) in the same directory of my exe. But I can't figure out a way this time.
After static build of Qt, the DLLs in the plugins/platforms/ folder are gone and they are replaced by libs. I have also tried to statically link with qwindows.lib but to no avail.
Any idea?
Regards.
To check what DLL are needed to deploy your application on Windows, use dependency Walker (http://www.dependencywalker.com/).
According to the documentation page Qt for Windows - Deployment some DLL are required to deploy a Qt application on Windows platforms:
qwindows.dll
icudtXX.dll
icuinXX.dll
icuucXX.dll
libGLESv2.dll
libEGL.dll
Depending on the compiler you use to compile the application, some other libraries may be required:
MSVC specific libraries:
msvcrXX.dll
msvcpXX.dll
MinGW specific libraries
libgcc_s_dw2-1.DLL
libstdc++-6.dll
Note: in all these files, replace XX by the current version of the DLL on your system.
If you didn't build Qt statically (default), you will also need Qt5 DLL depending on Qt module you use in your application. Example:
Qt5Core.dll
Qt5Test.dll
Qt5Network.dll
etc.
All these DLL can be found in "bin" folder, under your Qt installation dir.
EDIT
Recently, Qt developers created a tool named windeployqt.exe which helps you to gather all required library to deploy an application:
Usage: windeployqt [options] [file]
Qt Deploy Tool 5.3.0
The simplest way to use windeployqt is to add the bin directory of your Qt
installation (e.g. <QT_DIR\bin>) to the PATH variable and then run:
windeployqt <path-to-app-binary>
If ICU, ANGLE, etc. are not in the bin directory, they need to be in the PATH
variable. If your application uses Qt Quick, run:
windeployqt --qmldir <path-to-app-qml-files> <path-to-app-binary>
Options:
-?, -h, --help Displays this help.
-v, --version Displays version information.
--dir <directory> Use directory instead of binary directory.
--libdir <path> Copy libraries to path.
--debug Assume debug binaries.
--release Assume release binaries.
--force Force updating files.
--dry-run Simulation mode. Behave normally, but do not
copy/update any files.
--no-plugins Skip plugin deployment.
--no-libraries Skip library deployment.
--qmldir <directory> Scan for QML-imports starting from directory.
--no-quick-import Skip deployment of Qt Quick imports.
--no-translations Skip deployment of translations.
--no-system-d3d-compiler Skip deployment of the system D3D compiler.
--compiler-runtime Deploy compiler runtime (Desktop only).
--no-compiler-runtime Do not deploy compiler runtime (Desktop only).
--webkit2 Deployment of WebKit2 (web process).
--no-webkit2 Skip deployment of WebKit2.
--json Print to stdout in JSON format.
--list <option> Print only the names of the files copied.
Available options:
source: absolute path of the source files
target: absolute path of the target files
relative: paths of the target files, relative
to the target directory
mapping: outputs the source and the relative
target, suitable for use within an
Appx mapping file
--verbose <level> Verbose level.
Qt libraries can be added by passing their name (-xml) or removed by passing
the name prepended by --no- (--no-xml). Available libraries:
bluetooth clucene concurrent core declarative designercomponents designer gui
clucene qthelp multimedia multimediawidgets multimediaquick network nfc opengl
positioning printsupport qml quick quickparticles script scripttools sensors
serialport sql svg test widgets winextras xml xmlpatterns
Arguments:
[file] Binary or directory containing the binary.
I just had the same problem, so here is my solution:
First of course build QT with the -static parameter.
Add the following libs to your build:
Qt5PlatformSupport.lib
qwindows.lib
imm32.lib
Add the following path to your library paths:
$(QTDIR)\plugins\platforms
In your code (preferably main.cpp) add the following line:
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
The resulting build should not depend on any qt-related dlls.
If you are using libGLESv2.dll, then you must include libEGL.dll, too. See
Deploying Qt C++ Application from Visual Studio qwindows.dll error
It's a Qt platform plugin issue. Using qt.conf resolved the problem for me on MacOS.
It doesn't matter if you link Qt staticly on not.
I tried to staticly link to libqcocoa.a (or qwindows.lib) without success. I will let you know if i succeed.
Just do the following steps:
Go to bin folder in your Qt installation directory
Copy all files with .dll extension
Past them to C:\Windows\System32 for 32 bits or C:\Windows\syswow64 for 64 bits.
Tada! you will never see that irritating message again

Compiling a static QT application on Windows

I am new to QT and I am enjoying the experience except for the fact I cannot manage to statically link the QT library to the output binaries. When I run the output file outside of the QT directory, I get The program can't start beacuse QtCored4.dll is missing. Obviously QT is dynamically linking their libraries and requesting a .dll I do not have. Is there a way to statically compile QT's libraries into a static binary so none of QT's dlls are required? I ask this because I am already up to 11 .dlls for my project, and I would really like to cut down the amount of files that have to be distributed with my software. Size is not a problem for me. Thanks.
I have tried adding CONFIG += static to the .pro file, but to no avail.
First off, you can't statically link Qt unless you've bought a commercial license. To do so would put you in violation of the LGPL license under which the non-commercial version is distributed. I feel your pain, I've got many, many DLL's to go with my software.
Thankfully, you probably do have the DLL's if you used the installer: you don't need to build from source, that should have been done automatically. You'll find them in Qt\Version\bin, where Qt is the directory you installed Qt, and version is the version of Qt you installed. For example, mine is found in G:\Libraries\Qt\4.7.1\bin.
I did, however, have some issues with not having one of the DLL's built - one for working with OpenGL - and performed a rebuild to do so. I've also done so when I've switched versions of Visual Studio. I think it's handy to be able to do so, it's easy, Open a terminal in the Qt directory, and execute:
configure.exe -platform XXX'
Where XXX denotes the type of build you want to perform. Valid options include win32-msvc2005, win32-msvc2008, win32-msvc2010. So I use:
configure.exe -platform win32-msvc2010'
Other options are detailed here. These instructions apply if you've downloaded the source code, however you might have to add the current directory to the path variable like so:
set PATH=%cd%\bin;%PATH%
The whole procedure should take about an hour.
You need to download the source packet of QT and compile it. It takes some time but is not really complicated.
Download and unzip QT source
Start a compiler shell (Visual Studio or mingw)
Execute configure in the QT source directory - add a flag for static compile here
execute make (Visual Studio nmake)
Wait some hours depending on the speed of your machine