QtCreator - use static dll lib - c++

I want to make my app without any dependancies for the users. It works well when I put the dll files into the same folder as my exe.
I'm trying to set all those libraries to static use the following code in my pro file (Qt 4.8.1):
QMAKE_LFLAGS += -static-libgcc
CONFIG += static
The previous code doesn't change anything.
Here are the dll file I have included into my project folder:
libgcc_s_dw2-1.dll
mingwm10.dll
QtCore4.dll
QtGui4.dll
The fact is I don't want my app be more than a file and I don't want the users to download libs files, which are heavy (9mB for the last one).
Is there a way to compile a Qt project and use static libs?
I've tried to read some tutorials I've found on the Internet but they are all outdated. For what I read too, the solution would be to rebuild the Qt source code staticaly.

To be able to fully statically compile against Qt you need to rebuild the framework as static, using the pre-compiled framework isn't enough - unless you have a commercial license and can get the static binaries.
See: http://www.formortals.com/how-to-statically-link-qt-4/, http://www.formortals.com/build-qt-static-small-microsoft-intel-gcc-compiler/ and http://doc.qt.nokia.com/4.7-snapshot/deployment.html#static-vs-shared-libraries for more information.

Download already compiled libraries. If you are building against a platform, use cross-compiled libraries of that. You can link those libraries in Makefile statically [in #INCLUDE and LIBS variable in makefile]. The only drawback is your exe size will be increased.

Related

Static libraries in Qt 5.14 MinGW toolchain? (default Qt installation)

Just now I noted that the MinGW Toolchain that comes with the default Qt installation, at least Qt 5.14, comes with a lib directory with libQt5*.a files. Are those files static libraries?
I think so because:
They have a size similar to the ones that I get when I compile Qt statically for release.
$file ./libQt5Core.a outputs ./libQt5Core.a: current ar archive, which is the same that it outputs for the statically compiled ones.
If indeed they're static libraries, how can I tell QMake (for example editing the .pro file) to link to those instead of linking to the shared ones?
Are those files static libraries?
No. They are not static. Qt’s default online installer provides only shared libraries. That *.a files are so-called import libraries.
Import library is an .a or .lib library, but it only contains bit of information needed to tell the linker/OS how your program interacts with the dll’s.
If you need Qt static windows build for some reasons, you have some options:
HARD Build whole Qt (or needed modules) by yourself.
EASY Use vcpkg: vcpkg install qt5:x64-windows-static
With vcpkg you can create custom MinGW triplet if you need MinGW for some reason. But I suggest you stick with MSVC.

Adding static library link line to my Qt project file makes no difference

I've read a lot of "duplicate" posts on the subject of statically linking your Qt project on windows to statically built Qt core libs. However, I was left very confused when I compared it to what I am seeing. What I gathered from all the posts is if you already have the statically built Qt libs (which I seem to have here C:\Qt\5.4\mingw491_32\lib) all you need to do is to include a line like so
LIBS += -LC:/Qt/5.4/mingw491_32/lib -lQt5Core -lQtGui
in your .pro file without having to specify -static anywhere. However, this line seems to do exactly ZERO. When I comment it out my project builds exactly the same way without it. The exe is the same size and when I try to run it from command prompt a runtime error appears in a dialog box saying it can't find Qt5Core.dll. Its clearly still linking dynamically. What am I doing wrong? Here is the whole .pro file.
#QT +=
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = testproj
TEMPLATE = app
SOURCES += main.cpp mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
QMAKE_CXXFLAGS +=-std=c++11
#LIBS += -LC:/Qt/5.4/mingw491_32/lib -lQt5Core
I clearly have major holes in my understanding of how Qt linking works on windows. Any help on this topic is highly appreciated.
Qt does not distribute static builds.
It seems you only have a dynamic build and got confused by the .lib/.a files. On Windows, there are 3 types of library:
Dynamic libraries (.dll)
Static libraries (.a or .lib depending on the toolchain)
Import libraries (.a or .lib depending on the toolchain)
Import libraries are only used with dynamic libraries. The import libraries are used by the linker when generating the exe, while the dynamic libraries are used at run time.
So if you have Qt5Core.lib or Qt5Core.a does not mean you have a static build of Qt.
To build your own static version of Qt, I suggest your read Building a static Qt for Windows using MinGW on Qt's wiki.
The major hint is: no matter how Qt is built, you shouldn't need to change anything in the project that uses Qt. Qmake and the configuration files in a given Qt install govern how the applications that use Qt are linked against Qt.
Another hint: a given Qt installation can be either configured for static or dynamic linking, not both. Thus any sort of configuration on the project end is superfluous: a given Qt install already "knows" how to link Qt correctly.
Any given .pro file should work whether Qt is linked statically or dynamically. You should not have any static-linking-specific entries. Remove the LIBS line from the project.
If your executable is dynamically linked, the only reason is that you're using a dynamically linked build of Qt. You should make a static build yourself first, and then use it with your project, and your executable will be statically linked against Qt. It's that simple.
Qt Creator doesn't care about these details either. All it does is invoke qmake and then the make tool. But each qmake is specific to a particular installation of Qt, and that qmake is configured to access that installation's configuration files, and those files contain the data needed to select the proper linking etc. So all of the information is specific to a particular kit one is using, and specifically to the Qt "version" that kit is using (really, a Qt installation because there can be multiple installs of the same version, configured differently).

How to use static library in Qt 5.2?

I downloaded qt recently.
I want to create an standalone exe,
but I don't know what can I do.
In Qt5.2, the folder "(Qt)\lib" is exists, but i can't use it in Qt Creator.
So, should I build from source,
or configure Qt Creator and use the "lib***.a"?
Any ideas?
Environment: MinGW-32 4.8.1(TDM) / Windows 7
You seem to have tried CONFIG+=static, but that is not meant for this use case. That is used when you would like to use build your library to be static after the end of the build.
This is not the case here because you already have static Qt libraries available, so what you wish instead, to link those statically against your executable.
You would need to use this in your qmake project file:
LIBS += -L/path/to/the/static/QtCore -lQtCore
You could also use, albeit this would make the build-system less portable across different platforms:
LIBS += /path/to/the/statis/QtCore/libQtCore.a

Qt 3rd Party Library Static Linking (QtSerialPort)

Turns out static linking was working, but only for Qt libraries. My 3rd party library QtSerialPort is not linking statically. After some reasearch, I've found that I either have to build this library statically or I have to link directly to a .pri file in my .pro file.
I'm not sure how to do either since it seems QtSerialPort has not been designed for static linking.
The .pri method I really don't understand and has been vaguely described in these two links:
http://qt-project.org/forums/viewthread/15223
http://www.qtcentre.org/archive/index.php/t-54505.html
Does anyone have any adivce on how to get either of these methods to work? Or possibly another method?
Also, MSVCP100.dll is not linking statically if anyone could give me any advice on that.
==================================================================================
I am trying to get Qt to statically link libraries so that I can make a standalone application. I have followed various tutorials on how to build Qt statically then building a static application but I am not having much luck. I believe I have succesfully built Qt with static linking because the application has grown in size from 79KB to 7+MB but I am still getting errors saying QtCore4.dll and QtSerialPort.dll are missing. Also, another issue I'm having when using this static configuration, which isn't too serious, is that when I close my program Windows thinks it has crashed and gives me a window saying MyProgram.exe has stopped working...
I am on a Windows machine using MSVC 2010 with Qt 4.8.5 and am using the third party library QtSerialPort.
What I've done accoring to the guides I've been reading is:
Download and extract qt-everywhere-opensource-src-4.8.5.zip
Open /mkspec/mwin32-msvc2010/qmake.conf and change the follwing lines to
CONFIG += qt warn_on release incremental flat link_prl precompile_header autogen_precompile_source copy_dir_files debug_and_release debug_and_release_target
and
QMAKE_CLFAGS_RELEASE = -O2 -MT
I then open the MSVC2010 command prompt and cd to this . I then enter the commands
configure -static -release -platform win32-msvc2010
nmake sub-src
After this is done I open my project and add
CONFIG += static
to the .pro file. In QtCreator I then go into Projects, Manage Kits then to Qt Versions and browse to the qMake I just generated. I add a new Kit with this version of qMake. I then clean all and switch to this new kit and run qmake from QtCreator. I then use msvc2010 command prompt to go to the directory where the files are generated and then
nmake release
This generates a rather large .exe but like I said, it's still depending on a couple .dll's.
For static linking of external library one have a couple options, both have their pros and cons.
I. Compile the library for static linking yourself. Link to it.
Look for possible existing configuration switches for static linking. There can be something like QTSERIALPORT_STATIC = no, etc. in the library's .pro/.pri files. Just say yes for the library to compile for static linking and go to the step 4!
In .pro/.pri file replace CONFIG += dll with CONFIG += static.
Remove export declarations from the library. Typically Qt library symbols are declared with some definition like QTSERIALPORT_EXPORT which expands to Q_DECL_EXPORT/Q_DECL_IMPORT in shared library build / its header files usage when linking. You'll need to find where this QTSERIALPORT_EXPORT is defined and replace it with empty definition:
#define QTSERIALPORT_EXPORT // in source file
or
DEFINES += QTSERIALPORT_EXPORT # in .pro/.pri file
Build the library.
Link to the library .lib/.a file, use the library header files for symbol declarations in your project.
II. Include the library source files into your project and compile them within it (no linking at all).
Include all the source files of the library into your project (add to SOURCES in qmake project file)
Determine all the stuff the library depends on (other libraries, Qt options, etc.) and include it also into your .pro file.
OR
Include the proper .pri file into your .pro if the library author provides it for in-project compilation (i.e. include(QtSerialPort.pri) or something.)
Remove export/import declarations from the library source code — as described in the item 3 of part I.
Build your project.

How to build Qt 5 statically

I worked with Qt Creator 2.6.2 based on Qt 5.0.1 in a linux environnement. The application works fine on the computer where I made the compilation but when I execute it on another computer I got errors like :
error while loading shared libraries: libQt5WebKitWidgets.so.5: cannot open shared object file: No such file or directory
error while loading shared libraries: libxslt.so.1: cannot open shared object file: No such file or directory
I found some solutions in this link (some links are dead).
So I added to my .pro file this line :
CONFIG += static
To compile my project statically.
I thought the file I'll get will be larger but I got the same size and the same errors.
Thank you.
The Qt shared libraries don't exist on the other computer you tested it on. So you need to either:
Copy the shared libraries to your other machine. Or...
Create a static Qt build to link with your application.
It's not sufficient to just add CONFIG += static to your .pro file, you also need Qt static libraries. So to do #2 you'll need to get the Qt source code and build it yourself.
Also, Qt is licensed under the LGPL so you'll need to be aware of that when static linking. There are some who believe that the LGPL does not allow static linking (unless you LGPL your own code) and others who believe that it does (so long as you're willing to release the object code for your app). But that's a whole other discussion.