How to statically link Qt 4.6.0 (如何静态编译发布Qt应用程序)
2013-06-06 15:42
786 查看
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.
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:
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()'等错误
相关文章推荐
- 如何静态编译发布Qt应用程序
- How to statically link Qt 4
- 为何以及如何使用Netlink Socket ( Why and How to Use Netlink Socket)
- 如何转换非发布网站为发布网站(how to convert nonpublishing sites to publishing sites)
- iPhone How-to:如何移除发布版本中的NSLog输出
- 如何发布Qt应用程序
- [HOW TO]如何在iPhone应用程序中发送邮件
- QT18 how to link QListView with sqlite Database values
- QT19 How to link QComboBox with sqlite Database values
- 如何将QT应用程序打包发布
- 如何在Windows下发布QT应用程序?
- How to Secure an Android App 如何保护Android应用程序
- How to Write a simple UEFI EDKII Application:如何编写一个UEFI简单的应用程序[5]
- 如何利用QT在terminal终端执行ROS系统中的roslaunch命令(How to launch a roslaunch in Qt with a terminal (GUI) )
- 如何在QT for S60 中使用pthread库(Carbide.c++) (How to use pthread in QT4.6 for S60)
- 如何在C++中使用VBScript(使用Qt)--How to use VBScript in C++ (Using Qt)
- 为何以及如何使用Netlink Socket ( Why and How to Use Netlink Socket)
- 【Linux】How to Uninstall a Program in Linux(如何卸载Linux系统的应用程序)
- 如何创建软链接 how to create a symbolic link in clearcase
- 为何以及如何使用Netlink Socket ( Why and How to Use Netlink Socket)