作用:
检查CXX编译器是否支持给定的标志,[Check whether the CXX compiler supports a given flag.]
使用方法:
官方说明
CHECK_CXX_COMPILER_FLAG(<flag> <var>)
<flag> - the compiler flag <var> - variable to store the result
This internally calls the check_cxx_source_compiles macro and sets CMAKE_REQUIRED_DEFINITIONS to <flag>. See help for CheckCXXSourceCompiles for a listing of variables that can otherwise modify the build. The result only tells that the compiler does not give an error message when it encounters the flag. If the flag has any effect or even a specific one is beyond the scope of this module
举个🌰:检查当前的编译器是否支持c++11
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
message(STATUS "Using flag -std=c++11.")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
message(STATUS "Using flag -std=c++0x.")
else()
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
举个🌰:避免二进制中的时间戳用于可复制构建,直到GCC 4.9才添加
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG(-Wdate-time COMPILER_SUPPORTS_WDATE_TIME)
if (COMPILER_SUPPORTS_WDATE_TIME)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wdate-time")
endif()
在4.9(含)的gcc版本中添加了-Wdate-time
告警,如果在代码中使用了__DATE__, __TIME__, or __TIMESTAMP__
这几个宏,会产生错误;
【代码搬运】