How to statically link Qt 4

Make your entire Qt application work entirely from a single file.


qtlogo I 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 .dll s .

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. Do not use a static-compiled Qt build for development!

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:

  • qcn codecs (Simplified Chinese)
  • qjp codecs (Japanese)
  • qkr codecs (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 T ools –> O ptions, and in “Qt Versions” shown below, click the plus sign icon and add your Qt installation as before:

    qtcreator

     

     

    Qt Creator note : If  you are using Qt Creator for your projects and have compile errors, deselect “use jom instead of nmake” under Tools –> Options –> Projects.

    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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值