您的位置:首页 > 编程语言 > Qt开发

How to statically link Qt 4

2015-01-30 11:40 302 查看
原文地址:http://www.formortals.com/how-to-statically-link-qt-4/

————————————————————————————————

How to statically link Qt 4

September 4, 2009
Charles N. Burns
25 Comments

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


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 .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:



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

Complete your Qt project using normal debug libraries.
Compile the Qt libraries for static linking (needs to be done only once).
Add the necessary lines of code to include any Qt plugins you may need.
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:

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:



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



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:



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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: