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:

missing_dll

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:

    MSVC -- Qt Options menu

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

    MSVC++ Qt setup menu.

    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:

    qtcreator

     

    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.

    另外:

    首先使用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()'等错误


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值