【发布时间】:2021-07-09 21:37:22
【问题描述】:
[背景] 我必须将一些 .cpp 文件从 repo [repo2] 移出到新的 repo [repo1]。为此,我从 repo2 中获取了这些 cpp 文件并制作了一个库(repo1 中的 xyz.lib),如下所示。
[在 repo1]
add_library(XYZ PUBLIC stdafx.h
stdafx.cpp
m.cpp
p.cpp)
target_link_libraries(XYZ CONAN_PKG::SDK)
set_source_files_properties(stdafx.cpp PROPERTIES COMPILE_FLAGS "/Yc")
target_include_directories(XYZ PRIVATE
${CMAKE_SOURCE_DIR}/<some_header>
)
[In repo2] - repo1 中的这个库可通过 conan-package 提供给 repo2。这就是它的消费方式:
add_library(M_TARGET SHARED
#m.cpp #These files commented out as, its now available through lib from repo1
#p.cpp #These files commented out as, its now available through lib from repo1
m.h
p.h
stdafx.cpp)
target_link_libraries(M_TARGET
CONAN_PKG::XYZ)
问题:当我编译 repo2 时,它无法链接 CONAN_PKG::XYZ 中可用的库来构建目标 M_TARGET。
我尝试了什么?
- 我尝试关注其他链接,但找不到任何有效的链接。 cmake does not link conan's installed library
Cmake target_link_libraries not linking my library 2.尝试将库从公共更改为静态,反之亦然,不起作用。
这里是 conanbuildinfo.cmake :
set(CONAN_LIB_DIRS_XYZ "<PATH>/lib")
目录结构:
<PATH>/lib/debug/XYZ.lib
<PATH>/lib/release/XYZ.lib
即使我将XYZ.lib 放入<PATH>/lib,也会出现链接错误。
错误:
error LNK2001: unresolved external symbol
M_TARGET.dll : fatal error LNK1120: 2 unresolved externals
如果我浏览到 Visual Studio 中的属性并在链接器输入中手动添加库,它会编译。我错过了什么?请帮忙,因为这阻碍了我的工作。
[按要求添加更多日志]:
37>Link:
Creating library C:/build/install/lib/RelWithDebInfo/M_TARGET.lib and object C:/build/install/lib/RelWithDebInfo/M_TARGET.exp
37>communication_handler.obj : error LNK2001: unresolved external symbol "public: int __cdecl xy_z::XYServiceZ::Init(char const *)" (?Init@XYServiceZ@rxy_z@@QEAAHPEBD@Z) [C:\build\src\M_TARGET\M_TARGET.vcxproj]
37>communication_handler.obj : error LNK2001: unresolved external symbol "public: __cdecl xy_z::XYServiceZ::XYServiceZ(class dx::Dupe &)" (??0XYServiceZ@xy_z@@QEAA@AEDupe@dx@@@Z) [C:\build\src\M_TARGET\M_TARGET.vcxproj]
63>CustomBuild:
Building Custom Rule C:/src/libs/ScanLib/test/unit/scan_manager_test/CMakeLists.txt
CMake does not need to re-run because C:\build\src\libs\ScanLib\test\unit\scan_manager_test\CMakeFiles\generate.stamp is up-to-date.
37>C:\build\install\bin\RelWithDebInfo\M_TARGET.dll : fatal error LNK1120: 2 unresolved externals [C:\build\src\M_TARGET\M_TARGET.vcxproj]
[错误结束]
【问题讨论】:
-
请显示minimal reproducible example。完整的错误信息是什么?缺少的符号是在哪个库中定义的?
-
有必要在这个最小的可重现示例中查看您用于创建包 xyz 的 conanfile.py 文件(不仅是源文件和 CMakeLists.txt),是否有可能xyz 配方中缺少。
-
我已尝试添加错误消息,但不确定它有多大帮助。 @AlanBirtles:符号在 XYZ 库中。
-
@drodri :我假设该库已正确打包,因为当我进行手动链接时,它可以工作。你还需要 conanfile.py 吗?
-
@AlanBirtles - 我可能在 repo2 的 cmake 中遗漏了一些我不确定的东西。否则手动链接也应该失败。