cmake will not compile to C++ 11 standard

CMake add support for CXX_STANDARD and CXX_STANDARD_REQUIRED properties on 3.1 version. CXX_STANDARD: Take one of CMAKE_CXX_STANDARD values and they are 98, 11 and 14. If you pass CXX_STANDARD 11 and you compiler do not support c++11 CXX_STANDARD become 98 automatically and cmake do not give you any error if CXX_STANDARD_REQUIRED is OFF or unset. If your set CXX_STANDARD_REQUIRED "ON" CXX_STANDARD specific value become as a required property to build and cmake handle this.

In orde to use CHECK_CXX_COMPILER_FLAG you nedd to include CheckCXXCompilerFlag module:

include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
if(COMPILER_SUPPORTS_CXX11)
    message(STATUS "${COMPILER_SUPPORTS_CXX11}")
else(COMPILER_SUPPORTS_CXX11)
    message(FATAL_ERROR "${COMPILER_SUPPORTS_CXX11}")
endif(COMPILER_SUPPORTS_CXX11)

If you have a old cmake you need handle complicate and not portable flags from compilers sush as:

function(suported_compilers)
  if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
    execute_process(
      COMMAND ${CMAKE_CXX_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION)
    if(NOT(GCC_VERSION VERSION_GREATER 4.7 OR GCC_VERSION VERSION_EQUAL 4.7))
      message(FATAL_ERROR "${PROJECT_NAME} requires g++ 4.7 or greater.")
    endif(NOT(GCC_VERSION VERSION_GREATER 4.7 OR GCC_VERSION VERSION_EQUAL 4.7))
  elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
  else()
    message(FATAL_ERROR "Your compiler is supported in the build set?, please "
                  "contact the maintainer.")
  endif()
endfunction(suported_compilers)
function(set_targets_compilers_flags target_list)
  suported_compilers()
  foreach(tg ${target_list})
    if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
      set_target_properties(${tg} PROPERTIES COMPILE_FLAGS "-g -std=c++14 -Wall -Wextra -Werror")
    elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
      set_target_properties(${tg} PROPERTIES COMPILE_FLAGS "/W4 /WX /EHsc")
    endif()
  endforeach()
endfunction(set_targets_compilers_flags)

In CMake versions earlier than 3.1, we use

 add_compile_options(-std=c++11) # CMake 2.8.12 or newer

to add compile options to the compiler call as described in the CMake Docs.

That's propably not as portable as the one in Alvaro's answer, but it's more readable and since you are on you RasPi, I guess, GCC and Clang as target compilers will do.

Edit: For the sake of completeness: In CMake version 3.1 and newer, if you want to force C++11, you need the following lines:

set(CMAKE_CXX_STANDARD 11) # C++11...
set(CMAKE_CXX_STANDARD_REQUIRED ON) #...is required...
set(CMAKE_CXX_EXTENSIONS OFF) #...without compiler extensions like gnu++11

This enables the options for all targets following this declaration during compilation. If you want to control this more fine-grained, see Alvaro's answer or the CMake Docs of set_taget_properties(), which then looks something like this:

set_target_properties(myTarget PROPERTIES
    CXX_STANDARD 11
    CXX_STANDARD_REQUIRED ON
    CXX_EXTENSIONS OFF
)

Edit: But beware that C++11 support in GCC 4 is not complete and there might be things that behave differently from the defined standard.


Since the current cmake release is 3.10, I thought it may be appropriate to identify the newer method. While the suggestion to use add_compiler_

For anyone looking here at a more modern version of cmake (3.1+), The most appropriate answer is not to identify a version of a given compiler but to let CMAKE know what features need to be available.

target_compile_features(engine
    PRIVATE cxx_range_for
    )

Tags:

C++

C++11

Cmake