成语使用Conan通过CMake构建系统消费包

Idiom use of Conan to consume packages by CMake build system

在柯南 documentation 中,对于 cmake_find_package 生成器,提到:

In the CMakeList.txt you do not need to specify or include anything related with Conan at all, just rely on the find_package feature

在我的 case, after calling find_package(boost COMPONENTS boost program_options REQUIRED) in CMakeLists.txt 配置模块中,${Boost_INCLUDE_DIRS} 变量为空(未定义),因此 CMake 由于 boost 缺少包含的头文件 而无法构建在 StartupConfig.cpp.
我发现的唯一解决方法是在根 CMakeLists.txt.
include(FindBoost.cmake) 如果我评论 include(FindBoost.cmake) 并在 find_package 中通过 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 中找到所需的包?

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