VS2010使用静态编译的qt库

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

       虽然之前为了方便,直接安装的qt-windows-opensource-5.0.2-msvc2010_32-x86-offline.exe, 省去了本身编译这一步,但官方提供的库是动态编译的,是以lib + dll形式存在的,因此无法在个人程序中静态编译。python

       为了可以获得静态编译的效果,毅然选择本身编译源码,生成静态库,下面是我编译静态库的所有过程。c++

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

     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/]windows

你能够选择下面地址直接下载: 
     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  ruby

     四、进入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工具

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

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

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

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

      八、首先咱们在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, 而后运行

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

      

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

对于只引用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

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

     十一、运行程序,OK程序能够跑起来了, 查看如下程序,发布版exe接近7M(默认界面)

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

相关文章
相关标签/搜索