備注:應用程序的編譯器版本+位數必須與靜態(tài)庫的編譯器版本+位數一致,才能編譯通過(guò)??!比如靜態(tài)鏈接庫是在 MSVC2015+32bit 環(huán)境編譯的,那么應用程序的編譯環(huán)境也必須是 MSVC2015+32bit。
在項目的源文件目錄下創(chuàng )建一個(gè) include 子目錄,將庫的頭文件 qwdialogpen.h 、插件的 debug 和 release 兩種模式編譯生成的庫文件 myStaticLibd.lib 和 myStaticLib.lib 復制到此目錄下,項目在編譯鏈接時(shí)需要此頭文件和庫文件。
備注:靜態(tài)庫在debug編譯時(shí),生成的 lib 文件不會(huì )帶 d 結尾,需要手動(dòng)在文件名后加 “d”!
右擊項目,在彈出的菜單中選擇 “添加庫…” ,選擇庫類(lèi)型時(shí),選擇 外部庫“External Library”;選擇庫文件位置時(shí),就把 include 文件夾下的 myStaticLib.lib 選中即可,會(huì )自動(dòng)填充 “Include path”編輯框,選擇 windows 平臺,連接方式選擇 Dynamic(動(dòng)態(tài)鏈接庫,插件是Dynamic)/Static(靜態(tài)鏈接庫) ,勾選下方 Add “d” suffix for debug version,表示在 debug 版本的庫名稱(chēng)后面添加一個(gè)字母 “d”,以便編譯器自動(dòng)區分 release 和 debug 版本的庫文件。
修改項目文件 BatteryUser.pro,添加:
下面的太復雜,直接提供簡(jiǎn)化版:
注意格式為使用格式:LIBS += -L[path to lib] -l[name of lib]
例如:LIBS += -L$$PWD/include/ -llibNetApi,其中$$PWD表示當前./,libNetApi為靜態(tài)庫的名稱(chēng),注意不要加上.a或者.lib這樣的后綴。
上面是引入靜態(tài)庫,還需要引入頭文件,格式如下:INCLUDEPATH += $$PWD/include,其中include為引入的頭文件在項目中的存儲目錄
#設置添加的庫文件,會(huì )判斷當前項目時(shí)以 debug 還是 release 模式編譯
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/include/ -lmyStaticLib
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/include/ -lmyStaticLibd
#頭文件目錄
INCLUDEPATH += $$PWD/include
#項目依賴(lài)目錄
DEPENDPATH += $$PWD/include
這樣只有MSVC編譯器可以編譯
win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/include/libmyStaticLib.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/include/libmyStaticLibd.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/include/myStaticLib.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/include/myStaticLib.lib
這樣只有 MinGW編譯器可以編譯
#設置添加的庫文件,會(huì )判斷當前項目時(shí)以 debug 還是 release 模式編譯
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/include/ -lmyStaticLib
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/include/ -lmyStaticLibd
#頭文件目錄
INCLUDEPATH += $$PWD/include
#項目依賴(lài)目錄
DEPENDPATH += $$PWD/include
win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/include/libmyStaticLib.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/include/libmyStaticLibd.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/include/myStaticLib.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/include/myStaticLib.lib
這樣 MSVC 和 MinGW編譯器都可以編譯
#include "qwdialogpen.h"
在 debug 和 release 兩種模式下分別編譯。
聯(lián)系客服