"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Detect C++11 Compiler Support in CMake?

How to Detect C++11 Compiler Support in CMake?

Published on 2024-11-06
Browse:682

How to Detect C  11 Compiler Support in CMake?

Detection of C 11 Compiler Support in CMake

Overview

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 3.1.0 and Later

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:

cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)

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.

Specifying C Standard Explicitly

CMake allows you to explicitly set the C standard for a target using the CXX_STANDARD and CXX_STANDARD_REQUIRED properties. For example:

set_property(TARGET prog PROPERTY CXX_STANDARD 11)
set_property(TARGET prog PROPERTY CXX_STANDARD_REQUIRED ON)

This ensures that the compiler is invoked with the correct flags, such as -std=c 11.

Specify Required C Features

Alternatively, you can specify the required C features using the target_compile_features command. From this list, CMake can deduce the appropriate C standard.

target_compile_features(foobar PRIVATE cxx_strong_enums cxx_constexpr cxx_auto_type)

Get the list of supported C features using CMAKE_CXX_KNOWN_FEATURES.

Conclusion

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.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3