Qt 5引入了一种新的编写方式。

Qt开发界面很方便,但发布程序就不那么方便了,你的把引用到的dll一起发布才行,要是能静态编译就好了,发布的时候只有一个exe多方便。

虽然以前为了方便,直接安装的qt-windows-opensource-5.0.2-msvc2010_32-x86-offline.exe, 省去了自己编译这一步,但官方提供的库是动态编译的,是以lib + dll形式存在的,所以没法在我的程序中静态编译。

为了能够得到静态编译的效果,毅然选择自己编译源码,生成静态库,下面是我编译静态库的全部过程。

1、下载qt源码,5.0.2 qt-everywhere-opensource-src-5.0.2.zip 
       2、解压到F:\qt-src-5.0.2 
       3、查看readme文件,其中有提到,需要下载3个安装工具 
   Windows: 
   --------

Open a Windows SDK (7.0, 7.1 or later) command prompt. Ensure that the 
     following tools can be found in the path: 
     * Perl version 5.12 or later   [http://www.activestate.com/activeperl/] 
     * Python version 2.7 or later  [http://www.activestate.com/activepython/] 
     * Ruby version 1.9.3 or later  [http://rubyinstaller.org/]

你可以选择下面地址直接下载: 
     1. Perl 5.8 or later 
        x86  http://www.activestate.com/activeperl/downloads/thank-you?dl=http://downloads.activestate.com/ActivePerl/releases/5.16.3.1603/ActivePerl-5.16.3.1603-MSWin32-x86-296746.msi   
       amd64  http://www.activestate.com/activeperl/downloads/thank-you?dl=http://downloads.activestate.com/ActivePerl/releases/5.16.3.1603/ActivePerl-5.16.3.1603-MSWin32-x64-296746.msi   
       2. Python 2.7 or later 
       x86  http://www.activestate.com/activepython/downloads/thank-you?dl=http://downloads.activestate.com/ActivePython/releases/2.7.2.5/ActivePython-2.7.2.5-win32-x86.msi   
       amd64  http://www.activestate.com/activepython/downloads/thank-you?dl=http://downloads.activestate.com/ActivePython/releases/2.7.2.5/ActivePython-2.7.2.5-win64-x64.msi 
      3. Ruby 
              amd64  http://rubyforge.org/frs/download.php/76806/rubyinstaller-2.0.0-p0-x64.exe

4、进入Open Visual Studio Command Prompt (2010), cd 到源文件目录下,执行以下命令

F:\qt-src-5.0.1 > configure -prefix F:\qt-static -debug-and-release -static -platform win32-msvc2010 -no-c++11  -no-icu -opengl desktop -qt-sql-sqlite -qt-zlib -qt-style-windowsvista -qt-libpng -qt-libjpeg -nomake demos -nomake examples -nomake tests -mp

5、执行完后,直接运行nmake(漫长的等待中)

6、打开VS2010, Qt5--> Qt Options    添加版本

static5.0.2      F:\qt-src-5.0.2\qtbase

7、创建qt工程后,Qt-->Qt Project Settings, 选择静态库版本:static5.0.2

8、首先我们在Debug模式下运行,会有以下链接错误 
      Qt5Cored.lib(qeventdispatcher_win.obj) : error LNK2019: unresolved external symbol _WSAAsyncSelect@16 referenced in function "public: void __thiscall            QEventDispatcherWin32Private::doWsaAsyncSelect(int)" ( ?doWsaAsyncSelect@QEventDispatcherWin32Private@@QAEXH@Z )

这个链接容易解决,属性页-->Link-->input  Additional Dependencies 加入Ws2_32.lib, 然后运行

9、虽然链接错误解决了,但运行仍会报错

报的错误就是这章的错误:http://www.cnblogs.com/foohack/p/3909320.html

解决这个问题,需要花费一些时间了,下面就直接提供最终解决方案了。

对于只引用qt默认的三个库的程序,依赖lib列表如下: 
imm32.lib 
winmm.lib 
Ws2_32.lib 
qtmaind.lib 
Qt5Cored.lib 
Qt5Guid.lib 
Qt5Widgetsd.lib 
opengl32.lib 
Qt5PlatformSupportd.lib 
qwindowsd.lib

注意Debug模式静态库名字后面都是带d的,另外qwindows.lib单独在一个文件中,需要在属性页-->Link-->General Additional Library Directories加入lib的目录, 
F:\qt-src-5.0.2\qtbase\plugins\platforms

10、以上lib加好后,还有关键一步,在main函数文件中加入以下两行代码 
     #include <QtPlugin> 
    Q_IMPORT_PLUGIN (QWindowsIntegrationPlugin);

11、运行程序,OK程序可以跑起来了, 查看以下程序,发布版exe接近7M(默认界面)

12、以上流程只是实现了qt库的静态编译,如果你想你的程序独立于编译器的话,还得设置Runtime Library  为/MT 或  /MTd

最新文章

  1. sublime text 3 快捷键大全以及配置编译环境
  2. ASP.NET整理:Cookie,Application,Session,页面生命周期
  3. 用Wireshark抓包分析超过70秒的请求
  4. Android 第三方
  5. MySql避免重复插入记录方法(ignore,Replace,ON DUPLICATE KEY UPDATE)
  6. Linux发展史
  7. 轻量级开源内存数据库SQLite性能测试
  8. linux poll
  9. 使用gdb调试多线程程序总结
  10. php安全编程—sql注入攻击
  11. 关于Mysql的索引
  12. 智能指针之 unique_ptr
  13. 关于TabLayout与ViewPager在Fragment中嵌套Fragment使用或配合使用的思考
  14. 【python】flask+nginx配置
  15. eclipse jdk版本设置
  16. Cpu表现出正弦曲线
  17. 【紫书】 The Falling Leaves UVA - 699 递归得简单
  18. Ubuntu C/C++开发环境的安装和配置
  19. 对WebView进行的一些设置
  20. thinkphp5.0安装

热门文章

  1. 计算机语言学习导论[C/C++]
  2. android中button点击频率控制
  3. jquery+ajax分页
  4. windows oid 利用SNMP获得主机信息(转)
  5. Web 前端利器Emmet 的HTML用法总结
  6. eclipse 中 maven3 创建web项目
  7. [Javascript] Task queue &amp; Event loop.
  8. [Redux] Passing the Store Down Explicitly via Props
  9. 解决Fetching android sdk component information加载过久问题
  10. kvm 启动libvirtd市出现错误