Home > Backend Development > C++ > body text

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

Barbara Streisand
Release: 2024-11-01 08:10:30
Original
267 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!