VS2010使用靜態編譯的qt庫

       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、雖然鏈接錯誤解決了,但運行仍會報錯

      

解決這個問題,需要花費一些時間了,下面就直接提供最終解決方案了。

對於只引用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

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章