Building static Qt 4.x on Windows with MinGW

Qt is the famous framework for develop applications that can work in Windows, Linux and Mac. Just develop your application using the framework API and recompile for the right OS. Very easy. As additional feature it came with an integrated IDE that can speed up the development work. The only thing that, maybe, can result "annoying" for some some people is the standard libraries binary format distributed with the official release.



NOTE: this tutoria apply only to version 4.x of Qt libraries.

In fact, if you download the full Windows SDK or only the libraries binary package, they come only as dynamic library version, no static libraries are provided. Use dynamic libraries compilation have the advantage of generate a very small executable file but, as negative side, you need to provided, in addition of your executable file, all the required Qt dynamic libraries. Some of the main Qt libraries have a very big size and also if you develop a very simple application your final installation package will have a considerable size. Alternative solution is to use Qt in static libraries format but, as already said, this option is not provided in the official package. The only solution we have is to manually recompile the code for generate the static libraries we need.
Here is reported a small tutorial for make this operation. There are several solution for obtain the same result. Basically it depends by the IDE you want to use for the developing (Qt Creator or Visual Studio for make an example). Personally I chose to use the Qt Creator and the provided MinGW compiler, than this tutorial will be based to reach the result using these tools. Different compiler or IDE need different actions to those set out here.

So, let's go to proceed. As first step you need to download the Qt Creator (download link here). There are two packages available. The full SDK containing the IDE, the libraries in binary format and some additional development tool (like the simulator for Symbian OS and so on). In alternative there is the standalone IDE without any other additions. If you don't need the additional development tools I strongly suggest to download the standalone IDE since the package is smallest that the first one and contain only the required executables for develop standard applications. Once finished download install it. There is no particular note here, the only thing you must pay attention is to ensure the MinGW compiler flag is set. This is required to install the compiler together with the IDE. We'll choose as installation path "C:\Qt" folder.



As second step you need to download the zip archive containing the full Qt source codes. Once finished extract all the content of the archive in the same path where you installed the Qt Creator (following our example you'll extract the archive content inside "C.\Qt" folder). If you followed the instruction correctly now you should have two folders inside the main installation path (the X.X.X mean the version number of the Qt Creator and Qt libraries you downloaded. For example 2.3.1):

    C:\Qt\qtcreator-x.x.x
    C:\Qt\qt-everywhere-opensource-src-x.x.x

OK, now we need to make few changes in  some files inside sources folder. Using Notepad.exe open the following file:

    C:\Qt\qt-everywhere-opensource-src-x.x.x\mkspecs\win32-g++\qmake.conf

Search for the following line:

    QMAKE_LFLAGS  =

and change to (add -static -static-libgcc):

    QMAKE_LFLAGS  = -static -static-libgcc

Save and close. Now open and modify the following three files:

    C:\Qt\qt-everywhere-opensource-src-x.x.x\qmake\Makefile.win32   
    C:\Qt\qt-everywhere-opensource-src-x.x.x\qmake\Makefile.win32-g++
    C:\Qt\qt-everywhere-opensource-src-x.x.x\qmake\Makefile.win32-g++-sh

Same operation of above. Search for the following line:

    LFLAGS  = [other-params...]

and add the param -static-libgcc if is not already present (is possible that in the last two files of the list this param is already present). So the line will become:

    LFLAGS  = -static-libgcc [other-params...]

Close and save. Now open a command prompt window by executing the cmd.exe tool. We are going to use this command prompt for compile the source codes just extracted but, before, we need to set the path of the MinGW compiler in the environment variables. This instruct the OS to find the executable compiler automatically. Proceed with this step by typing the following command in the prompt and press Enter and the end:

set PATH=%PATH%;C:\Qt\qtcreator-X.X.X\mingw\bin

Follow a screenshot of the operation:


Remember to always substitute the X.X.X with the version number of the Qt Creator you installed (basically is the name of the installation folder). Now the windows prompt is correctly configured for compile the sources. Now let's go to the source folder by typing:

cd C:\Qt\qt-everywhere-opensource-src-X.X.X

Same rule for the X.X.X as explained above. Here we need to configure the makefiles for generate a static version of the libraries. This can be easily obtained by typing:

configure -static -release -no-exceptions [other params...]

The first three params are the basic settings for obtain static version of the libraries. However is possible to set a large number of additional parameters based to the features you need to have included in the libraries. For have a complete list of all the params available for your system type configure --help. If you configured correctly the path of the MinGW compiler, after launched the instructions, you should start to see the procedure preparing all the makefiles to generate the libraries. It will take some time. At the end just type:

mingw32-make sub-src

for start the real compilation process. Here you can go to have some coffee or other relaxing activities since the full compilation of all libraries take a very long time. Be patient. Anyway the end will come and at this point you need only to make the last step, configure Qt Creator for use your new libraries just generated.

Open Qt Creator. Select the menu item "Tools => Options..." and in the option window select Qt4 item in the list control on the left.


Now click on the Add button. This will ask for the position of the qmake.exe program. Browse thought your Qt source folder until reach the /bin subfolder. The program is there. Following our example paths the file should be in:

C:\Qt\qt-everywhere-opensource-src-X.X.X\bin\qmake.exe

Click on OK button and the result should appear similar to the image above. Now is really finished and you can compile your application using static libraries instead dynamics one.

Before leave just a couple of suggestions. Remember to don't move or rename the Qt source folder where you compiled the libraries since during configuration and compilation phases some information regarding the folder path are saved in a file. Altering the folder path or name could cause to show an error in Qt Creator similar to the following:

The Qt version is invalid: Qt version is not properly installed, please run make install

and, obviously, the compilation of your Qt project will not work anymore. If you need to make a rename or move action you need to clean the current configuration and repeat the compilation steps after the change. You can clean the current configuration with the command:

mingw32-make distclean

If you want to generate also the debug version of static libraries you only need to use the "-debug" configuration option instead of "-released" as used in this example.

Comments

  1. This is excellent! Thank you.

    ReplyDelete
  2. This was surprisingly helpful! I was having issues with moc.exe complaining about not finding InterlockedCompareExchange@12. After modifying the LFLAGS things are working like a charm (so far.. still building). Thanks!

    ReplyDelete
  3. Hey man, good tutorial, one thing. I'm following your tutorial with Qt 5.1.0 and QtCretor 2.8.0 that does not have minGW inside, could you update your tutorial for that, or tell me what can I do with QtCreator 2.8.0? Thanks.

    ReplyDelete
  4. Mingw is a free compiler, you can download from here:

    http://www.mingw.org

    and manually add. I suppose it should work... (not tried yet)

    ReplyDelete
  5. Hello again, thanks for your answer, I did the compilation process, and works fine, but when I'm trying to compile my program, I'm getting this error: cannot find lqtmaind
    cannot find -lQGuid. I don't know why qt can't find this modules.

    ReplyDelete
  6. Now works, I just forgot to select "release" on qt-creator project configuration. Thanks a lot.

    ReplyDelete
  7. ...but qt-everywhere-opensource-src-5.1.1 doesn't contain
    C:\Qt\qt-everywhere-opensource-src-x.x.x\qmake\Makefile.win32-g++
    or:
    C:\Qt\qt-everywhere-opensource-src-x.x.x\qmake\Makefile.win32-g++-sh

    Any thoughts?

    ReplyDelete
  8. Hi

    You right. After your comment I changed the title by specifing the tutorial apply only to 4.x version of Qt. Reading some other comments above it seem someone has successfull applied part of the info here for make same static libraries with Qt 5. However I didn't try by myself right now than I can not help you. Sorry.

    ReplyDelete
  9. It's great ,success for Qt 4.8.5 MinGw

    ReplyDelete

Post a Comment

Popular posts from this blog

Access GPIO from Linux user space

Android: adb push and read-only file system error

Tree in SQL database: The Nested Set Model