問題描述
我有以下 CMakeLists.txt
文件來基于 Qt 生成我的項目:
I have the following CMakeLists.txt
file to generate my project based on Qt:
cmake_minimum_required(VERSION 2.8.12)
project(MyProject)
find_package(Qt5Widgets)
set(MyProjectLib_src ${PROJECT_SOURCE_DIR}/gui.cpp)
set(MyProjectLib_hdr ${PROJECT_SOURCE_DIR}/gui.h)
set(MyProjectLib_ui ${PROJECT_SOURCE_DIR}/gui.ui)
set(MyProjectBin_src ${PROJECT_SOURCE_DIR}/main.cpp)
qt5_wrap_cpp(MyProjectLib_hdr_moc ${MyProjectLib_hdr})
qt5_wrap_ui (MyProjectLib_ui_moc ${MyProjectLib_ui})
include_directories(${PROJECT_SOURCE_DIR})
include_directories(${PROJECT_BINARY_DIR})
add_library(MyProjectLib SHARED
${MyProjectLib_src}
${MyProjectLib_hdr_moc}
${MyProjectLib_ui_moc}
)
target_link_libraries(MyProjectLib Qt5::Widgets)
add_executable(MyProject ${MyProjectBin_src})
target_link_libraries(MyProject MyProjectLib)
當我嘗試編譯生成的項目時,出現以下錯誤:
When I try to compile the generated project, I got the following error:
錯誤 LNK1104:無法打開文件 'DebugMyProjectLib.lib'
error LNK1104: cannot open file 'DebugMyProjectLib.lib'
對應目錄Debug
包含:
MyPtojectLib.dll
MyProjectLib.ilk
MyProjectLib.pdb
推薦答案
您將 MyProjectLib
聲明為共享庫,因此除非您導出庫的全部或部分符號,否則您將只擁有一個 .dll
設計為在運行時加載,沒有 .lib
可以在編譯時鏈接,就像你試圖做的那樣.
You declared MyProjectLib
as a shared library, so unless you exported all or part of the symbols of the library, you will only have a .dll
designed to be loaded at runtime, and no .lib
to link against at compile time as you're trying to do.
一個快速的解決方案可能是將 MyProjectLib
聲明為靜態庫:
A quick solution may be to declare MyProjectLib
as a static library:
add_library(MyProjectLib STATIC ...)
另一種選擇是使用新的"cmake 功能來導出所有符號(參見 這篇文章):
Another option could be to use "new" cmake features to export all symbols (see this article):
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
您還可以通過顯式聲明要導出的符號來使用傳統"方式,如這個答案(長答案).您首先需要在代碼中的某處聲明一些 API 宏:
You can also use the "traditional" way by explicitly declaring the symbols to be exported, like in this answer (the long answer). You will first need to declare some API macro somewhere in your code:
#ifdef MyProjectLib_EXPORTS
#define MyProjectLib_API __declspec(dllexport)
#else
#define MyProjectLib_API __declspec(dllimport)
#endif
注意 MyProjectLib_EXPORTS
是由 cmake 為共享庫自動生成的:你不需要關心這個.然后對于代碼中的每個類,在聲明中使用宏:
Note that MyProjectLib_EXPORTS
is automatically generated by cmake for shared libraries: you don't need to care about this. Then for each of your class in your code, use the macro in the declaration:
class MyProjectLib_API MyClass { /* ... */ };
MyClass
在編譯MyProjectLib
時會是一個導出符號,因為MyProjectLib_EXPORTS
會被定義,而MyProjectLib_API 會擴展為__declspec(dllexport)
.所以它會被導出到一個 .lib
文件中.
MyClass
will be an exported symbol when compiling MyProjectLib
because MyProjectLib_EXPORTS
will be defined, and MyProjectLib_API will expand to __declspec(dllexport)
. So it will be exported in a .lib
file.
當鏈接到 MyProjectLib
時,它將是一個導入符號,因為 MyProjectLib_EXPORTS
將是未定義的,并且 MyProjectLib_API
將擴展為 __declspec(dllimport)
.
It will be an imported symbol when linking against MyProjectLib
because MyProjectLib_EXPORTS
will be undefined, and MyProjectLib_API
will expand to __declspec(dllimport)
.
您也可以像這樣改進您的 cmake 文件:
You may also improve your cmake file like this:
qt5_wrap_cpp(MyProjectLib_hdr_moc ${MyProjectLib_hdr})
qt5_wrap_ui (MyProjectLib_ui_moc ${MyProjectLib_ui})
您可以使用 AUTOMOC
和 AUTOUIC
代替讓 cmake 自動處理對 Qt 實用程序的調用.
You may use AUTOMOC
and AUTOUIC
instead to let cmake automatically handle the call to Qt's utilities.
include_directories (${PROJECT_SOURCE_DIR})
include_directories (${PROJECT_BINARY_DIR})
PROJECT_SOURCE_DIR
默認是一個包含目錄,我不明白你為什么需要在這里添加 PROJECT_BINARY_DIR
:只需刪除這些行.
PROJECT_SOURCE_DIR
is an include directory by default, and I can't see why you need to add PROJECT_BINARY_DIR
here: just remove these lines.
清理后,您的 cmake 文件可能會變成這樣:
Once cleaned, your cmake file may become something like this:
cmake_minimum_required(VERSION 2.8.12)
project(MyProject)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
find_package(Qt5Widgets)
set(MyProjectLib_src
${PROJECT_SOURCE_DIR}/gui.cpp
${PROJECT_SOURCE_DIR}/gui.h
${PROJECT_SOURCE_DIR}/gui.ui
)
add_library(MyProjectLib STATIC
${MyProjectLib_src}
)
target_link_libraries(MyProjectLib Qt5::Widgets)
set(MyProjectBin_src ${PROJECT_SOURCE_DIR}/main.cpp)
add_executable(MyProject
${MyProjectBin_src}
)
target_link_libraries (MyProject MyProjectLib)
這篇關于錯誤 LNK1104:無法打開文件“DebugMyProjectLib.lib"的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!