【问题标题】:Idiom use of Conan to consume packages by CMake build systemCMake构建系统使用柯南消费包的成语
【发布时间】:2021-12-29 12:06:47
【问题描述】:

在柯南documentation的cmake_find_package生成器中提到:

在 CMakeList.txt 中,您根本不需要指定或包含任何与柯南相关的内容,只需依赖 find_package 功能即可

在我的case 中,在配置模块的CMakeLists.txt 中调用find_package(boost COMPONENTS boost program_options REQUIRED) 后,${Boost_INCLUDE_DIRS} 变量为空(未定义),因此 CMake 由于 boost 缺少头文件而无法构建 包含在 StartupConfig.cpp 中。
我发现的唯一解决方法是在根目录CMakeLists.txt 中使用include(FindBoost.cmake)
如果我评论 include(FindBoost.cmake) 并在 find_package 中通过 Boost 更改 boost,我会收到以下构建错误:

maze/0.1.0: Calling build()
maze/0.1.0: CMake command: cmake -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE="/root/.conan/data/maze/0.1.0/_/_/build/5f5fdaf06f8bd18961521dbe87621377ba962aa5/conan_toolchain.cmake" -DCMAKE_INSTALL_PREFIX="/root/.conan/data/maze/0.1.0/_/_/package/5f5fdaf06f8bd18961521dbe87621377ba962aa5" "/root/.conan/data/maze/0.1.0/_/_/build/5f5fdaf06f8bd18961521dbe87621377ba962aa5"
Using Conan toolchain: /root/.conan/data/maze/0.1.0/_/_/build/5f5fdaf06f8bd18961521dbe87621377ba962aa5/conan_toolchain.cmake.
-- The C compiler identification is GNU 9.2.0
-- The CXX compiler identification is GNU 9.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/local/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Conan: Component target declared 'GTest::gtest'
-- Conan: Component target declared 'GTest::gtest_main'
-- Conan: Component target declared 'GTest::gmock'
-- Conan: Component target declared 'GTest::gmock_main'
-- Conan: Target declared 'gtest::gtest'
-- Conan: Component target declared 'boost::headers'
-- Conan: Component target declared 'boost::_boost_cmake'
-- Conan: Component target declared 'boost::diagnostic_definitions'
-- Conan: Component target declared 'boost::disable_autolinking'
-- Conan: Component target declared 'boost::dynamic_linking'
-- Conan: Component target declared 'boost::_libboost'
-- Conan: Component target declared 'boost::atomic'
-- Conan: Component target declared 'boost::container'
-- Conan: Component target declared 'boost::date_time'
-- Conan: Component target declared 'boost::exception'
-- Conan: Component target declared 'boost::math'
-- Conan: Component target declared 'boost::math_c99'
-- Conan: Component target declared 'boost::math_c99f'
-- Conan: Component target declared 'boost::math_c99l'
-- Conan: Component target declared 'boost::math_tr1'
-- Conan: Component target declared 'boost::math_tr1f'
-- Conan: Component target declared 'boost::math_tr1l'
-- Conan: Component target declared 'boost::program_options'
-- Conan: Component target declared 'boost::regex'
-- Conan: Component target declared 'boost::serialization'
-- Conan: Component target declared 'boost::stacktrace'
-- Conan: Component target declared 'boost::stacktrace_addr2line'
-- Conan: Component target declared 'boost::stacktrace_backtrace'
-- Conan: Component target declared 'boost::stacktrace_basic'
-- Conan: Component target declared 'boost::stacktrace_noop'
-- Conan: Component target declared 'boost::system'
-- Conan: Component target declared 'boost::test'
-- Conan: Component target declared 'boost::test_exec_monitor'
-- Conan: Component target declared 'boost::wserialization'
-- Conan: Component target declared 'boost::chrono'
-- Conan: Component target declared 'boost::filesystem'
-- Conan: Component target declared 'boost::nowide'
-- Conan: Component target declared 'boost::prg_exec_monitor'
-- Conan: Component target declared 'boost::random'
-- Conan: Component target declared 'boost::thread'
-- Conan: Component target declared 'boost::timer'
-- Conan: Component target declared 'boost::type_erasure'
-- Conan: Component target declared 'boost::unit_test_framework'
-- Conan: Component target declared 'boost::wave'
-- Conan: Component target declared 'boost::context'
-- Conan: Component target declared 'boost::contract'
-- Conan: Component target declared 'boost::coroutine'
-- Conan: Component target declared 'boost::fiber'
-- Conan: Component target declared 'boost::fiber_numa'
-- Conan: Component target declared 'boost::graph'
-- Conan: Component target declared 'boost::iostreams'
-- Conan: Component target declared 'boost::locale'
-- Conan: Component target declared 'boost::log'
-- Conan: Component target declared 'boost::log_setup'
-- Conan: Target declared 'boost::boost'
-- Conan: Target declared 'bzip2::bzip2'
-- Conan: Target declared 'ZLIB::ZLIB'
-- Conan: Target declared 'libbacktrace::libbacktrace'
-- Conan: Component target declared 'GTest::gtest'
-- Conan: Component target declared 'GTest::gtest_main'
-- Conan: Component target declared 'GTest::gmock'
-- Conan: Component target declared 'GTest::gmock_main'
-- Conan: Target declared 'gtest::gtest'
-- Conan: Component target declared 'GTest::gtest'
-- Conan: Component target declared 'GTest::gtest_main'
-- Conan: Component target declared 'GTest::gmock'
-- Conan: Component target declared 'GTest::gmock_main'
-- Conan: Target declared 'gtest::gtest'
-- Configuring done
-- Generating done
-- Build files have been written to: /root/.conan/data/maze/0.1.0/_/_/build/5f5fdaf06f8bd18961521dbe87621377ba962aa5
maze/0.1.0: CMake command: cmake --build '/root/.conan/data/maze/0.1.0/_/_/build/5f5fdaf06f8bd18961521dbe87621377ba962aa5' '--' '-j12'
[  7%] Building CXX object config/CMakeFiles/Configs.dir/src/StartupConfig.cpp.o
[ 14%] Building CXX object generator/CMakeFiles/Generator.dir/src/MazeGenerator.cpp.o
[ 21%] Building CXX object solver/CMakeFiles/Solver.dir/src/MazeSolver.cpp.o
/root/.conan/data/maze/0.1.0/_/_/build/5f5fdaf06f8bd18961521dbe87621377ba962aa5/config/src/StartupConfig.cpp:5:10: fatal error: boost/program_options.hpp: No such file or directory
    5 | #include <boost/program_options.hpp>
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [config/CMakeFiles/Configs.dir/build.make:76: config/CMakeFiles/Configs.dir/src/StartupConfig.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:203: config/CMakeFiles/Configs.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....

我是否错过了ConanFile 中的配置?
您能否提出更惯用的方式,以便 CMake 找到 ConanFile 中所需的包?

【问题讨论】:

  • 正确的拼写是find_package(Boost ...),而不是find_package(boost ...)
  • 完整的输出也有帮助,很可能是因为名称错误(使用Boost)而没有找到它,但也可能是你没有传递工具链,如果构建为消费者直接与cmake ... -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake,或类似的东西。
  • 请将set( Boost_DEBUG ON) 放入您的CMakeLists.txt。它应该为您提供有关正在发生的事情的更多信息。
  • @Tsyvarev 通过 Boost 更改提升并不能解决问题。
  • @Jellyboy 实际上,我在描述中添加了错误消息。

标签: c++ boost cmake packaging conan


【解决方案1】:

按照@Tsyvarev 的建议,在target_link_libraries(${PROJECT_NAME} INTERFACE boost::boost boost::program_options) 中将INTERFACE 更改为PRIVATE 解决了我的问题。 有关更多详细信息,我建议阅读有关 CMake 范围含义的说明: https://leimao.github.io/blog/CMake-Public-Private-Interface/

【讨论】:

    猜你喜欢
    • 2021-12-15
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 2021-12-09
    • 1970-01-01
    • 2021-11-14
    相关资源
    最近更新 更多