我正在用CMake开发一个项目.我的代码包含constexprVisual Studio 2015中允许但在Visual Studio 2013中不允许的方法.
如何检查CMakeLists.txt指定编译器是否支持该功能?我在CMake文档中看到过CMAKE_CXX_KNOWN_FEATURES,但我不明白如何使用它.
您可以使用target_compile_features来要求C++ 11(/ 14/17)功能:
target_compile_features(target PRIVATE|PUBLIC|INTERFACE feature1 [feature2 ...])
Run Code Online (Sandbox Code Playgroud)
随着feature1作为中列出的特征CMAKE_CXX_KNOWN_FEATURES.例如,如果要constexpr在公共API中使用,则可以使用:
add_library(foo ...)
target_compile_features(foo PUBLIC cxx_constexpr)
Run Code Online (Sandbox Code Playgroud)
您还应该查看允许检测功能作为选项的WriteCompilerDetectionHeader模块,并在编译器不支持时为某些功能提供向后兼容性实现:
write_compiler_detection_header(
FILE foo_compiler_detection.h
PREFIX FOO
COMPILERS GNU MSVC
FEATURES cxx_constexpr cxx_nullptr
)
Run Code Online (Sandbox Code Playgroud)
如果关键字可用,foo_compiler_detection.h将生成一个FOO_COMPILER_CXX_CONSTEXPR定义的文件constexpr:
#include "foo_compiler_detection.h"
#if FOO_COMPILER_CXX_CONSTEXPR
// implementation with constexpr available
constexpr int bar = 0;
#else
// implementation with constexpr not available
const int bar = 0;
#endif
Run Code Online (Sandbox Code Playgroud)
此外,FOO_CONSTEXPR将定义并将扩展constexpr为当前编译器是否存在该功能.否则它将是空的.
FOO_NULLPTR将被定义,nullptr如果当前编译器存在该特征,则将扩展为.否则它将扩展到兼容性实现(例如NULL).
#include "foo_compiler_detection.h"
FOO_CONSTEXPR int bar = 0;
void baz(int* p = FOO_NULLPTR);
Run Code Online (Sandbox Code Playgroud)
请参阅CMake文档.
| 归档时间: |
|
| 查看次数: |
4452 次 |
| 最近记录: |