【问题标题】:Error while linking library available through conan-package using target_link_libraries. When linking manually, it works使用 target_link_libraries 通过 conan-package 链接可用库时出错。手动链接时,它可以工作
【发布时间】: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

我尝试了什么?

  1. 我尝试关注其他链接,但找不到任何有效的链接。 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 放入&lt;PATH&gt;/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 中遗漏了一些我不确定的东西。否则手动链接也应该失败。

标签: c++ cmake conan


【解决方案1】:

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"]

【讨论】:

    猜你喜欢
    • 2021-11-18
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    • 2019-09-15
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多