당신은 C++ 11 (/ 17분의 14) 기능을 필요로 target_compile_features를 사용할 수 있습니다
target_compile_features(target PRIVATE|PUBLIC|INTERFACE feature1 [feature2 ...])
CMAKE_CXX_KNOWN_FEATURES
에 나열된 기능이있는 feature1
으로. 당신이 공개 API에 constexpr
을 사용하려는 경우 예를 들어, 사용할 수 있습니다
add_library(foo ...)
target_compile_features(foo PUBLIC cxx_constexpr)
는 또한 옵션으로 기능을 감지 할 수있는 WriteCompilerDetectionHeader
module을 살펴 및 이전 버전과의 호환성을 제공한다 일부 기능을 구현하는 것은 컴파일러를 지원하지 않는 경우
여기
write_compiler_detection_header(
FILE foo_compiler_detection.h
PREFIX FOO
COMPILERS GNU MSVC
FEATURES cxx_constexpr cxx_nullptr
)
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
또한, FOO_CONSTEXPR
정의되고 기능은 현재 컴파일러이있는 경우 constexpr
로 확장됩니다. 그렇지 않으면 비어있게됩니다.
FOO_NULLPTR
이 정의되고 현재 컴파일러의 기능이있는 경우 nullptr
으로 확장됩니다. 그렇지 않으면 호환성 구현으로 확장됩니다 (예 : NULL
).
#include "foo_compiler_detection.h"
FOO_CONSTEXPR int bar = 0;
void baz(int* p = FOO_NULLPTR);
CMake documentation을 참조하십시오.