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

相關文章
相關標籤/搜索