How to statically link Qt 4.6.0 (如何静态编译发布Qt应用程序)


qtlogoI was downloading Ubuntu Netbook Remix for my EeePC 900A and found that it’s installer is not a CD image, but a raw image meant to be written to a USB FLASH drive. After downloading their handy image writer, I found that this simple utility came with quite a few support files, including a surprising 13.5MB of .dlls.

This image writer tool was written using Nokia’s Qt software, which allows easy development and distribution for all the major platforms from a single C++ source base. This program was compiled in the same way most Windows software is today — with “dynamic linking”, requiring the Qt libraries, among others, be shipped with the program as separate files.

This is fine for large software projects, but it’s a little cumbersome for small tools like this one. Many programs can be distributed such that all necessary files are built into the executable in a process called static linking. Sometimes it is nice to be able to download just the program itself and not have to worry about making sure various DLLs are included (though setting up an installer largely eliminates this problem).

Static linking has its advantages and disadvantages, but when not done, running a program unaccompanied by even one support file will result in a cryptic error for the user:

How to statically link Qt 4.6.0 (如何静态编译发布Qt应用程序)_第1张图片

These 4 steps are all you need to make your entire project result in a single, easy-to-distribute .EXE file:

  1. Complete your Qt project using normal debug libraries.
  2. Compile the Qt libraries for static linking (needs to be done only once).
  3. Add the necessary lines of code to include any Qt plugins you may need.
  4. Compile your release version with the static libraries.

 

Step 1: Complete your Qt project as usual using normal debug libraries.

 

 

I’ll leave this to you. If you aren’t sure how to proceed, start here.

Step 2: Compile the Qt libraries for static linking

While not difficult, this is beyond the scope of the article. Building static Qt is fairly easy, and is covered in this article. It also covers how to shrink Qt (13.5MB is large even without static linking), and how to build Qt with several popular compilers..

Step 3: Add the necessary lines of code to include any Qt plugins you may need.

First, enable the Qt plugins needed by your project.

Only a small number of Qt features are part of a plugin (the rest will work simply by including the appropriate header). There are several commonly used plugins.

Image formats:

  • qgif
  • qjpeg
  • qico (Windows icons)
  • qsvg (Scalable Vector Graphics)
  • qtiff

    Database support:

  • qsqldb2
  • qsqlite (SQLite 3)
  • qsqlmysql
  • qsqloci (Oracle)
  • qsqlodbc
  • qsqlpsql (PostGreSQL)

    Asian language codecs:

  • qcncodecs (Simplified Chinese)
  • qjpcodecs (Japanese)

    qkrcodecs (Korean)

    And finally, qtaccessiblewidgets for support of assistive technologies.

    I’ll use the Oracle plugin (qsqloci) and the GIF image plugin (qgif) in the example below. Regardless of the plugins you need, they will be used in exactly the same manner.

    Edit your project (.pro) file. It should be in the same folder as your source code. It should look something like this:

    TARGET = YourProjectName
    TEMPLATE = app
    SOURCES += main.cpp window.cpp
    HEADERS += window.h

    Add the following lines:

    CONFIG += static
    static { // Everything below takes effect with CONFIG += static
        CONFIG += static
        QTPLUGIN += qsqloci qgif
     DEFINES += STATIC // Equivalent to "#define STATIC" in source code message("Static build.") }

    Finally, edit your main.cpp and add the following to the top:

    #include <QtPlugin>

    Q_IMPORT_PLUGIN(qsqloci)
    Q_IMPORT_PLUGIN(qgif)
    (in both files, replace qsqloci and qgif with your plugins) 
    
         
         
         
         

    Step 4: Compile your release version with the static libraries.

    No extra effort is required to compile your application with static libraries. Configure your project to compile in “release” mode (not debug mode) and then ensure the environment is aware of the location.

    In Microsoft Visual Studio 2008 Professional:

    • Download and install the Visual Studio add-on.
    • Start MSVC++ and open the Qt Options menu:

    How to statically link Qt 4.6.0 (如何静态编译发布Qt应用程序)_第2张图片

    • Name the Qt installation as you like, then add the path.

    How to statically link Qt 4.6.0 (如何静态编译发布Qt应用程序)_第3张图片

    In Qt. Creator the process is similar:

    • Click Tools –> Options, and in “Qt Versions” shown below, click the plus sign icon and add your Qt installation as before:

    How to statically link Qt 4.6.0 (如何静态编译发布Qt应用程序)_第4张图片

     

    That’s it.

    Compile your application normally and notice the (surprisingly large) executable file in the release folder.

    To actually make the Qt libraries usable in static linking, and to make them much smaller than their default distribution, see this article.

    http://www.formortals.com/how-to-statically-link-qt-4/
    另外:

    首先使用confgure -static 编译qt静态库

    然后在main函数前加入程序使用的plugin,否则图片图标显示不正常,文字乱码

    #include <QtPlugin> 
    Q_IMPORT_PLUGIN(qjpeg)
    Q_IMPORT_PLUGIN(qgif)
    Q_IMPORT_PLUGIN(qcncodecs)
    Q_IMPORT_PLUGIN(qico)

    最后在工程 pro文件中加入库

    LIBS += D:/QT/4.6.0/plugins/imageformats/libqgif.a
    LIBS += D:/QT/4.6.0/plugins/imageformats/libqjpeg.a
    LIBS += D:/QT/4.6.0/plugins/codecs/libqcncodecs.a
    LIBS += D:/QT/4.6.0/plugins/imageformats/libqico.a

    否则编译出现

    undefined reference to `qt_plugin_instance_qico()'等错误


  • 你可能感兴趣的:(How to statically link Qt 4.6.0 (如何静态编译发布Qt应用程序))