從網上找了很多的介紹,終于完成了編譯,作個總結
1)去 releases.qt-project.org/qt4/source/ 下載下傳并安裝 qt-win-opensource-4.8.1-mingw.exe,安裝的時候注意指定mingw所在目錄。如果沒有安裝mingw,先下載下傳并解壓mingw
2) 編輯 C:\Qt\4.8.1\mkspecs\win32-g++\qmake.conf 檔案 把 QMAKE_LFLAGS =
改為:QMAKE_LFLAGS = -static
然後将下面一行:
QMAKE_LFLAGS_DLL = -shared
修改為:
QMAKE_LFLAGS_DLL = -static
Edit <QTDIR>\qmake\Makefile.win32* files:
Change
LFLAGS =
to (add -static-libgcc)
LFLAGS = -static-libgcc
3)添加mysql支援
先根據 hi.baidu.com/llydmissile/item/b484c88a81adecded1f8cd3f 文章的描述獲得mysql的頭檔案和 libmysql.a & libmysql.def。 然後需要告訴編譯器mysql的檔案在什麼地方,
編輯C:\Qt\4.8.1\src\sql\Makefile.Release 檔案,
INCPATH =………… 後面附加 -I"C:\MYSQL\INCLUDE"
LIB = …………後面附加 -LC:\MYSQL\LIB\ -lmysql
儲存.
然後把C:\MySQL\lib 目錄下的libmysql.a, libmysql.def, libmysql.dll, libmysql.lib等檔案複制到C:\Qt\4.8.1\lib 目錄下
4)首先確定perl已經安裝
打開qt4.8.1環境的指令提示符,預設進入C:\Qt\4.8.1目錄。運作
set PATH=%path%;C:\MySQL\lib;C:\Perl\site\bin;C:\Perl\bin;
((((((don't use this)))))configure -debug-and-release -static -fast
configure -static -release -no-exceptions -qt-sql-mysql
然後 mingw32-make sub-src
如果編譯中發現配置有錯造成編譯停止,使用mingw32-make confclean删除配置,再重新運作configure
如果不是配置造成的錯誤,修正錯誤後再次運作mingw32-make sub-src即可,編譯器會回到上次出錯處繼續編譯
5)
應用程式編譯時,也需要在.pro檔案中加入以下代碼:
static { # everything below takes effect with CONFIG += static
CONFIG += static
CONFIG += staticlib # this is needed if you create a static library, not a static executable
DEFINES += STATIC message("~~~ static build ~~~") # this is for information, that the static build is done
mac: TARGET = $$join(TARGET,,,_static) #this adds an _static in the end, so you can seperate static build from non static build
win32: TARGET = $$join(TARGET,,,s) #this adds an s in the end, so you can seperate static build from non static build
}
然後,在Projects中設定Build steps -> qmake 的附加選項 CONFIG+=static。(llyd注:事實證明這一步并不需要)
此處的靜态編譯是針對QT的動态連接配接庫,是以,仍然需要附加不屬于QT部分的dll,例如:基于線程的mingwm10.dll和libgcc_s_dw2-1.dll
ps:.pro中的代碼是否必要未驗證(反正我加上了,可以運作,不知道不加上可不可以...)
llyd注, DEFINES += STATIC message這一行好像不被識别,删掉就行了
6)編譯出的qt版本可以整合到qt sdk 中的qt creator中,工具菜單- 選項... - 左邊的建構和運作 - Qt版
本頁籤,點選添加,定位到C:\qt\4.8.1\bin\qmake.exe,确認即可。版本名稱可以自己随便取
release編譯出的可執行檔案比較大,可以strip一下exe、在upx一下exe和dll,體積會非常小。
GNU 二進制實用程式Binutils (GNU binary utilities)包括:objdump、readelf、addr2line、strip、ar、nm、ldd、ngprof、gcov等。
這個我還沒有測試。