When I try to run a CMake generated makefile to compile my program, I get the error that
range based for loops are not supported in C++ 98 mode.
I tried adding add_definitions(-std=c++0x)
to my CMakeLists.txt
, but it did not help.
I tried this too:
if(CMAKE_COMPILER_IS_GNUCXX) add_definitions(-std=gnu++0x) endif()
When I do g++ --version
, I get:
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
I have also tried SET(CMAKE_CXX_FLAGS "-std=c++0x")
, which also does not work.
I do not understand how I can activate C++ 11 features using CMake.
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.
In the C/C++ ecosystem, the best tool for project configuration is CMake. CMake allows you to specify the build of a project, in files named CMakeLists. txt, with a simple syntax (much simpler than writing Makefiles).
It is used in conjunction with native build environments such as Make, Qt Creator, Ninja, Android Studio, Apple's Xcode, and Microsoft Visual Studio. It has minimal dependencies, requiring only a C++ compiler on its own build system. CMake is distributed as open-source software under a permissive BSD-3-Clause license.
CMake is an open-source, cross-platform tool that uses compiler and platform independent configuration files to generate native build tool files specific to your compiler and platform. The CMake Tools extension integrates Visual Studio Code and CMake to make it easy to configure, build, and debug your C++ project.
CMake 3.1 introduced the CMAKE_CXX_STANDARD variable that you can use. If you know that you will always have CMake 3.1 or later available, you can just write this in your top-level CMakeLists.txt file, or put it right before any new target is defined:
set (CMAKE_CXX_STANDARD 11)
If you need to support older versions of CMake, here is a macro I came up with that you can use:
macro(use_cxx11) if (CMAKE_VERSION VERSION_LESS "3.1") if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11") endif () else () set (CMAKE_CXX_STANDARD 11) endif () endmacro(use_cxx11)
The macro only supports GCC right now, but it should be straight-forward to expand it to other compilers.
Then you could write use_cxx11()
at the top of any CMakeLists.txt file that defines a target that uses C++11.
If you are using CMake and clang to target macOS there is a bug that can cause the CMAKE_CXX_STANDARD
feature to simply not work (not add any compiler flags). Make sure that you do one of the following things:
Use cmake_minimum_required to require CMake 3.0 or later, or
Set policy CMP0025 to NEW with the following code at the top of your CMakeLists.txt file before the project
command:
# Fix behavior of CMAKE_CXX_STANDARD when targeting macOS. if (POLICY CMP0025) cmake_policy(SET CMP0025 NEW) endif ()
The CMake command target_compile_features()
is used to specify the required C++ feature cxx_range_for
. CMake will then induce the C++ standard to be used.
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR) project(foobar CXX) add_executable(foobar main.cc) target_compile_features(foobar PRIVATE cxx_range_for)
There is no need to use add_definitions(-std=c++11)
or to modify the CMake variable CMAKE_CXX_FLAGS
, because CMake will make sure the C++ compiler is invoked with the appropriate command line flags.
Maybe your C++ program uses other C++ features than cxx_range_for
. The CMake global property CMAKE_CXX_KNOWN_FEATURES
lists the C++ features you can choose from.
Instead of using target_compile_features()
you can also specify the C++ standard explicitly by setting the CMake properties CXX_STANDARD
and CXX_STANDARD_REQUIRED
for your CMake target.
See also my more detailed answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With