Skip to content
Mayeul d'Avezac edited this page Nov 24, 2015 · 4 revisions

Checking for specific features

Look for some c++11 features. Uses a script modified from here. Usage is given below.

# First need to enable c++
enable_language(CXX)

# Tell cmake to look into GreatCMakeCookOff for recipes
list(APPEND CMAKE_MODULE_PATH Path/to/cookoff)
# Adds flags to compiler to launch it into c++11 land
include(AddCPP11Flags)

# The following will print out all available features.
include(CheckCXX11Features)
cxx11_find_all_features(ALL_FEATURES)
message(STATUS "[c++11] features we can check for: ${ALL_FEATURES}")

# The following checks for all features
cxx11_feature_check()

# An internal value is set if a particular feature exists.
if(HAS_CXX11_AUTO)
  message(STATUS "[c++11] has auto.")
endif()
if(HAS_CXX11_LAMBDA)
  message(STATUS "[c++11] has lambda.")
endif()

Alternatively, only a subset of features can be checked for, and some can be required:

cxx11_feature_check(auto lambda REQUIRED long_long share_ptr variadic_templates)

The previous statement will fail if long long, std::shared_ptr<...>, and variadic templates are not available. It will also check for the availability of auto and lambda, but without failing.

Figuring out flags for some compilers

The script checks the existence of a few flags to enable c++11 features on different compilers. The output is somewhat verbose, but it seems to do the job for gcc, darwin-gcc, and microsoft visual studio. In addition, the intel compilers have to be told to use an external c++11 standard library. This script cannot figure where this library would be (hint: g++ provides it), so that is left up to the user. The script can be activated with a one liner.

include("path/to/cookoff/AddCPP11Flags.cmake")

NOTE: The extra flags are added to CMAKE_CXX_FLAGS and stored in CXX11_FLAGS.

NOTE: On windows + visual studio, disables warnings 4251 and ups fake variadic templates to 10.