使用 target_link_libraries 链接通过 conan-package 可用的库时出错。手动链接时,它有效
Error while linking library available through conan-package using target_link_libraries. When linking manually, it works
[背景] 我必须将一些 .cpp 文件从存储库 [repo2] 移到新的存储库 [repo1]。为了实现这一点,我从 repo2 中获取了这些 cpp 文件并制作了一个库(xyz.lib in repo1)如下。
[在 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>
)
[在 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 时,它无法 link CONAN_PKG::XYZ
中可用的库来构建目标 M_TARGET
.
我尝试了什么?
- 我试图关注其他 link,但我找不到任何有用的东西。
cmake does not link conan's installed library
Cmake target_link_libraries not linking my library
2. 尝试将库从 public 更改为静态,反之亦然,但没有用。
这是conanbuildinfo.cmake:
set(CONAN_LIB_DIRS_XYZ "<PATH>/lib")
目录结构:
<PATH>/lib/debug/XYZ.lib
<PATH>/lib/release/XYZ.lib
即使我将 XYZ.lib
放入 <PATH>/lib
,我也会收到 linking 错误。
错误:
error LNK2001: unresolved external symbol
M_TARGET.dll : fatal error LNK1120: 2 unresolved externals
如果我浏览到 visual studio 中的属性并 在 link er 输入中手动添加库,它会编译 。我错过了什么?请帮助,因为这阻碍了我的工作。
[按要求添加更多日志]:
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]
[错误结束]
在 conanbuildinfo.cmake
中,某些 PACKAGE_LIBS
路径未设置。当创建包的 conanfile.py
不包含有关包中存在的库的确切路径的信息时,就会发生这种情况。在这种情况下,我们需要在创建包时明确提及库的名称。
例如:
如果我制作了一个名称为 CONAN_PKG::Remediation
且目录结构如下的包:
在 lib/
里面我们有 RemediationTest.lib
.
当我们在 target_link_libraries() 中使用它时,这个库无法识别
target_link_libraries(M_TARGET CONAN_PKG::Remediation)
。由于这无法识别 lib/,因此会导致 未解决的外部错误 [链接错误]。
修复:
创建包时,在 conanfile.py
中添加:
conanfile.py:
def package_info(self):
self.cpp_info.libs = ["RemediationTest"]
[背景] 我必须将一些 .cpp 文件从存储库 [repo2] 移到新的存储库 [repo1]。为了实现这一点,我从 repo2 中获取了这些 cpp 文件并制作了一个库(xyz.lib in repo1)如下。
[在 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>
)
[在 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 时,它无法 link CONAN_PKG::XYZ
中可用的库来构建目标 M_TARGET
.
我尝试了什么?
- 我试图关注其他 link,但我找不到任何有用的东西。 cmake does not link conan's installed library
Cmake target_link_libraries not linking my library 2. 尝试将库从 public 更改为静态,反之亦然,但没有用。
这是conanbuildinfo.cmake:
set(CONAN_LIB_DIRS_XYZ "<PATH>/lib")
目录结构:
<PATH>/lib/debug/XYZ.lib
<PATH>/lib/release/XYZ.lib
即使我将 XYZ.lib
放入 <PATH>/lib
,我也会收到 linking 错误。
错误:
error LNK2001: unresolved external symbol
M_TARGET.dll : fatal error LNK1120: 2 unresolved externals
如果我浏览到 visual studio 中的属性并 在 link er 输入中手动添加库,它会编译 。我错过了什么?请帮助,因为这阻碍了我的工作。
[按要求添加更多日志]:
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]
[错误结束]
在 conanbuildinfo.cmake
中,某些 PACKAGE_LIBS
路径未设置。当创建包的 conanfile.py
不包含有关包中存在的库的确切路径的信息时,就会发生这种情况。在这种情况下,我们需要在创建包时明确提及库的名称。
例如:
如果我制作了一个名称为 CONAN_PKG::Remediation
且目录结构如下的包:
在 lib/
里面我们有 RemediationTest.lib
.
当我们在 target_link_libraries() 中使用它时,这个库无法识别
target_link_libraries(M_TARGET CONAN_PKG::Remediation)
。由于这无法识别 lib/,因此会导致 未解决的外部错误 [链接错误]。
修复:
创建包时,在 conanfile.py
中添加:
conanfile.py:
def package_info(self):
self.cpp_info.libs = ["RemediationTest"]