Home >Backend Development >C++ >How can I detect C 11 support in a compiler using CMake?

How can I detect C 11 support in a compiler using CMake?

Barbara Streisand
Barbara StreisandOriginal
2024-11-01 08:10:30333browse

How can I detect C  11 support in a compiler using CMake?

Detecting C 11 Support in a Compiler with CMake

Identifying Compiler Support

CMake version 3.1.0 onward provides CMAKE_CXX_COMPILE_FEATURES to identify C features supported by the compiler.

Specifying C Standard Explicitly

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>

Specifying Required C Features

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>

Supported C Features

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn