Saturday, March 5, 2011

Statically building with Qt Creator on Linux

So, you have developed a nice app using Qt library with Qt Creator and now you want to share it (or deliver it to a client), but you don't want to make them install the whole Qt library in their computer?

What you need is to put everything into a single executable file, into other words, you need to link statically the Qt library. But how to do this the default one is shared? - You have to recompile it (don't panic now, it's easy), but statically this time, so thus Qt Creator will be able to link you program statically during compile (or to be more precise, link) time.

Don't worry, it ain't rocket science, in a few steps you will able to happily build your first single file applications =D.

First Step - Download the most recent Qt library source code by clicking on this link.

Second Step - Unpack it to an easy-to-find location in your user folder. Eg. /home/myUser/qtLibrary

Third Step - Create a new folder where the statical library will be stored. Eg. /home/myUser/qtStaticLib

Fourth Step - Open up a terminal window. Type:
cd /home/MyUser/qtLibrary (enter key)
./configure -prefix /home/MyUser/QtLibs/qtStaticLib -largefile -static -qt-libjpeg -qt-libpng -qt-libmng -qt-zlib -debug-and-release -phonon -phonon-backend -svg -dbus -script -qt-sql-mysql -multimedia -audio-backend -opengl -stl -glib -xrender -xshape -xcursor -xinput -xrandr -xfixes -xsync -xkb -xmlpatterns -fontconfig -glib -mitshm -sm -pch -nis -cups -gtkstyle -nomake demos -nomake examples -nomake tools (enter key)

Probably this step WILL fail, and messages like Phonon support cannot be enabled due to functionality tests! will be shown.

This is because you don't have all the libraries which are necessary to build Qt. For the example given above, you will need libgstreamer0.10-dev. Just open your package manager (Synaptic if you're using Ubuntu), search and download it. Run step four again.

Here's a list of the libraries that were missing for me:
libgstreamer0.10-dev, libgstreamer-plugins-base0.10-dev, libxext-dev, libxrender-dev, libmysql++-dev, libxcursor-dev, libxfixes-dev, libxi-dev, libxrandr-dev, libgtk2.0-dev, freeglut3-dev, libasound2-dev, libxv-dev, libdbus-1-dev, libcups2-dev.

If ./configure passes without any complaints, proceed to the next step.

Fifth Step - Type in terminal:
make -j 2 (enter key) PS. -j 2 tells gcc to use two threads to compile, so if you have a CPU with two cores it will use both. Replace the number with the number of cores that you have. Now sit down and relax, it will take a while.

Sixth Step - Congratulations! You have successfully compiled Qt library statically! Now you must make Qt Creator use it instead of the shared standard version.
- Close that terminal that you opened before.
- Open your Qt Creator.
- Go to menu Tools -> Options -> Qt 4 (tab).
- Click on the plus signal button and then browse the qmake executable inside /home/myUser/qtStaticLib.
- Name it as you like, I typed Qt Statical.
- Click the Ok button.


Seventh Step - Nice! Your Qt Creator knows about the existence of another library. Finally all that is left is to make your project use it instead of the standard one.
- Open your project.
- Click on the Projects button on the left.
- Change Qt Version to Qt Statical.


- Finally, add the following to your .pro file:
CONFIG += static.


 - Recompile your project. Check out how the executable file size increased. DONE!

13 comments:

  1. Due to my requests...
    No more new posts?

    ReplyDelete
  2. Great tutorial, thanks!

    ReplyDelete
  3. Hi, I'm using Qt 4.82 on Debian Wheezy. I found the relevant libs in /usr/lib/i386-linux-gnu but there's no configure in the directory. Could there be another location for the Qt libraries?

    Thanks.

    ReplyDelete
    Replies
    1. The missing libraries that I mention in the article refer to 3rd party ones, they should be installed in your system. I suggest you to use the package manager that comes with your distro in order to download and install them. The "configure" script is located right in root of the directory where you unzip the Qt library source. Let me know if this solved your issue.

      Delete
  4. Hello
    Thank you 4 your tutorial... it seems i finally found a easy one for hobby-coders like me
    Can you still help me with step one? :D

    Here's what i get

    ./configure -prefix /media/daten/qt5_staticlib/qt5_staticlib -largefile -static -qt-libjpeg -qt-libpng -qt-libmng -qt-zlib -debug-and-release -phonon -phonon-backend -svg -dbus -script -qt-sql-mysql -multimedia -audio-backend -opengl -stl -glib -xrender -xshape -xcursor -xinput -xrandr -xfixes -xsync -xkb -xmlpatterns -fontconfig -glib -mitshm -sm -pch -nis -cups -gtkstyle -nomake demos -nomake examples -nomake tools
    + cd qtbase
    + /media/daten/qt5_staticlib/qt-everywhere-opensource-src-5.3.1/qtbase/configure -top-level -prefix /media/daten/qt5_staticlib/qt5_staticlib -largefile -static -qt-libjpeg -qt-libpng -qt-libmng -qt-zlib -debug-and-release -phonon -phonon-backend -svg -dbus -script -qt-sql-mysql -multimedia -audio-backend -opengl -stl -glib -xrender -xshape -xcursor -xinput -xrandr -xfixes -xsync -xkb -xmlpatterns -fontconfig -glib -mitshm -sm -pch -nis -cups -gtkstyle -nomake demos -nomake examples -nomake tools
    -qt-libmng: invalid command-line switch
    -phonon: invalid command-line switch
    -phonon-backend: invalid command-line switch
    -svg: invalid command-line switch
    -script: invalid command-line switch
    -multimedia: invalid command-line switch
    -stl: invalid command-line switch
    -xmlpatterns: invalid command-line switch
    Unknown part demos passed to -nomake.


    Is it because of the renewings in QT5? Can i just skip the command-line switches, which its not recognizing?

    Thank you a lot

    ReplyDelete
    Replies
    1. You're right, for QT5 is different. Actually, it is much simpler than this.
      I'm not at home right now, but as soon as I get there I'll try to recompile QT5. Already did it once, just can't remember exactly how right now.

      Delete
  5. I followed these instructions but no text was displayed on the application window. I am running qt 5.6

    ReplyDelete
    Replies
    1. Hello Anonymous.
      This post refers to a very old Qt version, much before 5.6 (latest currently).
      The steps described above are no longer valid for 5>.

      Delete
  6. Hey P. Naschpitz. Would be nice if you can post the easy install instructions for qt 5.6 . I think manny people would like to know how to do it. maybee you also can post your linux version.

    ReplyDelete
    Replies
    1. I would like to know how to do a static building with Qt 5.7.. Thanks in advance!

      Delete
  7. Dear Mr. Naschpitz, you're a so handsome programmer.... Why you keeping hiding yourself like this? Get out of the closet you pourpurinated queer programmer!

    ReplyDelete