C++有C++11、C++17、C++20等,应该如何指定具体哪个版本的C++呢?
实例
方法:低版本时使用
实例
此方法显示了设置C++标准的通用方法。这可以与大多数版本的CMake一起使用。但是,如果你使用CMake的最新版本,则可以用更方便的方法。
# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 2.8)
# Set the project name
project (hello_cpp11)
# try conditional compilation
# 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>
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
# check results and add flag
if(COMPILER_SUPPORTS_CXX11)#
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)#
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
# Add an executable
add_executable(${PROJECT_NAME} main.cpp)
理论
(1)检查编译标志是否支持
- CMake支持尝试使用传递给函数CMAKE_CXX_COMPILER_FLAG的任何标志编译程序。然后将结果存储在你传入的变量中。比如:
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
- 此示例将尝试使用标志-std=c++11编译程序,并将结果存储在变量COMPILER_SUPPORTS_CXX11中。
include(CheckCXXCompilerFlag)
这一行告诉CMake包含此函数以使其可用。
(2)添加标志
- 一旦确定编译是否支持标志,就可以使用标准的cmake方法将该标志添加到目标。在本例中,我们使用CMAKE_CXX_FLAGS将该标志传递到所有目标。
if(COMPILER_SUPPORTS_CXX11)#
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)#
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
- 上面的示例只检查编译标志的GCC版本,并支持从C++11回退到标准化前的C++0x标志。在实际使用中,你可能希望检查C14,或者添加对不同编译设置方法的支持,例如-std=gnu11。
结果说明
(1)怎么看执行C++几
执行
cmake ..
make VERBOSE=1
方法:使用CMAKE_CXX_STANDARD变量
从CMake v3.1开始,可以CMAKE_CXX_STANDARD变量设置C++标准
实例
# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.1)
# Set the project name
project (hello_cpp11)
# set the C++ standard to C++ 11
set(CMAKE_CXX_STANDARD 11)
# Add an executable
add_executable(${PROJECT_NAME} main.cpp)
理论
- 设置
CMAKE_CXX_STANDARD
变量会导致所有目标上的CXX_STANDARD属性改变。这会影响CMake在编译时设置适当的标志。 - 注意:
CMAKE_CXX_STANDARD
变量会回退到最接近的不会失败的适当标准。例如,如果请求-std=gnu11,最后可能变成-std=gnu0x。这可能会在编译时导致意外故障。
结果说明
方法:使用target_compile_features函数
实例
cmake_minimum_required(VERSION 3.1)
# Set the project name
project (hello_cpp11)
# Add an executable
add_executable(${PROJECT_NAME} main.cpp)
# set the C++ standard to the appropriate standard for using auto
target_compile_features(${PROJECT_NAME} PUBLIC cxx_auto_type)
# Print the list of known compile features for this version of CMake
message("List of compile features: ${CMAKE_CXX_COMPILE_FEATURES}")
理论
使用target_compile_features
- 在目标上调用target_compile_features函数将检查传入的功能,并由CMake确定正确的用于目标的编译器标志。
target_compile_features(${PROJECT_NAME} PUBLIC cxx_auto_type)
- 与其他target_*函数一样,你可以指定所选目标的功能范围。这将填充目标的INTERFACE_COMPILE_FEATURES属性。可用功能列表可从CMAKE_CXX_COMPILE_FEATURES变量中找到。你可以使用以下代码获取可用功能的列表:
message("List of compile features: ${CMAKE_CXX_COMPILE_FEATURES}")