Home >Backend Development >C++ >How can I detect C 11 support in a compiler using CMake?
CMake version 3.1.0 onward provides CMAKE_CXX_COMPILE_FEATURES to identify C features supported by the compiler.
Set CXX_STANDARD and CXX_STANDARD_REQUIRED target properties to specify the desired standard:
<code class="cmake">add_executable(prog main.cc) set_property(TARGET prog PROPERTY CXX_STANDARD 11) set_property(TARGET prog PROPERTY CXX_STANDARD_REQUIRED ON)</code>
Use target_compile_features to specify specific C features and CMake will deduce the appropriate standard:
<code class="cmake">project(foobar CXX) add_executable(foobar main.cc) set(needed_features # Specify the required C++ features used in the program cxx_strong_enums cxx_constexpr cxx_auto_type) target_compile_features(foobar PRIVATE ${needed_features})</code>
The following code lists the C features supported by your CMake version:
<code class="cmake">cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR) get_property(known_features GLOBAL PROPERTY CMAKE_CXX_KNOWN_FEATURES) foreach(i ${known_features}) message("${i}") endforeach()</code>
The above is the detailed content of How can I detect C 11 support in a compiler using CMake?. For more information, please follow other related articles on the PHP Chinese website!