如何解决 Conan 未创建 CMake 可以找到的包目标的问题

问题描述 投票:0回答:1

我在尝试将 Boost::filesystem 与 Conan 包管理器和 CMake 一起使用时遇到链接器错误。我已通过 Conan 成功安装了 Boost,但无法链接到 Boost::filesystem。详情如下:

这是我的

project/conanfile.txt

[requires]
boost/1.86.0

[generators]
CMakeDeps
CMakeToolchain

要安装依赖项,我导航到根目录 (

cd project
) 并运行:
conan install . -of=external --build=MISSING

输出:

======== Finalizing install (deploy, generators) ========
conanfile.txt: Writing generators to <redacted>/TestProject/external
conanfile.txt: Generator 'CMakeDeps' calling 'generate()'
conanfile.txt: CMakeDeps necessary find_package() and targets for your CMakeLists.txt
    find_package(Boost)
    target_link_libraries(... boost::boost)
conanfile.txt: Generator 'CMakeToolchain' calling 'generate()'
conanfile.txt: CMakeToolchain generated: conan_toolchain.cmake
conanfile.txt: CMakeToolchain: Preset 'conan-release' added to CMakePresets.json.
    (cmake>=3.23) cmake --preset conan-release
    (cmake<3.23) cmake <path> -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake  -DCMAKE_POLICY_DEFAULT_CMP0091=NEW -DCMAKE_BUILD_TYPE=Release
conanfile.txt: CMakeToolchain generated: <redacted>/TestProject/external/CMakePresets.json
conanfile.txt: CMakeToolchain generated: <redacted>/TestProject/CMakeUserPresets.json
conanfile.txt: Generating aggregated env files
conanfile.txt: Generated aggregated env files: ['conanbuild.sh', 'conanrun.sh']
Install finished successfully

这将成功安装所有依赖项(以及传递依赖项)。我遇到的问题与从 Boost 链接库有关。在我的

project/CMakeLists.txt

cmake_minimum_required(VERSION 3.29)
project(TestProject)

add_executable(TestProject main.cpp)

find_package(Boost)

set(Boost_FILESYSTEM_LIB_PATH ${Boost_INCLUDE_DIRS}/../lib/libboost_filesystem.a)

target_include_directories(TestProject PUBLIC ${Boost_INCLUDE_DIRS})

target_link_libraries(TestProject
        PUBLIC
        # This works
        Boost::boost
        # This works (When commented out, linker error below occurs)
        #${Boost_FILESYSTEM_LIB_PATH}
        # Using this to link introduces linker errors
        Boost::filesystem
)

这是尝试使用文件系统符号的代码

project/main.cpp

#include <iostream>
#include <boost/filesystem.hpp>

int main()
{
  auto ret = boost::filesystem::exists("/foo");
  std::cout << ret << std::endl;
  return 0;
}

我现在运行该程序。项目总体结构如下:

project/CMakeLists.txt
project/external (Conan output folder)
project/main.cpp
project/build

使用 Makefile 进行构建(通过终端):

cd project/build
cmake -DCMAKE_TOOLCHAIN_FILE=external/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Debug ..
make 

这是尝试通过

Boost::filesystem
进行链接时的链接器错误输出:

[ 50%] Building CXX object CMakeFiles/TestProject.dir/main.cpp.o
[100%] Linking CXX executable TestProject
Undefined symbols for architecture arm64:
  "boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)", referenced from:
      boost::filesystem::exists(boost::filesystem::path const&) in main.cpp.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

这是尝试通过

${Boost_FILESYSTEM_LIB_PATH
进行链接时的输出:

[ 50%] Building CXX object CMakeFiles/TestProject.dir/main.cpp.o
[100%] Linking CXX executable TestProject
[100%] Built target TestProject

我尝试更改线路:

find_package(Boost)
至:
find_package(Boost REQUIRED COMPONENTS filesystem)
无济于事。

如何解决这些链接器错误(而不是使用显式库路径)?我读过柯南教程并观看了一些视频。我在几个图书馆中都遇到过这种情况。我仍在学习如何使用柯南,所以我可能会遗漏一些非常明显的东西。感谢您的帮助。

c++ macos conan apple-clang
1个回答
0
投票

使用 conan 时,您无法在 cmake 命令行上更改诸如

CMAKE_BUILD_TYPE
之类的设置,您已经使用
conan install
安装了发行包,但是当您将构建类型更改为调试时,将生成 Conan 目标以链接到库的不存在的调试版本(您不会收到错误,因为 conan 最终使用 cmake 忽略的未定义变量)。

您应该为调试版本设置单独的 conan 配置文件,或者在 conan 安装期间设置调试设置:

conan install . -of=external --build=MISSING -s build_type=debug

参见https://docs.conan.io/2/tutorial/consuming_packages/ Different_configurations.html

© www.soinside.com 2019 - 2024. All rights reserved.