在Conan 文档 for cmake_find_package生成器中,提到:
在CMakeList.txt中,您根本不需要指定或包含任何与Conan相关的内容,只需依赖于find_package特性即可。
在我的案例中,在CMakeLists.txt of config模块中调用find_package(boost COMPONENTS boost program_options REQUIRED)
之后,${Boost_INCLUDE_DIRS}
变量为空(未定义),因此CMake无法构建,原因是CMake缺少头文件< boost /Programoptions.hpp>包含在StartupConfig.cpp中。
我找到的唯一解决办法是在根include(FindBoost.cmake)
中使用CMakeLists.txt。
如果我注释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的要求找到包吗?
发布于 2021-12-29 15:16:48
正如@Tsyvarev所建议的,在target_link_libraries(${PROJECT_NAME} INTERFACE boost::boost boost::program_options)
中通过私有方式更改接口解决了我的问题。有关更多细节,我建议阅读有关CMake作用域含义:https://leimao.github.io/blog/CMake-Public-Private-Interface/的说明。
https://stackoverflow.com/questions/70519181
复制相似问题