Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Qt and Qtcreator link the libraries statically instead of dynamic? [duplicate]

Tags:

I know it might be a question similar to many others, but after searching for many times and failing to get a definitive and effective solution I'm having to ask this question.

I'm using Qt 5.2.0 for Windows 32-bit (VS 2010, 570 MB), and I have already made my programming, and it's all done. But now I want to distribute it as .exe file to my colleagues, but to do so without complication and to avoid having to distribute dll files I need to build the program using static linking.

Could you please describe how I can make Qt 5.2.0 for Windows 32-bit (VS 2010, 570 MB) build the whole program using static linking?

Thanks.

like image 519
the_naive Avatar asked Dec 27 '13 10:12

the_naive


People also ask

How do I create a static library in Qt?

alternatively you can right-click your project in Qt Creator and select "Add Library...", choose "External library" and browse for your library file: For libraries compiled with MSCV compiler in windows, you look for . lib or . dll.

Is Qt dynamically linked?

Qt uses dynamic linking by default. You'll notice this immediately during deployment to a non-developer machine, because your code will not run without the Qt libraries. If your concern is the LGPL, just be careful when compiling Qt itself.


1 Answers

You can use the CONFIG variable for this with qmake:

CONFIG += static

or

CONFIG += staticlib

However, you will need to make sure that you have all the libraries that you wish to bundle up, available as static.

This includes the Qt framework itself as well if you comply with the license to do so. The official installation only sets up dynamic libraries (.dll files), so you would need to build Qt on your own to accomplish this.

You could use the following commands to build Qt statically for your own purpose:

configure -developer-build -opensource -nomake examples -nomake tests -static
qmake -r
nmake

Note that in general when building third-party Qt softwares like yours, you better invoke qmake with the following parameter to pass your environment properly:

qmake -r -spec win32-msvc2010 

Please also noted that as Frank and ManuelH wrote in the comment, static linkage is not allowed if your application is not free licensed either a LGPL or at least compatible to LGPL, nor do you use commercial license for Qt. It is better to make sure about this before picking up your approach.

Once that is done, you can use the LIBS variable in the regular way, as in: pass the path of your static library to it along with the library name, so something like this:

LIBS += -L/path/to/the/static/library -lstaticlibraryname

Note that the static library name passed to the -l parameter should not contain the static library extension, for instance .lib on Windows.

As a fallback, you can always link other libraries statically, and put the Qt dll files beside the executable, and you deploy the folder as a "package". That is probably the easier way for you to go.

like image 91
lpapp Avatar answered Oct 07 '22 23:10

lpapp