cmake conan2:在包中找不到库'X'(在“测试包”阶段)

yb3bgrhw  于 2023-10-20  发布在  其他
关注(0)|答案(1)|浏览(150)

我在用柯南2试试运气,但我很难让它来建我的图书馆。准确地说,我的库打包得很好,但是test_package调用失败了,因为它找不到所述库。下面是conan生成的cmakedeps_macros.cmake的CMake错误:

======== Testing the package: Building ========
cojson/0.1.0 (test package): Calling build()
cojson/0.1.0 (test package): Running CMake.configure()
cojson/0.1.0 (test package): RUN: cmake -G "Visual Studio 17 2022" -DCMAKE_TOOLCHAIN_FILE="C:/dev/cojson/test_package/build/msvc-193-x86_64-20-release/generators/conan_toolchain.cmake" -DCMAKE_INSTALL_PREFIX="C:/dev/cojson/test_package" -DCMAKE_POLICY_DEFAULT_CMP0091="NEW" "C:\dev\cojson\test_package"
...(cmake stuff)...
CMake Error at build/msvc-193-x86_64-20-release/generators/cmakedeps_macros.cmake:67 (message):
  Library 'cojson::cojson' not found in package.  If 'cojson::cojson' is a
  system library, declare it with 'cpp_info.system_libs' property

我不太擅长看窗帘后面。我对cmake有相当好的理解,但我不太明白目标是如何导入到test_package上下文中的。所以我有点迷失了这个错误。以下是我的项目结构:

├── CMakeLists.txt
├── src
│   ├── CMakeLists.txt
│   └── main.cpp
├── include
│   └── cojson
│       └── myheader.hpp
├── tests
│   ├── CMakeLists.txt
│   └── tests.cpp
├── test_package
│   ├── conanfile.py
│   ├── CMakeLists.txt
│   └── src
│       └── example.cpp
└── LICENSE

我已经看过了所有的教程示例(当然这些示例可以顺利地工作),并在文档中四处查看,但我没有找到一个关于为什么目标可能不知道的具体提示。我的猜测是

  • 目标使用不同的导出名称:实际上,目标名为cojson_core,但我将导出名称更改为cojson,如下所示:

set_target_properties(cojson_core PROPERTIES EXPORT_NAME cojson)
这样行不行
以下是相关部分:

  • conanfile.py(root):*
import os
from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout
from conan.tools.build import check_min_cppstd
from conan.tools.files import copy

class cojsonConan(ConanFile):
    name = "cojson"
    version = "0.1.0"

    # Optional metadata
    license = "https://github.com/MartyMcFlyInTheSky/cojson/blob/master/LICENSE"
    author = "Samuel Beer"
    url = "https://github.com/MartyMcFlyInTheSky/cojson"
    description = "A C++ JSON library using coroutines"
    topics = ("json", "coroutines", "cpp20")

    # Binary configuration
    settings = "os", "compiler", "build_type", "arch"
    options = {
        "shared": [True, False],
        "fPIC": [True, False],
    }
    default_options = {
        "shared": False,
        "fPIC": True,
    }

    # Sources located in same place
    exports = ["LICENSE"]
    exports_sources = "CMakeLists.txt", "src/*", "include/*", "tests/*"

    def validate(self):
        check_min_cppstd(self, "20")

    def config_options(self):
        if self.settings.os == "Windows":
            del self.options.fPIC

    def configure(self):
        # delete fPIC option if shared
        if self.options.shared:
            # If os=Windows, fPIC will have been removed in config_options()
            # use rm_safe to avoid double delete errors
            self.options.rm_safe("fPIC")

    def requirements(self):
        self.test_requires("gtest/1.14.0")

    def _configure_cmake(self):
        cmake = CMake(self)
        cmake.configure()
        return cmake

    def layout(self):
        cmake_layout(self)

    def generate(self):
        tc = CMakeToolchain(self)
        tc.generate()

    def build(self):
        cmake = self._configure_cmake()
        cmake.build()
        # if not self.conf.get("tools.build:skip_test", default=False):
        cmake.test()

    def package(self):
        copy(self, "LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
        cmake = self._configure_cmake()
        cmake.install()

    # consumer information
    def package_info(self):
        self.cpp_info.libs = ["cojson"]
  • conanfile.py(test_package):*
import os

from conan import ConanFile
from conan.tools.cmake import CMake, cmake_layout
from conan.tools.build import can_run

class cojsonConan(ConanFile):
    settings = "os", "compiler", "build_type", "arch"
    generators = "CMakeDeps", "CMakeToolchain"

    def requirements(self):
        self.requires(self.tested_reference_str)

    def build(self):
        cmake = CMake(self)
        cmake.configure()
        cmake.build()

    def layout(self):
        cmake_layout(self)

    def test(self):
        if can_run(self):
            cmd = os.path.join(self.cpp.build.bindir, "example")
            self.run(cmd, env="conanrun")

编辑1

@Tsyvarev在评论中有一个值得注意的想法,即不导出单独的目标名称。所以我所做的是我把我的主要目标放在cojson_core上,并把它别名为cojson::cojson_core以保持一致性,并在tests和test_package CMakeFiles中进行了调整:

  • test_package/CMakeLists.txt*
...
target_link_libraries(example cojson::cojson_core)
...

在conanfile中,我还必须调整导出的库:

  • conanfile.py*
def package_info(self):
    # self.cpp_info.libs = ["cojson"]
    self.cpp_info.libs = ["cojson_core"]
    self.cpp_info.set_property("cmake_target_name", "cojson::cojson_core")

这至少让我更进一步,因为似乎找到了cojson::cojson_core。然而:我现在得到另一个错误,声明它不知道lib头文件在哪里:

C:\dev\cojson\test_package\src\example.cpp(2,10): fatal  error C1083: Datei (Include) kann nicht geöffnet werden: "cojson/cojson.hpp": No such file or directory [C:\dev\cojson\test_package\build\msvc-193-x
86_64-20-release\example.vcxproj]

这非常令人困惑,因为test_package显然已经找到了应该声明include目录的cojson::cojson_core目标。我用cmake单独测试了test_package,如果我这样做,它就可以工作:
cojson/test_package/中执行

cmake -S . -B build -G "MinGW Makefiles" -DCMAKE_INSTALL_PREFIX=C:\dev\installcojson
cmake --build build

目标被找到,包含目录也被找到。为什么它不工作时,柯南是这样做。有什么想法吗?也许有人能解释一下柯南是如何定义目标的:这些“ Package 器”目标是conan为每个实际的cmake目标定义的吗?

编辑2

丢失头文件的问题现已解决。我已经排除了

set_target_properties(cojson_core PROPERTIES PUBLIC_HEADER
    ${CMAKE_CURRENT_SOURCE_DIR}/../include/cojson/cojson.hpp;
)

因为某种原因把它插回去最终让我构建了这个包。不过,这个问题还没有解决,因为我希望我的目标有一个不同的导出名称:我尝试再次调整 * conanfile.py * 中的属性

def package_info(self):
    # self.cpp_info.libs = ["cojson"]
    self.cpp_info.libs = ["cojson"]
    self.cpp_info.set_property("cmake_target_name", "cojson::cojson")

......上面那个臭名昭著的错误又回来了。目标怎么会不被识别?

qij5mzcb

qij5mzcb1#

我解决了
问题是conan工具链不仅会查找配置模块,还会查找使用find_library()的二进制文件

  • test_package/build/msvc../generators/cmakedeps_macros.cmake:23*:
find_library(CONAN_FOUND_LIBRARY NAMES ${_LIBRARY_NAME} PATHS ${package_libdir}
             NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)

因此,二进制名称必须与conanfile.py的库名称完全匹配。

解决方案

添加

set_target_properties(cojson_core PROPERTIES OUTPUT_NAME "cojson")

到与 conanfile.py 中定义的库对应的目标:

def package_info(self):
    self.cpp_info.libs = ["cojson"]

相关问题