問題描述
我知道這可能是一個與許多其他問題類似的問題,但在搜索了很多次但未能得到明確有效的解決方案后,我不得不提出這個問題.
I know it might be a question similar to many others, but after searching for many times and failing to get a definitive and effective solution I'm having to ask this question.
我正在使用 Qt 5.2.0 for Windows 32-bit (VS 2010, 570 MB),我已經完成了我的編程,一切都完成了.但現在我想將它作為 .exe 文件分發給我的同事,但這樣做不會很復雜,也為了避免分發 dll 文件,我需要使用靜態鏈接構建程序.
I'm using Qt 5.2.0 for Windows 32-bit (VS 2010, 570 MB), and I have already made my programming, and it's all done. But now I want to distribute it as .exe file to my colleagues, but to do so without complication and to avoid having to distribute dll files I need to build the program using static linking.
能否請您描述一下我如何制作 Qt 5.2.0 for Windows 32 位(VS 2010,570 MB) 使用靜態鏈接構建整個程序?
Could you please describe how I can make Qt 5.2.0 for Windows 32-bit (VS 2010, 570 MB) build the whole program using static linking?
謝謝.
推薦答案
你可以在 qmake 中使用 CONFIG
變量:
You can use the CONFIG
variable for this with qmake:
CONFIG += static
或
CONFIG += staticlib
但是,您需要確保您擁有所有要捆綁的庫,以靜態方式提供.
However, you will need to make sure that you have all the libraries that you wish to bundle up, available as static.
如果您遵守許可,這也包括 Qt 框架本身.官方安裝只設置動態庫(.dll文件),所以需要自己構建Qt來完成.
This includes the Qt framework itself as well if you comply with the license to do so. The official installation only sets up dynamic libraries (.dll files), so you would need to build Qt on your own to accomplish this.
您可以使用以下命令為您自己的目的靜態構建 Qt
:
You could use the following commands to build Qt
statically for your own purpose:
configure -developer-build -opensource -nomake examples -nomake tests -static
qmake -r
nmake
請注意,通常在構建像您這樣的第三方 Qt 軟件時,最好使用以下參數調用 qmake
以正確傳遞您的環境:
Note that in general when building third-party Qt softwares like yours, you better invoke qmake
with the following parameter to pass your environment properly:
qmake -r -spec win32-msvc2010
還請注意,正如 Frank 和 ManuelH 在評論中所寫的那樣,如果您的應用程序未獲得 LGPL 的免費許可或至少與 LGPL 兼容,并且您也未使用 Qt 的商業許可,則不允許靜態鏈接.最好在采取您的方法之前確定這一點.
Please also noted that as Frank and ManuelH wrote in the comment, static linkage is not allowed if your application is not free licensed either a LGPL or at least compatible to LGPL, nor do you use commercial license for Qt. It is better to make sure about this before picking up your approach.
完成后,您可以以常規方式使用 LIBS
變量,例如:將靜態庫的路徑與庫名稱一起傳遞給它,如下所示:
Once that is done, you can use the LIBS
variable in the regular way, as in: pass the path of your static library to it along with the library name, so something like this:
LIBS += -L/path/to/the/static/library -lstaticlibraryname
請注意,傳遞給 -l
參數的靜態庫名稱不應包含靜態庫擴展名,例如 Windows 上的 .lib
.
Note that the static library name passed to the -l
parameter should not contain the static library extension, for instance .lib
on Windows.
作為后備,您始終可以靜態鏈接其他庫,并將 Qt dll 文件放在可執行文件旁邊,然后將該文件夾部署為包".這對您來說可能是更簡單的方法.
As a fallback, you can always link other libraries statically, and put the Qt dll files beside the executable, and you deploy the folder as a "package". That is probably the easier way for you to go.
這篇關于如何讓 Qt 和 Qtcreator 靜態鏈接庫而不是動態鏈接?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!