Home > Article > Backend Development > How to Detect C 11 Compiler Support in CMake?
In this guide, we explore methods to detect automatically if a compiler supports C 11 within CMake, providing a comprehensive analysis of both the latest and previous CMake versions.
CMake version 3.1.0 introduced a powerful feature: detecting the C features supported by a compiler. This is achieved through the cmake_minimum_required command:
<code class="cmake">cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)</code>
By specifying the minimum CMake version, you gain access to the CMAKE_CXX_COMPILE_FEATURES variable, which lists all supported C features. This enables you to determine the C standard to use in your project.
CMake allows you to explicitly set the C standard for a target using the CXX_STANDARD and CXX_STANDARD_REQUIRED properties. For example:
<code class="cmake">set_property(TARGET prog PROPERTY CXX_STANDARD 11) set_property(TARGET prog PROPERTY CXX_STANDARD_REQUIRED ON)</code>
This ensures that the compiler is invoked with the correct flags, such as -std=c 11.
Alternatively, you can specify the required C features using the target_compile_features command. From this list, CMake can deduce the appropriate C standard.
<code class="cmake">target_compile_features(foobar PRIVATE cxx_strong_enums cxx_constexpr cxx_auto_type)</code>
Get the list of supported C features using CMAKE_CXX_KNOWN_FEATURES.
CMake provides multiple ways to detect C 11 compiler support and specify the C standard. This flexibility allows for tailored C project configurations, ensuring compatibility and seamless compilation.
The above is the detailed content of How to Detect C 11 Compiler Support in CMake?. For more information, please follow other related articles on the PHP Chinese website!