使用 CMake 构建库时如何将柯南的 compiler.cppstd 设置传播到编译器?
How to propagate conan's compiler.cppstd setting to the compiler when building a library with CMake?
如果您使用 conan 构建库并将 compiler.cppstd
设置设置为例如20
并调用 conan install
,库仍使用给定编译器的默认标准构建。
The docs 说:
The value of compiler.cppstd provided by the consumer is used by the build helpers:
- The CMake build helper will set the CONAN_CMAKE_CXX_STANDARD and CONAN_CMAKE_CXX_EXTENSIONS definitions that will be converted to the corresponding CMake variables to activate the standard automatically with the conan_basic_setup() macro.
看来您需要调用 conan_basic_setup()
来激活此设置。但是我怎么称呼它呢?通过修补库的 CMakeLists.txt?我肯定不想仅仅为了使用正确的标准版本而这样做。我可以看到一些根据设置手动设置 CMake 定义的食谱,例如:
- https://github.com/conan-io/conan-center-index/blob/master/recipes/folly/all/conanfile.py#L117
- https://github.com/conan-io/conan-center-index/blob/master/recipes/crc32c/all/conanfile.py#L58
- https://github.com/conan-io/conan-center-index/blob/master/recipes/azure-storage-cpp/all/conanfile.py#L71
- https://github.com/conan-io/conan-center-index/blob/master/recipes/caf/all/conanfile.py#L105
但这也感觉像是一个 hack。那么确保使用我指定的 compiler.cppstd
构建库的正确方法是什么?
避免打补丁,它既丑陋又脆弱,对于每个新版本,由于上游的更改,您都需要更新。
主要方法是 CMakeLists.txt 作为包装器,作为真实示例:https://github.com/conan-io/conan-center-index/blob/5f77986125ee05c4833b0946589b03b751bf634a/recipes/proposal/all/CMakeLists.txt 以及许多其他方法。
cmake_minimum_required(VERSION 3.1)
project(cmake_wrapper)
include(conanbuildinfo.cmake)
conan_basic_setup(KEEP_RPATHS)
add_subdirectory(source_subfolder)
这里重要的是包括 conanbuildinfo.cmake
,它由 cmake Conan generator, then the CMake 助手生成,其余的将完成。
这里的source_subfolder
是项目源文件夹,配置好所有Conan设置后CMakeLists.txt会加载主CMakeLists.txt
如果您使用 conan 构建库并将 compiler.cppstd
设置设置为例如20
并调用 conan install
,库仍使用给定编译器的默认标准构建。
The docs 说:
The value of compiler.cppstd provided by the consumer is used by the build helpers:
- The CMake build helper will set the CONAN_CMAKE_CXX_STANDARD and CONAN_CMAKE_CXX_EXTENSIONS definitions that will be converted to the corresponding CMake variables to activate the standard automatically with the conan_basic_setup() macro.
看来您需要调用 conan_basic_setup()
来激活此设置。但是我怎么称呼它呢?通过修补库的 CMakeLists.txt?我肯定不想仅仅为了使用正确的标准版本而这样做。我可以看到一些根据设置手动设置 CMake 定义的食谱,例如:
- https://github.com/conan-io/conan-center-index/blob/master/recipes/folly/all/conanfile.py#L117
- https://github.com/conan-io/conan-center-index/blob/master/recipes/crc32c/all/conanfile.py#L58
- https://github.com/conan-io/conan-center-index/blob/master/recipes/azure-storage-cpp/all/conanfile.py#L71
- https://github.com/conan-io/conan-center-index/blob/master/recipes/caf/all/conanfile.py#L105
但这也感觉像是一个 hack。那么确保使用我指定的 compiler.cppstd
构建库的正确方法是什么?
避免打补丁,它既丑陋又脆弱,对于每个新版本,由于上游的更改,您都需要更新。
主要方法是 CMakeLists.txt 作为包装器,作为真实示例:https://github.com/conan-io/conan-center-index/blob/5f77986125ee05c4833b0946589b03b751bf634a/recipes/proposal/all/CMakeLists.txt 以及许多其他方法。
cmake_minimum_required(VERSION 3.1)
project(cmake_wrapper)
include(conanbuildinfo.cmake)
conan_basic_setup(KEEP_RPATHS)
add_subdirectory(source_subfolder)
这里重要的是包括 conanbuildinfo.cmake
,它由 cmake Conan generator, then the CMake 助手生成,其余的将完成。
这里的source_subfolder
是项目源文件夹,配置好所有Conan设置后CMakeLists.txt会加载主CMakeLists.txt