问题描述
我有以下 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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!