错误LNK1104:无法打开文件'Debug\MyProjectLib.lib' [英] Error LNK1104: cannot open file 'Debug\MyProjectLib.lib'
问题描述
我有以下 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:无法打开文件'Debug\MyProjectLib.lib'
error LNK1104: cannot open file 'Debug\MyProjectLib.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.
与<$链接时将是导入的符号c $ c> 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
和而是让AUTO来让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:无法打开文件'Debug\MyProjectLib.lib'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!