Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if a c++11 feature is enabled in compiler with CMAKE

Tags:

c++

c++11

cmake

I'm developing a project with CMake. My code contains constexpr methods, that are allowed in Visual Studio 2015, but not in Visual Studio 2013.

How can I check in the CMakeLists.txt if the feature is supported by the specified compiler? I've seen in CMake documentation CMAKE_CXX_KNOWN_FEATURES, but I didn't understand how to use it.

like image 678
Jepessen Avatar asked Dec 19 '16 10:12

Jepessen


People also ask

How do I know if G ++ supports C ++ 11?

To see if your compiler has C++11 support, run it with just the --version option to get a print out of the version number. Do this for whichever compiler(s) you wish to use with Rosetta. Acceptable versions: GCC/g++: Version 4.8 or later.

How does CMake check compiler?

Inspecting the Default Compiler. When CMake is invoked to read a configuration file, temporary files are generated, including a cache and a CMakeFiles directory containing information specific to the environment. CMake uses this information to select the most appropriate compiler for the project.

What version of C++ does CMake use?

New in version 3.1. The C++ standard whose features are requested to build this target.

Does CMake contain a compiler?

About CMake. CMake is an extensible, open-source system that manages the build process in an operating system and in a compiler-independent manner.


1 Answers

You can use target_compile_features to require a C++11(/14/17) feature:

target_compile_features(target PRIVATE|PUBLIC|INTERFACE feature1 [feature2 ...])

With feature1 being a feature listed in CMAKE_CXX_KNOWN_FEATURES. For example, if you want to use constexpr in your public API, you can use:

add_library(foo ...)
target_compile_features(foo PUBLIC cxx_constexpr)

You should also take a look at the WriteCompilerDetectionHeader module which allows to detect features as options, and provides a backward compatibility implementation for some features if the compiler does not support them:

write_compiler_detection_header(
    FILE foo_compiler_detection.h
    PREFIX FOO
    COMPILERS GNU MSVC
    FEATURES cxx_constexpr cxx_nullptr
)

Here a file foo_compiler_detection.h will be generated with FOO_COMPILER_CXX_CONSTEXPR defined if the keyword constexpr is available:

#include "foo_compiler_detection.h"

#if FOO_COMPILER_CXX_CONSTEXPR

// implementation with constexpr available
constexpr int bar = 0;

#else

// implementation with constexpr not available
const int bar = 0;

#endif

Moreover, FOO_CONSTEXPR will be defined and will expand to constexpr if the feature exists for the current compiler. It will be empty otherwise.

FOO_NULLPTR will be defined and will expand to nullptr if the feature exists for the current compiler. It will expand to a compatibility implementation otherwise (e.g. NULL).

#include "foo_compiler_detection.h"

FOO_CONSTEXPR int bar = 0;

void baz(int* p = FOO_NULLPTR);

See CMake documentation.

like image 151
rgmt Avatar answered Oct 21 '22 10:10

rgmt