How to activate C++ 11 in CMake?
When I try to run 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.
c++11 cmake
add a comment |
When I try to run 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.
c++11 cmake
11
TheSET(CMAKE_CXX_FLAGS "-std=c++0x")
works fine for me, so there is probably a problem somewhere else in the CMakeLists file. Make sure you don't accidentally overwrite the contents of CMAKE_CXX_FLAGS later on.
– ComicSansMS
Jun 1 '12 at 14:11
7
add_definitions(-std=c++11) works for me with CMake 2.8.8
– kyku
Jun 2 '12 at 8:49
@ComicSansMS: You are totally right! I overwrote it, which was my own mistake. I have corrected it, and now it is working fine! C++11 stuff is very cool! I wanted to loop on a vector of structures, which would require iterator and needless coding noise if I did not have range based for loops. I guess I could use BOOST_FOREACH though, but oh well...
– TheBicentennialMan
Jun 3 '12 at 10:31
24
For CMake ≥3.1,set(CMAKE_CXX_STANDARD 11)
(before defining the target) is the best way.
– emlai
Jun 13 '15 at 2:26
@tuple_cat You can do it target-based as well. But be aware thatCXX_STANDARD
does not work on MSVC, so basically you have to fall back totarget_compile_features
if you want something that works cross-platform.
– Ela782
Dec 9 '16 at 20:21
add a comment |
When I try to run 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.
c++11 cmake
When I try to run 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.
c++11 cmake
c++11 cmake
edited Nov 13 '16 at 4:37
nbro
5,69185096
5,69185096
asked Jun 1 '12 at 13:31
TheBicentennialManTheBicentennialMan
2,83762541
2,83762541
11
TheSET(CMAKE_CXX_FLAGS "-std=c++0x")
works fine for me, so there is probably a problem somewhere else in the CMakeLists file. Make sure you don't accidentally overwrite the contents of CMAKE_CXX_FLAGS later on.
– ComicSansMS
Jun 1 '12 at 14:11
7
add_definitions(-std=c++11) works for me with CMake 2.8.8
– kyku
Jun 2 '12 at 8:49
@ComicSansMS: You are totally right! I overwrote it, which was my own mistake. I have corrected it, and now it is working fine! C++11 stuff is very cool! I wanted to loop on a vector of structures, which would require iterator and needless coding noise if I did not have range based for loops. I guess I could use BOOST_FOREACH though, but oh well...
– TheBicentennialMan
Jun 3 '12 at 10:31
24
For CMake ≥3.1,set(CMAKE_CXX_STANDARD 11)
(before defining the target) is the best way.
– emlai
Jun 13 '15 at 2:26
@tuple_cat You can do it target-based as well. But be aware thatCXX_STANDARD
does not work on MSVC, so basically you have to fall back totarget_compile_features
if you want something that works cross-platform.
– Ela782
Dec 9 '16 at 20:21
add a comment |
11
TheSET(CMAKE_CXX_FLAGS "-std=c++0x")
works fine for me, so there is probably a problem somewhere else in the CMakeLists file. Make sure you don't accidentally overwrite the contents of CMAKE_CXX_FLAGS later on.
– ComicSansMS
Jun 1 '12 at 14:11
7
add_definitions(-std=c++11) works for me with CMake 2.8.8
– kyku
Jun 2 '12 at 8:49
@ComicSansMS: You are totally right! I overwrote it, which was my own mistake. I have corrected it, and now it is working fine! C++11 stuff is very cool! I wanted to loop on a vector of structures, which would require iterator and needless coding noise if I did not have range based for loops. I guess I could use BOOST_FOREACH though, but oh well...
– TheBicentennialMan
Jun 3 '12 at 10:31
24
For CMake ≥3.1,set(CMAKE_CXX_STANDARD 11)
(before defining the target) is the best way.
– emlai
Jun 13 '15 at 2:26
@tuple_cat You can do it target-based as well. But be aware thatCXX_STANDARD
does not work on MSVC, so basically you have to fall back totarget_compile_features
if you want something that works cross-platform.
– Ela782
Dec 9 '16 at 20:21
11
11
The
SET(CMAKE_CXX_FLAGS "-std=c++0x")
works fine for me, so there is probably a problem somewhere else in the CMakeLists file. Make sure you don't accidentally overwrite the contents of CMAKE_CXX_FLAGS later on.– ComicSansMS
Jun 1 '12 at 14:11
The
SET(CMAKE_CXX_FLAGS "-std=c++0x")
works fine for me, so there is probably a problem somewhere else in the CMakeLists file. Make sure you don't accidentally overwrite the contents of CMAKE_CXX_FLAGS later on.– ComicSansMS
Jun 1 '12 at 14:11
7
7
add_definitions(-std=c++11) works for me with CMake 2.8.8
– kyku
Jun 2 '12 at 8:49
add_definitions(-std=c++11) works for me with CMake 2.8.8
– kyku
Jun 2 '12 at 8:49
@ComicSansMS: You are totally right! I overwrote it, which was my own mistake. I have corrected it, and now it is working fine! C++11 stuff is very cool! I wanted to loop on a vector of structures, which would require iterator and needless coding noise if I did not have range based for loops. I guess I could use BOOST_FOREACH though, but oh well...
– TheBicentennialMan
Jun 3 '12 at 10:31
@ComicSansMS: You are totally right! I overwrote it, which was my own mistake. I have corrected it, and now it is working fine! C++11 stuff is very cool! I wanted to loop on a vector of structures, which would require iterator and needless coding noise if I did not have range based for loops. I guess I could use BOOST_FOREACH though, but oh well...
– TheBicentennialMan
Jun 3 '12 at 10:31
24
24
For CMake ≥3.1,
set(CMAKE_CXX_STANDARD 11)
(before defining the target) is the best way.– emlai
Jun 13 '15 at 2:26
For CMake ≥3.1,
set(CMAKE_CXX_STANDARD 11)
(before defining the target) is the best way.– emlai
Jun 13 '15 at 2:26
@tuple_cat You can do it target-based as well. But be aware that
CXX_STANDARD
does not work on MSVC, so basically you have to fall back to target_compile_features
if you want something that works cross-platform.– Ela782
Dec 9 '16 at 20:21
@tuple_cat You can do it target-based as well. But be aware that
CXX_STANDARD
does not work on MSVC, so basically you have to fall back to target_compile_features
if you want something that works cross-platform.– Ela782
Dec 9 '16 at 20:21
add a comment |
13 Answers
13
active
oldest
votes
CMake 3.1 introduced the CMAKE_CXX_STANDARD variable that you can use. If you know that you will always have CMake 3.1 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.
CMake issue #15943 for clang users targeting macOS
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 ()
9
This should be the accepted answer. It does not require touching each target's properties individually, and works cross-platform.
– DevSolar
Jan 20 '16 at 12:49
3
Agreed, this should be the accepted answer as of CMake 3.1+. It doesn't seem to work for me on the mac though. You can force it to give you --std=c++11 with --stdlib=libc++, the default on the Mac. Instead CMAKE_CXX_STANDARD always includes the gnu extensions if they are supported, and the result doesn't seem to build against --stdlib=libc++. Instead you have to switch to gnu's --stdlib=libstdc++. The Mac is the special case though. For Linux, choosing gnu++11 with libstdc++ is the norm. Of course, that is easily corrected with a if(APPLE) add_compile_options() to tack on the flags.
– Atifm
May 23 '16 at 18:40
2
One problem of this approach is thatgnu++11
is enforced, even when these variables are definedset(CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION clang)
set(CMAKE_ANDROID_STL_TYPE c++_static)
. For me the only viable way was the classicset (CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
– Antonio
May 31 '17 at 10:11
1
man gcc says -std=... not --std=... (one dash, not two).
– GroovyCakes
Aug 2 '17 at 22:00
2
@Unapiedra: It's a CMake bug but you can work around it, see gitlab.kitware.com/cmake/cmake/issues/15943
– David Grayson
Nov 1 '17 at 15:34
|
show 6 more comments
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.
3
It seems the edit from today is misleading. CMake 3.0.0 does not contain target_compile_features. Correct me if I'm wrong. I think the command is only present in the nightly builds of CMake.
– Erik Sjölund
Sep 14 '14 at 22:16
4
I'd say this is the most accurate answer
– Michał Walenciak
Jan 7 '15 at 13:38
8
I think this is how it is supposed to do. The other answers just manually add flags and therefore introduce incompatibilities. However this seems to be only available in CMake 3.1+
– Uli Köhler
Mar 14 '15 at 18:32
2
@UliKöhler this is actually still not available, and may possibly turn up for some compilers in 3.2. Don't use this method in the short term; its completely not portable.
– Doug
Mar 20 '15 at 3:37
2
Any idea how to do this in CMake 2.6?
– nuzzolilo
Dec 31 '15 at 3:29
|
show 3 more comments
I am using
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
But if you want to play with C++11
, g++ 4.6.1
is pretty old.
Try to get a newer g++
version.
4
This is, for me, the only right and nice answer to this question, with current (rolled out) cmake on most recent Linux' using g++.
– Patrick B.
Oct 19 '14 at 20:18
1
Copied and pasted this and it worked perfectly. I am on Cygwin using CMAKE 2.8.9. I know about most of the approaches I'm reading here because I follow the CMAKE mailing list and I've ported WebKit to a variety of compilers. The thing we had done for WebKit ports was to install CMake 2.8.12. However because I know Cygwin's CMAKE is old, I wanted something that applied to that version. (Not porting WebKit to Cygwin, sorry)
– cardiff space man
Jan 22 '15 at 0:28
Great, this is a drop-in for old CMake and g++ 4.6 (and future-proof). I also upvoted theCXX_STANDARD
-based answers, but this was the only answer useful in my situation.
– Tomasz Gandor
Jan 30 '15 at 14:20
This is exactly what I was looking for, however it's not clear what is the minimum cmake version needed to use that module. cmake --help-module doesn't help much about it.
– Jan Segre
Mar 31 '15 at 18:12
1
@JanSegre the module first appeared in 2.4, cmake.org/…
– KoKuToru
Apr 1 '15 at 6:00
|
show 1 more comment
The easiest way to set the Cxx standard is:
set_property(TARGET tgt PROPERTY CXX_STANDARD 11)
See the CMake documentation for more details.
2
Yes, this definitely looks like one of the best ways to do it in modern CMake (3.1+)
– Erbureth
May 18 '15 at 21:47
12
Or you can justset(CMAKE_CXX_STANDARD 11)
to define the default property for all targets created after that.
– emlai
Jun 13 '15 at 2:14
1
Does this work in CMake 2.8?
– einpoklum
Nov 19 '15 at 15:48
This is also the solution you need if you want to set different C++ standards on different targets, because theset
command suggested by @emlai is global and affects all subsequent targets.
– Elliott Slaughter
Oct 18 '18 at 21:53
add a comment |
As it turns out, SET(CMAKE_CXX_FLAGS "-std=c++0x")
does activate many C++11 features. The reason it did not work was that the statement looked like this:
set(CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS} -g -ftest-coverage -fprofile-arcs")
Following this approach, somehow the -std=c++0x
flag was overwritten and it did not work. Setting the flags one by one or using a list method is working.
list( APPEND CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS} -g -ftest-coverage -fprofile-arcs")
36
I always just use: SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") # for gcc >= 4.7, or c++0x for 4.6
– David Doria
Sep 21 '12 at 16:42
I once did a little script for that (not complete though): github.com/Morwenn/POLDER/blob/master/cmake/set_cxx_norm.cmake
– Morwenn
Feb 15 '13 at 14:08
9
-1. If you specify any CMAKE_CXX_FLAGS from the command line, the second method will produce a semicolon in the build command (and repeat the original CMAKE_CXX_FLAGS twice).
– Nikolai
May 29 '13 at 14:26
instead of manually adding the -g flag you should set the CMAKE_BUILD_TYPE variable to debug: voices.canonical.com/jussi.pakkanen/2013/03/26/…
– bames53
Nov 24 '14 at 20:52
1
TheCXX_STANDARD
property is better as it is compiler-agnostic.
– jaskmar
Aug 14 '18 at 10:13
add a comment |
The easiest way:
add_compile_options(-std=c++11)
5
Only available from cmake 3.0
– Raúl Salinas-Monteagudo
Mar 10 '16 at 12:36
4
This causes warning and errors when compiling C files in the same project.
– rosewater
Jun 10 '16 at 16:10
add a comment |
This is another way of enabling C++11 support,
ADD_DEFINITIONS(
-std=c++11 # Or -std=c++0x
# Other flags
)
I have encountered instances where only this method works and other methods fail. Maybe it has something to do with the latest version of CMake.
9
That will only work if you are ONLY using C++ compiler. If you're also using the CC compiler it will fail.
– Emmanuel
Nov 13 '13 at 16:45
5
add_definitions
is only supposed to used for adding DEFINITIONS, i.e. -D SOMETHING. And as @Emmanuel said, it does not work in many cases.
– xuhdev
Nov 7 '14 at 0:26
I used that before but had problems when I included a C file becauseadd_definitions
was not made for setting flags.
– Jan Segre
Mar 31 '15 at 18:15
add a comment |
For CMake 3.8 and newer you can use
target_compile_features(target PUBLIC cxx_std_11)
1
this is the way recommended of latest version of CMake
– camino
May 19 '18 at 17:36
What is the function ofPUBLIC
here?
– Rotsiser Mho
Jun 12 '18 at 3:16
2
@RotsiserMhoPUBLIC
means that other targets that depend on your target will use C++11 as well. For example if your target is a library then all targets that link against your library withtarget_link_libraries
will be compiled with C++11 support.
– eyelash
Jun 12 '18 at 7:05
add a comment |
On modern CMake (>= 3.1) best way to set global requirement is:
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
It translates to "I want C++11 for all targets, it's not optional, I dont't want to use any GNU or MS extensions."
As of c++17, this still is IMHO the best way.
Source:
https://crascit.com/2015/03/28/enabling-cxx11-in-cmake/
This way is not ideal for newest versions of C++ if you don't have the latest CMake. E.g. you can't enable C++2a this way until CMake 3.12.
– Ruslan
Jan 29 at 11:41
add a comment |
What works for me is to set the following line in your CMakeLists.txt:
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
Setting this command activates the C++11 features for the compiler and after executing the cmake ..
command, you should be able to use range based for loops
in your code and compile it without any errors.
This is in the end the best answer if you want exactly-std=c++11
, asset (CMAKE_CXX_STANDARD 11)
will use the flag-std=gnu++11
, which might be undesirable.
– Antonio
May 31 '17 at 10:08
1
@Antonioset (CMAKE_CXX_EXTENSIONS OFF)
– mloskot
Aug 29 '17 at 15:14
add a comment |
I think just these two lines are enough.
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
1
This makes sense when all targets in a project use the same C++ standard (all compiled libraries and executables use C++11, for example). Otherwise, the Cmaketarget_compile_features
function, applied for each individual target, as shown in other answers, is a more recommended approach.
– Allan
Aug 6 '18 at 10:45
add a comment |
In case you want to activate always the latest C++ standard, here's my extension of David Grayson's answer, in light of the recent (CMake 3.8, CMake 3.11) additions of values of 17 and 20 for CMAKE_CXX_STANDARD):
IF (CMAKE_VERSION VERSION_LESS "3.8")
SET(CMAKE_CXX_STANDARD 14)
ELSEIF (CMAKE_VERSION VERSION_LESS "3.11")
SET(CMAKE_CXX_STANDARD 17)
ELSE() # CMake 3.11 or higher:
SET(CMAKE_CXX_STANDARD 20)
ENDIF()
# Typically, you'll also want to turn off compiler-specific extensions:
SET(CMAKE_CXX_EXTENSIONS OFF)
(use that code in the place of set (CMAKE_CXX_STANDARD 11)
in the linked anwswer).
add a comment |
OS X and Homebrew LLVM related:
Don't forget to call cmake_minimum_required(VERSION 3.3) and project() after it!
Or CMake will insert project()
implicitly before line 1, causing trouble with Clang version detection and possibly other sorts of troubles. Here is a related issue.
1
The question is not OSX or LVVM related, specifically. Also, there's no reason to require CMake v3.x for C++11 development. As for clang version detection - that's not what OP asked about.
– einpoklum
Jun 3 '16 at 19:52
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10851247%2fhow-to-activate-c-11-in-cmake%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
13 Answers
13
active
oldest
votes
13 Answers
13
active
oldest
votes
active
oldest
votes
active
oldest
votes
CMake 3.1 introduced the CMAKE_CXX_STANDARD variable that you can use. If you know that you will always have CMake 3.1 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.
CMake issue #15943 for clang users targeting macOS
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 ()
9
This should be the accepted answer. It does not require touching each target's properties individually, and works cross-platform.
– DevSolar
Jan 20 '16 at 12:49
3
Agreed, this should be the accepted answer as of CMake 3.1+. It doesn't seem to work for me on the mac though. You can force it to give you --std=c++11 with --stdlib=libc++, the default on the Mac. Instead CMAKE_CXX_STANDARD always includes the gnu extensions if they are supported, and the result doesn't seem to build against --stdlib=libc++. Instead you have to switch to gnu's --stdlib=libstdc++. The Mac is the special case though. For Linux, choosing gnu++11 with libstdc++ is the norm. Of course, that is easily corrected with a if(APPLE) add_compile_options() to tack on the flags.
– Atifm
May 23 '16 at 18:40
2
One problem of this approach is thatgnu++11
is enforced, even when these variables are definedset(CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION clang)
set(CMAKE_ANDROID_STL_TYPE c++_static)
. For me the only viable way was the classicset (CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
– Antonio
May 31 '17 at 10:11
1
man gcc says -std=... not --std=... (one dash, not two).
– GroovyCakes
Aug 2 '17 at 22:00
2
@Unapiedra: It's a CMake bug but you can work around it, see gitlab.kitware.com/cmake/cmake/issues/15943
– David Grayson
Nov 1 '17 at 15:34
|
show 6 more comments
CMake 3.1 introduced the CMAKE_CXX_STANDARD variable that you can use. If you know that you will always have CMake 3.1 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.
CMake issue #15943 for clang users targeting macOS
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 ()
9
This should be the accepted answer. It does not require touching each target's properties individually, and works cross-platform.
– DevSolar
Jan 20 '16 at 12:49
3
Agreed, this should be the accepted answer as of CMake 3.1+. It doesn't seem to work for me on the mac though. You can force it to give you --std=c++11 with --stdlib=libc++, the default on the Mac. Instead CMAKE_CXX_STANDARD always includes the gnu extensions if they are supported, and the result doesn't seem to build against --stdlib=libc++. Instead you have to switch to gnu's --stdlib=libstdc++. The Mac is the special case though. For Linux, choosing gnu++11 with libstdc++ is the norm. Of course, that is easily corrected with a if(APPLE) add_compile_options() to tack on the flags.
– Atifm
May 23 '16 at 18:40
2
One problem of this approach is thatgnu++11
is enforced, even when these variables are definedset(CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION clang)
set(CMAKE_ANDROID_STL_TYPE c++_static)
. For me the only viable way was the classicset (CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
– Antonio
May 31 '17 at 10:11
1
man gcc says -std=... not --std=... (one dash, not two).
– GroovyCakes
Aug 2 '17 at 22:00
2
@Unapiedra: It's a CMake bug but you can work around it, see gitlab.kitware.com/cmake/cmake/issues/15943
– David Grayson
Nov 1 '17 at 15:34
|
show 6 more comments
CMake 3.1 introduced the CMAKE_CXX_STANDARD variable that you can use. If you know that you will always have CMake 3.1 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.
CMake issue #15943 for clang users targeting macOS
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 ()
CMake 3.1 introduced the CMAKE_CXX_STANDARD variable that you can use. If you know that you will always have CMake 3.1 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.
CMake issue #15943 for clang users targeting macOS
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 ()
edited Dec 29 '17 at 18:30
answered Jun 23 '15 at 18:05
David GraysonDavid Grayson
57.8k19106149
57.8k19106149
9
This should be the accepted answer. It does not require touching each target's properties individually, and works cross-platform.
– DevSolar
Jan 20 '16 at 12:49
3
Agreed, this should be the accepted answer as of CMake 3.1+. It doesn't seem to work for me on the mac though. You can force it to give you --std=c++11 with --stdlib=libc++, the default on the Mac. Instead CMAKE_CXX_STANDARD always includes the gnu extensions if they are supported, and the result doesn't seem to build against --stdlib=libc++. Instead you have to switch to gnu's --stdlib=libstdc++. The Mac is the special case though. For Linux, choosing gnu++11 with libstdc++ is the norm. Of course, that is easily corrected with a if(APPLE) add_compile_options() to tack on the flags.
– Atifm
May 23 '16 at 18:40
2
One problem of this approach is thatgnu++11
is enforced, even when these variables are definedset(CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION clang)
set(CMAKE_ANDROID_STL_TYPE c++_static)
. For me the only viable way was the classicset (CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
– Antonio
May 31 '17 at 10:11
1
man gcc says -std=... not --std=... (one dash, not two).
– GroovyCakes
Aug 2 '17 at 22:00
2
@Unapiedra: It's a CMake bug but you can work around it, see gitlab.kitware.com/cmake/cmake/issues/15943
– David Grayson
Nov 1 '17 at 15:34
|
show 6 more comments
9
This should be the accepted answer. It does not require touching each target's properties individually, and works cross-platform.
– DevSolar
Jan 20 '16 at 12:49
3
Agreed, this should be the accepted answer as of CMake 3.1+. It doesn't seem to work for me on the mac though. You can force it to give you --std=c++11 with --stdlib=libc++, the default on the Mac. Instead CMAKE_CXX_STANDARD always includes the gnu extensions if they are supported, and the result doesn't seem to build against --stdlib=libc++. Instead you have to switch to gnu's --stdlib=libstdc++. The Mac is the special case though. For Linux, choosing gnu++11 with libstdc++ is the norm. Of course, that is easily corrected with a if(APPLE) add_compile_options() to tack on the flags.
– Atifm
May 23 '16 at 18:40
2
One problem of this approach is thatgnu++11
is enforced, even when these variables are definedset(CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION clang)
set(CMAKE_ANDROID_STL_TYPE c++_static)
. For me the only viable way was the classicset (CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
– Antonio
May 31 '17 at 10:11
1
man gcc says -std=... not --std=... (one dash, not two).
– GroovyCakes
Aug 2 '17 at 22:00
2
@Unapiedra: It's a CMake bug but you can work around it, see gitlab.kitware.com/cmake/cmake/issues/15943
– David Grayson
Nov 1 '17 at 15:34
9
9
This should be the accepted answer. It does not require touching each target's properties individually, and works cross-platform.
– DevSolar
Jan 20 '16 at 12:49
This should be the accepted answer. It does not require touching each target's properties individually, and works cross-platform.
– DevSolar
Jan 20 '16 at 12:49
3
3
Agreed, this should be the accepted answer as of CMake 3.1+. It doesn't seem to work for me on the mac though. You can force it to give you --std=c++11 with --stdlib=libc++, the default on the Mac. Instead CMAKE_CXX_STANDARD always includes the gnu extensions if they are supported, and the result doesn't seem to build against --stdlib=libc++. Instead you have to switch to gnu's --stdlib=libstdc++. The Mac is the special case though. For Linux, choosing gnu++11 with libstdc++ is the norm. Of course, that is easily corrected with a if(APPLE) add_compile_options() to tack on the flags.
– Atifm
May 23 '16 at 18:40
Agreed, this should be the accepted answer as of CMake 3.1+. It doesn't seem to work for me on the mac though. You can force it to give you --std=c++11 with --stdlib=libc++, the default on the Mac. Instead CMAKE_CXX_STANDARD always includes the gnu extensions if they are supported, and the result doesn't seem to build against --stdlib=libc++. Instead you have to switch to gnu's --stdlib=libstdc++. The Mac is the special case though. For Linux, choosing gnu++11 with libstdc++ is the norm. Of course, that is easily corrected with a if(APPLE) add_compile_options() to tack on the flags.
– Atifm
May 23 '16 at 18:40
2
2
One problem of this approach is that
gnu++11
is enforced, even when these variables are defined set(CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION clang)
set(CMAKE_ANDROID_STL_TYPE c++_static)
. For me the only viable way was the classic set (CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
– Antonio
May 31 '17 at 10:11
One problem of this approach is that
gnu++11
is enforced, even when these variables are defined set(CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION clang)
set(CMAKE_ANDROID_STL_TYPE c++_static)
. For me the only viable way was the classic set (CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
– Antonio
May 31 '17 at 10:11
1
1
man gcc says -std=... not --std=... (one dash, not two).
– GroovyCakes
Aug 2 '17 at 22:00
man gcc says -std=... not --std=... (one dash, not two).
– GroovyCakes
Aug 2 '17 at 22:00
2
2
@Unapiedra: It's a CMake bug but you can work around it, see gitlab.kitware.com/cmake/cmake/issues/15943
– David Grayson
Nov 1 '17 at 15:34
@Unapiedra: It's a CMake bug but you can work around it, see gitlab.kitware.com/cmake/cmake/issues/15943
– David Grayson
Nov 1 '17 at 15:34
|
show 6 more comments
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.
3
It seems the edit from today is misleading. CMake 3.0.0 does not contain target_compile_features. Correct me if I'm wrong. I think the command is only present in the nightly builds of CMake.
– Erik Sjölund
Sep 14 '14 at 22:16
4
I'd say this is the most accurate answer
– Michał Walenciak
Jan 7 '15 at 13:38
8
I think this is how it is supposed to do. The other answers just manually add flags and therefore introduce incompatibilities. However this seems to be only available in CMake 3.1+
– Uli Köhler
Mar 14 '15 at 18:32
2
@UliKöhler this is actually still not available, and may possibly turn up for some compilers in 3.2. Don't use this method in the short term; its completely not portable.
– Doug
Mar 20 '15 at 3:37
2
Any idea how to do this in CMake 2.6?
– nuzzolilo
Dec 31 '15 at 3:29
|
show 3 more comments
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.
3
It seems the edit from today is misleading. CMake 3.0.0 does not contain target_compile_features. Correct me if I'm wrong. I think the command is only present in the nightly builds of CMake.
– Erik Sjölund
Sep 14 '14 at 22:16
4
I'd say this is the most accurate answer
– Michał Walenciak
Jan 7 '15 at 13:38
8
I think this is how it is supposed to do. The other answers just manually add flags and therefore introduce incompatibilities. However this seems to be only available in CMake 3.1+
– Uli Köhler
Mar 14 '15 at 18:32
2
@UliKöhler this is actually still not available, and may possibly turn up for some compilers in 3.2. Don't use this method in the short term; its completely not portable.
– Doug
Mar 20 '15 at 3:37
2
Any idea how to do this in CMake 2.6?
– nuzzolilo
Dec 31 '15 at 3:29
|
show 3 more comments
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.
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.
edited May 23 '17 at 12:26
Community♦
11
11
answered Nov 23 '13 at 17:27
Erik SjölundErik Sjölund
6,80152649
6,80152649
3
It seems the edit from today is misleading. CMake 3.0.0 does not contain target_compile_features. Correct me if I'm wrong. I think the command is only present in the nightly builds of CMake.
– Erik Sjölund
Sep 14 '14 at 22:16
4
I'd say this is the most accurate answer
– Michał Walenciak
Jan 7 '15 at 13:38
8
I think this is how it is supposed to do. The other answers just manually add flags and therefore introduce incompatibilities. However this seems to be only available in CMake 3.1+
– Uli Köhler
Mar 14 '15 at 18:32
2
@UliKöhler this is actually still not available, and may possibly turn up for some compilers in 3.2. Don't use this method in the short term; its completely not portable.
– Doug
Mar 20 '15 at 3:37
2
Any idea how to do this in CMake 2.6?
– nuzzolilo
Dec 31 '15 at 3:29
|
show 3 more comments
3
It seems the edit from today is misleading. CMake 3.0.0 does not contain target_compile_features. Correct me if I'm wrong. I think the command is only present in the nightly builds of CMake.
– Erik Sjölund
Sep 14 '14 at 22:16
4
I'd say this is the most accurate answer
– Michał Walenciak
Jan 7 '15 at 13:38
8
I think this is how it is supposed to do. The other answers just manually add flags and therefore introduce incompatibilities. However this seems to be only available in CMake 3.1+
– Uli Köhler
Mar 14 '15 at 18:32
2
@UliKöhler this is actually still not available, and may possibly turn up for some compilers in 3.2. Don't use this method in the short term; its completely not portable.
– Doug
Mar 20 '15 at 3:37
2
Any idea how to do this in CMake 2.6?
– nuzzolilo
Dec 31 '15 at 3:29
3
3
It seems the edit from today is misleading. CMake 3.0.0 does not contain target_compile_features. Correct me if I'm wrong. I think the command is only present in the nightly builds of CMake.
– Erik Sjölund
Sep 14 '14 at 22:16
It seems the edit from today is misleading. CMake 3.0.0 does not contain target_compile_features. Correct me if I'm wrong. I think the command is only present in the nightly builds of CMake.
– Erik Sjölund
Sep 14 '14 at 22:16
4
4
I'd say this is the most accurate answer
– Michał Walenciak
Jan 7 '15 at 13:38
I'd say this is the most accurate answer
– Michał Walenciak
Jan 7 '15 at 13:38
8
8
I think this is how it is supposed to do. The other answers just manually add flags and therefore introduce incompatibilities. However this seems to be only available in CMake 3.1+
– Uli Köhler
Mar 14 '15 at 18:32
I think this is how it is supposed to do. The other answers just manually add flags and therefore introduce incompatibilities. However this seems to be only available in CMake 3.1+
– Uli Köhler
Mar 14 '15 at 18:32
2
2
@UliKöhler this is actually still not available, and may possibly turn up for some compilers in 3.2. Don't use this method in the short term; its completely not portable.
– Doug
Mar 20 '15 at 3:37
@UliKöhler this is actually still not available, and may possibly turn up for some compilers in 3.2. Don't use this method in the short term; its completely not portable.
– Doug
Mar 20 '15 at 3:37
2
2
Any idea how to do this in CMake 2.6?
– nuzzolilo
Dec 31 '15 at 3:29
Any idea how to do this in CMake 2.6?
– nuzzolilo
Dec 31 '15 at 3:29
|
show 3 more comments
I am using
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
But if you want to play with C++11
, g++ 4.6.1
is pretty old.
Try to get a newer g++
version.
4
This is, for me, the only right and nice answer to this question, with current (rolled out) cmake on most recent Linux' using g++.
– Patrick B.
Oct 19 '14 at 20:18
1
Copied and pasted this and it worked perfectly. I am on Cygwin using CMAKE 2.8.9. I know about most of the approaches I'm reading here because I follow the CMAKE mailing list and I've ported WebKit to a variety of compilers. The thing we had done for WebKit ports was to install CMake 2.8.12. However because I know Cygwin's CMAKE is old, I wanted something that applied to that version. (Not porting WebKit to Cygwin, sorry)
– cardiff space man
Jan 22 '15 at 0:28
Great, this is a drop-in for old CMake and g++ 4.6 (and future-proof). I also upvoted theCXX_STANDARD
-based answers, but this was the only answer useful in my situation.
– Tomasz Gandor
Jan 30 '15 at 14:20
This is exactly what I was looking for, however it's not clear what is the minimum cmake version needed to use that module. cmake --help-module doesn't help much about it.
– Jan Segre
Mar 31 '15 at 18:12
1
@JanSegre the module first appeared in 2.4, cmake.org/…
– KoKuToru
Apr 1 '15 at 6:00
|
show 1 more comment
I am using
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
But if you want to play with C++11
, g++ 4.6.1
is pretty old.
Try to get a newer g++
version.
4
This is, for me, the only right and nice answer to this question, with current (rolled out) cmake on most recent Linux' using g++.
– Patrick B.
Oct 19 '14 at 20:18
1
Copied and pasted this and it worked perfectly. I am on Cygwin using CMAKE 2.8.9. I know about most of the approaches I'm reading here because I follow the CMAKE mailing list and I've ported WebKit to a variety of compilers. The thing we had done for WebKit ports was to install CMake 2.8.12. However because I know Cygwin's CMAKE is old, I wanted something that applied to that version. (Not porting WebKit to Cygwin, sorry)
– cardiff space man
Jan 22 '15 at 0:28
Great, this is a drop-in for old CMake and g++ 4.6 (and future-proof). I also upvoted theCXX_STANDARD
-based answers, but this was the only answer useful in my situation.
– Tomasz Gandor
Jan 30 '15 at 14:20
This is exactly what I was looking for, however it's not clear what is the minimum cmake version needed to use that module. cmake --help-module doesn't help much about it.
– Jan Segre
Mar 31 '15 at 18:12
1
@JanSegre the module first appeared in 2.4, cmake.org/…
– KoKuToru
Apr 1 '15 at 6:00
|
show 1 more comment
I am using
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
But if you want to play with C++11
, g++ 4.6.1
is pretty old.
Try to get a newer g++
version.
I am using
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
But if you want to play with C++11
, g++ 4.6.1
is pretty old.
Try to get a newer g++
version.
answered Sep 14 '14 at 19:04
KoKuToruKoKuToru
3,73721518
3,73721518
4
This is, for me, the only right and nice answer to this question, with current (rolled out) cmake on most recent Linux' using g++.
– Patrick B.
Oct 19 '14 at 20:18
1
Copied and pasted this and it worked perfectly. I am on Cygwin using CMAKE 2.8.9. I know about most of the approaches I'm reading here because I follow the CMAKE mailing list and I've ported WebKit to a variety of compilers. The thing we had done for WebKit ports was to install CMake 2.8.12. However because I know Cygwin's CMAKE is old, I wanted something that applied to that version. (Not porting WebKit to Cygwin, sorry)
– cardiff space man
Jan 22 '15 at 0:28
Great, this is a drop-in for old CMake and g++ 4.6 (and future-proof). I also upvoted theCXX_STANDARD
-based answers, but this was the only answer useful in my situation.
– Tomasz Gandor
Jan 30 '15 at 14:20
This is exactly what I was looking for, however it's not clear what is the minimum cmake version needed to use that module. cmake --help-module doesn't help much about it.
– Jan Segre
Mar 31 '15 at 18:12
1
@JanSegre the module first appeared in 2.4, cmake.org/…
– KoKuToru
Apr 1 '15 at 6:00
|
show 1 more comment
4
This is, for me, the only right and nice answer to this question, with current (rolled out) cmake on most recent Linux' using g++.
– Patrick B.
Oct 19 '14 at 20:18
1
Copied and pasted this and it worked perfectly. I am on Cygwin using CMAKE 2.8.9. I know about most of the approaches I'm reading here because I follow the CMAKE mailing list and I've ported WebKit to a variety of compilers. The thing we had done for WebKit ports was to install CMake 2.8.12. However because I know Cygwin's CMAKE is old, I wanted something that applied to that version. (Not porting WebKit to Cygwin, sorry)
– cardiff space man
Jan 22 '15 at 0:28
Great, this is a drop-in for old CMake and g++ 4.6 (and future-proof). I also upvoted theCXX_STANDARD
-based answers, but this was the only answer useful in my situation.
– Tomasz Gandor
Jan 30 '15 at 14:20
This is exactly what I was looking for, however it's not clear what is the minimum cmake version needed to use that module. cmake --help-module doesn't help much about it.
– Jan Segre
Mar 31 '15 at 18:12
1
@JanSegre the module first appeared in 2.4, cmake.org/…
– KoKuToru
Apr 1 '15 at 6:00
4
4
This is, for me, the only right and nice answer to this question, with current (rolled out) cmake on most recent Linux' using g++.
– Patrick B.
Oct 19 '14 at 20:18
This is, for me, the only right and nice answer to this question, with current (rolled out) cmake on most recent Linux' using g++.
– Patrick B.
Oct 19 '14 at 20:18
1
1
Copied and pasted this and it worked perfectly. I am on Cygwin using CMAKE 2.8.9. I know about most of the approaches I'm reading here because I follow the CMAKE mailing list and I've ported WebKit to a variety of compilers. The thing we had done for WebKit ports was to install CMake 2.8.12. However because I know Cygwin's CMAKE is old, I wanted something that applied to that version. (Not porting WebKit to Cygwin, sorry)
– cardiff space man
Jan 22 '15 at 0:28
Copied and pasted this and it worked perfectly. I am on Cygwin using CMAKE 2.8.9. I know about most of the approaches I'm reading here because I follow the CMAKE mailing list and I've ported WebKit to a variety of compilers. The thing we had done for WebKit ports was to install CMake 2.8.12. However because I know Cygwin's CMAKE is old, I wanted something that applied to that version. (Not porting WebKit to Cygwin, sorry)
– cardiff space man
Jan 22 '15 at 0:28
Great, this is a drop-in for old CMake and g++ 4.6 (and future-proof). I also upvoted the
CXX_STANDARD
-based answers, but this was the only answer useful in my situation.– Tomasz Gandor
Jan 30 '15 at 14:20
Great, this is a drop-in for old CMake and g++ 4.6 (and future-proof). I also upvoted the
CXX_STANDARD
-based answers, but this was the only answer useful in my situation.– Tomasz Gandor
Jan 30 '15 at 14:20
This is exactly what I was looking for, however it's not clear what is the minimum cmake version needed to use that module. cmake --help-module doesn't help much about it.
– Jan Segre
Mar 31 '15 at 18:12
This is exactly what I was looking for, however it's not clear what is the minimum cmake version needed to use that module. cmake --help-module doesn't help much about it.
– Jan Segre
Mar 31 '15 at 18:12
1
1
@JanSegre the module first appeared in 2.4, cmake.org/…
– KoKuToru
Apr 1 '15 at 6:00
@JanSegre the module first appeared in 2.4, cmake.org/…
– KoKuToru
Apr 1 '15 at 6:00
|
show 1 more comment
The easiest way to set the Cxx standard is:
set_property(TARGET tgt PROPERTY CXX_STANDARD 11)
See the CMake documentation for more details.
2
Yes, this definitely looks like one of the best ways to do it in modern CMake (3.1+)
– Erbureth
May 18 '15 at 21:47
12
Or you can justset(CMAKE_CXX_STANDARD 11)
to define the default property for all targets created after that.
– emlai
Jun 13 '15 at 2:14
1
Does this work in CMake 2.8?
– einpoklum
Nov 19 '15 at 15:48
This is also the solution you need if you want to set different C++ standards on different targets, because theset
command suggested by @emlai is global and affects all subsequent targets.
– Elliott Slaughter
Oct 18 '18 at 21:53
add a comment |
The easiest way to set the Cxx standard is:
set_property(TARGET tgt PROPERTY CXX_STANDARD 11)
See the CMake documentation for more details.
2
Yes, this definitely looks like one of the best ways to do it in modern CMake (3.1+)
– Erbureth
May 18 '15 at 21:47
12
Or you can justset(CMAKE_CXX_STANDARD 11)
to define the default property for all targets created after that.
– emlai
Jun 13 '15 at 2:14
1
Does this work in CMake 2.8?
– einpoklum
Nov 19 '15 at 15:48
This is also the solution you need if you want to set different C++ standards on different targets, because theset
command suggested by @emlai is global and affects all subsequent targets.
– Elliott Slaughter
Oct 18 '18 at 21:53
add a comment |
The easiest way to set the Cxx standard is:
set_property(TARGET tgt PROPERTY CXX_STANDARD 11)
See the CMake documentation for more details.
The easiest way to set the Cxx standard is:
set_property(TARGET tgt PROPERTY CXX_STANDARD 11)
See the CMake documentation for more details.
edited Sep 12 '18 at 3:41


Peter Mortensen
13.7k1986113
13.7k1986113
answered May 15 '15 at 3:50
LuckyrandLuckyrand
52152
52152
2
Yes, this definitely looks like one of the best ways to do it in modern CMake (3.1+)
– Erbureth
May 18 '15 at 21:47
12
Or you can justset(CMAKE_CXX_STANDARD 11)
to define the default property for all targets created after that.
– emlai
Jun 13 '15 at 2:14
1
Does this work in CMake 2.8?
– einpoklum
Nov 19 '15 at 15:48
This is also the solution you need if you want to set different C++ standards on different targets, because theset
command suggested by @emlai is global and affects all subsequent targets.
– Elliott Slaughter
Oct 18 '18 at 21:53
add a comment |
2
Yes, this definitely looks like one of the best ways to do it in modern CMake (3.1+)
– Erbureth
May 18 '15 at 21:47
12
Or you can justset(CMAKE_CXX_STANDARD 11)
to define the default property for all targets created after that.
– emlai
Jun 13 '15 at 2:14
1
Does this work in CMake 2.8?
– einpoklum
Nov 19 '15 at 15:48
This is also the solution you need if you want to set different C++ standards on different targets, because theset
command suggested by @emlai is global and affects all subsequent targets.
– Elliott Slaughter
Oct 18 '18 at 21:53
2
2
Yes, this definitely looks like one of the best ways to do it in modern CMake (3.1+)
– Erbureth
May 18 '15 at 21:47
Yes, this definitely looks like one of the best ways to do it in modern CMake (3.1+)
– Erbureth
May 18 '15 at 21:47
12
12
Or you can just
set(CMAKE_CXX_STANDARD 11)
to define the default property for all targets created after that.– emlai
Jun 13 '15 at 2:14
Or you can just
set(CMAKE_CXX_STANDARD 11)
to define the default property for all targets created after that.– emlai
Jun 13 '15 at 2:14
1
1
Does this work in CMake 2.8?
– einpoklum
Nov 19 '15 at 15:48
Does this work in CMake 2.8?
– einpoklum
Nov 19 '15 at 15:48
This is also the solution you need if you want to set different C++ standards on different targets, because the
set
command suggested by @emlai is global and affects all subsequent targets.– Elliott Slaughter
Oct 18 '18 at 21:53
This is also the solution you need if you want to set different C++ standards on different targets, because the
set
command suggested by @emlai is global and affects all subsequent targets.– Elliott Slaughter
Oct 18 '18 at 21:53
add a comment |
As it turns out, SET(CMAKE_CXX_FLAGS "-std=c++0x")
does activate many C++11 features. The reason it did not work was that the statement looked like this:
set(CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS} -g -ftest-coverage -fprofile-arcs")
Following this approach, somehow the -std=c++0x
flag was overwritten and it did not work. Setting the flags one by one or using a list method is working.
list( APPEND CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS} -g -ftest-coverage -fprofile-arcs")
36
I always just use: SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") # for gcc >= 4.7, or c++0x for 4.6
– David Doria
Sep 21 '12 at 16:42
I once did a little script for that (not complete though): github.com/Morwenn/POLDER/blob/master/cmake/set_cxx_norm.cmake
– Morwenn
Feb 15 '13 at 14:08
9
-1. If you specify any CMAKE_CXX_FLAGS from the command line, the second method will produce a semicolon in the build command (and repeat the original CMAKE_CXX_FLAGS twice).
– Nikolai
May 29 '13 at 14:26
instead of manually adding the -g flag you should set the CMAKE_BUILD_TYPE variable to debug: voices.canonical.com/jussi.pakkanen/2013/03/26/…
– bames53
Nov 24 '14 at 20:52
1
TheCXX_STANDARD
property is better as it is compiler-agnostic.
– jaskmar
Aug 14 '18 at 10:13
add a comment |
As it turns out, SET(CMAKE_CXX_FLAGS "-std=c++0x")
does activate many C++11 features. The reason it did not work was that the statement looked like this:
set(CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS} -g -ftest-coverage -fprofile-arcs")
Following this approach, somehow the -std=c++0x
flag was overwritten and it did not work. Setting the flags one by one or using a list method is working.
list( APPEND CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS} -g -ftest-coverage -fprofile-arcs")
36
I always just use: SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") # for gcc >= 4.7, or c++0x for 4.6
– David Doria
Sep 21 '12 at 16:42
I once did a little script for that (not complete though): github.com/Morwenn/POLDER/blob/master/cmake/set_cxx_norm.cmake
– Morwenn
Feb 15 '13 at 14:08
9
-1. If you specify any CMAKE_CXX_FLAGS from the command line, the second method will produce a semicolon in the build command (and repeat the original CMAKE_CXX_FLAGS twice).
– Nikolai
May 29 '13 at 14:26
instead of manually adding the -g flag you should set the CMAKE_BUILD_TYPE variable to debug: voices.canonical.com/jussi.pakkanen/2013/03/26/…
– bames53
Nov 24 '14 at 20:52
1
TheCXX_STANDARD
property is better as it is compiler-agnostic.
– jaskmar
Aug 14 '18 at 10:13
add a comment |
As it turns out, SET(CMAKE_CXX_FLAGS "-std=c++0x")
does activate many C++11 features. The reason it did not work was that the statement looked like this:
set(CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS} -g -ftest-coverage -fprofile-arcs")
Following this approach, somehow the -std=c++0x
flag was overwritten and it did not work. Setting the flags one by one or using a list method is working.
list( APPEND CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS} -g -ftest-coverage -fprofile-arcs")
As it turns out, SET(CMAKE_CXX_FLAGS "-std=c++0x")
does activate many C++11 features. The reason it did not work was that the statement looked like this:
set(CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS} -g -ftest-coverage -fprofile-arcs")
Following this approach, somehow the -std=c++0x
flag was overwritten and it did not work. Setting the flags one by one or using a list method is working.
list( APPEND CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS} -g -ftest-coverage -fprofile-arcs")
answered Jun 1 '12 at 14:59
TheBicentennialManTheBicentennialMan
2,83762541
2,83762541
36
I always just use: SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") # for gcc >= 4.7, or c++0x for 4.6
– David Doria
Sep 21 '12 at 16:42
I once did a little script for that (not complete though): github.com/Morwenn/POLDER/blob/master/cmake/set_cxx_norm.cmake
– Morwenn
Feb 15 '13 at 14:08
9
-1. If you specify any CMAKE_CXX_FLAGS from the command line, the second method will produce a semicolon in the build command (and repeat the original CMAKE_CXX_FLAGS twice).
– Nikolai
May 29 '13 at 14:26
instead of manually adding the -g flag you should set the CMAKE_BUILD_TYPE variable to debug: voices.canonical.com/jussi.pakkanen/2013/03/26/…
– bames53
Nov 24 '14 at 20:52
1
TheCXX_STANDARD
property is better as it is compiler-agnostic.
– jaskmar
Aug 14 '18 at 10:13
add a comment |
36
I always just use: SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") # for gcc >= 4.7, or c++0x for 4.6
– David Doria
Sep 21 '12 at 16:42
I once did a little script for that (not complete though): github.com/Morwenn/POLDER/blob/master/cmake/set_cxx_norm.cmake
– Morwenn
Feb 15 '13 at 14:08
9
-1. If you specify any CMAKE_CXX_FLAGS from the command line, the second method will produce a semicolon in the build command (and repeat the original CMAKE_CXX_FLAGS twice).
– Nikolai
May 29 '13 at 14:26
instead of manually adding the -g flag you should set the CMAKE_BUILD_TYPE variable to debug: voices.canonical.com/jussi.pakkanen/2013/03/26/…
– bames53
Nov 24 '14 at 20:52
1
TheCXX_STANDARD
property is better as it is compiler-agnostic.
– jaskmar
Aug 14 '18 at 10:13
36
36
I always just use: SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") # for gcc >= 4.7, or c++0x for 4.6
– David Doria
Sep 21 '12 at 16:42
I always just use: SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") # for gcc >= 4.7, or c++0x for 4.6
– David Doria
Sep 21 '12 at 16:42
I once did a little script for that (not complete though): github.com/Morwenn/POLDER/blob/master/cmake/set_cxx_norm.cmake
– Morwenn
Feb 15 '13 at 14:08
I once did a little script for that (not complete though): github.com/Morwenn/POLDER/blob/master/cmake/set_cxx_norm.cmake
– Morwenn
Feb 15 '13 at 14:08
9
9
-1. If you specify any CMAKE_CXX_FLAGS from the command line, the second method will produce a semicolon in the build command (and repeat the original CMAKE_CXX_FLAGS twice).
– Nikolai
May 29 '13 at 14:26
-1. If you specify any CMAKE_CXX_FLAGS from the command line, the second method will produce a semicolon in the build command (and repeat the original CMAKE_CXX_FLAGS twice).
– Nikolai
May 29 '13 at 14:26
instead of manually adding the -g flag you should set the CMAKE_BUILD_TYPE variable to debug: voices.canonical.com/jussi.pakkanen/2013/03/26/…
– bames53
Nov 24 '14 at 20:52
instead of manually adding the -g flag you should set the CMAKE_BUILD_TYPE variable to debug: voices.canonical.com/jussi.pakkanen/2013/03/26/…
– bames53
Nov 24 '14 at 20:52
1
1
The
CXX_STANDARD
property is better as it is compiler-agnostic.– jaskmar
Aug 14 '18 at 10:13
The
CXX_STANDARD
property is better as it is compiler-agnostic.– jaskmar
Aug 14 '18 at 10:13
add a comment |
The easiest way:
add_compile_options(-std=c++11)
5
Only available from cmake 3.0
– Raúl Salinas-Monteagudo
Mar 10 '16 at 12:36
4
This causes warning and errors when compiling C files in the same project.
– rosewater
Jun 10 '16 at 16:10
add a comment |
The easiest way:
add_compile_options(-std=c++11)
5
Only available from cmake 3.0
– Raúl Salinas-Monteagudo
Mar 10 '16 at 12:36
4
This causes warning and errors when compiling C files in the same project.
– rosewater
Jun 10 '16 at 16:10
add a comment |
The easiest way:
add_compile_options(-std=c++11)
The easiest way:
add_compile_options(-std=c++11)
answered Feb 22 '16 at 11:17
alvarezalvarez
552413
552413
5
Only available from cmake 3.0
– Raúl Salinas-Monteagudo
Mar 10 '16 at 12:36
4
This causes warning and errors when compiling C files in the same project.
– rosewater
Jun 10 '16 at 16:10
add a comment |
5
Only available from cmake 3.0
– Raúl Salinas-Monteagudo
Mar 10 '16 at 12:36
4
This causes warning and errors when compiling C files in the same project.
– rosewater
Jun 10 '16 at 16:10
5
5
Only available from cmake 3.0
– Raúl Salinas-Monteagudo
Mar 10 '16 at 12:36
Only available from cmake 3.0
– Raúl Salinas-Monteagudo
Mar 10 '16 at 12:36
4
4
This causes warning and errors when compiling C files in the same project.
– rosewater
Jun 10 '16 at 16:10
This causes warning and errors when compiling C files in the same project.
– rosewater
Jun 10 '16 at 16:10
add a comment |
This is another way of enabling C++11 support,
ADD_DEFINITIONS(
-std=c++11 # Or -std=c++0x
# Other flags
)
I have encountered instances where only this method works and other methods fail. Maybe it has something to do with the latest version of CMake.
9
That will only work if you are ONLY using C++ compiler. If you're also using the CC compiler it will fail.
– Emmanuel
Nov 13 '13 at 16:45
5
add_definitions
is only supposed to used for adding DEFINITIONS, i.e. -D SOMETHING. And as @Emmanuel said, it does not work in many cases.
– xuhdev
Nov 7 '14 at 0:26
I used that before but had problems when I included a C file becauseadd_definitions
was not made for setting flags.
– Jan Segre
Mar 31 '15 at 18:15
add a comment |
This is another way of enabling C++11 support,
ADD_DEFINITIONS(
-std=c++11 # Or -std=c++0x
# Other flags
)
I have encountered instances where only this method works and other methods fail. Maybe it has something to do with the latest version of CMake.
9
That will only work if you are ONLY using C++ compiler. If you're also using the CC compiler it will fail.
– Emmanuel
Nov 13 '13 at 16:45
5
add_definitions
is only supposed to used for adding DEFINITIONS, i.e. -D SOMETHING. And as @Emmanuel said, it does not work in many cases.
– xuhdev
Nov 7 '14 at 0:26
I used that before but had problems when I included a C file becauseadd_definitions
was not made for setting flags.
– Jan Segre
Mar 31 '15 at 18:15
add a comment |
This is another way of enabling C++11 support,
ADD_DEFINITIONS(
-std=c++11 # Or -std=c++0x
# Other flags
)
I have encountered instances where only this method works and other methods fail. Maybe it has something to do with the latest version of CMake.
This is another way of enabling C++11 support,
ADD_DEFINITIONS(
-std=c++11 # Or -std=c++0x
# Other flags
)
I have encountered instances where only this method works and other methods fail. Maybe it has something to do with the latest version of CMake.
answered May 6 '13 at 6:52
HindolHindol
2,0281933
2,0281933
9
That will only work if you are ONLY using C++ compiler. If you're also using the CC compiler it will fail.
– Emmanuel
Nov 13 '13 at 16:45
5
add_definitions
is only supposed to used for adding DEFINITIONS, i.e. -D SOMETHING. And as @Emmanuel said, it does not work in many cases.
– xuhdev
Nov 7 '14 at 0:26
I used that before but had problems when I included a C file becauseadd_definitions
was not made for setting flags.
– Jan Segre
Mar 31 '15 at 18:15
add a comment |
9
That will only work if you are ONLY using C++ compiler. If you're also using the CC compiler it will fail.
– Emmanuel
Nov 13 '13 at 16:45
5
add_definitions
is only supposed to used for adding DEFINITIONS, i.e. -D SOMETHING. And as @Emmanuel said, it does not work in many cases.
– xuhdev
Nov 7 '14 at 0:26
I used that before but had problems when I included a C file becauseadd_definitions
was not made for setting flags.
– Jan Segre
Mar 31 '15 at 18:15
9
9
That will only work if you are ONLY using C++ compiler. If you're also using the CC compiler it will fail.
– Emmanuel
Nov 13 '13 at 16:45
That will only work if you are ONLY using C++ compiler. If you're also using the CC compiler it will fail.
– Emmanuel
Nov 13 '13 at 16:45
5
5
add_definitions
is only supposed to used for adding DEFINITIONS, i.e. -D SOMETHING. And as @Emmanuel said, it does not work in many cases.– xuhdev
Nov 7 '14 at 0:26
add_definitions
is only supposed to used for adding DEFINITIONS, i.e. -D SOMETHING. And as @Emmanuel said, it does not work in many cases.– xuhdev
Nov 7 '14 at 0:26
I used that before but had problems when I included a C file because
add_definitions
was not made for setting flags.– Jan Segre
Mar 31 '15 at 18:15
I used that before but had problems when I included a C file because
add_definitions
was not made for setting flags.– Jan Segre
Mar 31 '15 at 18:15
add a comment |
For CMake 3.8 and newer you can use
target_compile_features(target PUBLIC cxx_std_11)
1
this is the way recommended of latest version of CMake
– camino
May 19 '18 at 17:36
What is the function ofPUBLIC
here?
– Rotsiser Mho
Jun 12 '18 at 3:16
2
@RotsiserMhoPUBLIC
means that other targets that depend on your target will use C++11 as well. For example if your target is a library then all targets that link against your library withtarget_link_libraries
will be compiled with C++11 support.
– eyelash
Jun 12 '18 at 7:05
add a comment |
For CMake 3.8 and newer you can use
target_compile_features(target PUBLIC cxx_std_11)
1
this is the way recommended of latest version of CMake
– camino
May 19 '18 at 17:36
What is the function ofPUBLIC
here?
– Rotsiser Mho
Jun 12 '18 at 3:16
2
@RotsiserMhoPUBLIC
means that other targets that depend on your target will use C++11 as well. For example if your target is a library then all targets that link against your library withtarget_link_libraries
will be compiled with C++11 support.
– eyelash
Jun 12 '18 at 7:05
add a comment |
For CMake 3.8 and newer you can use
target_compile_features(target PUBLIC cxx_std_11)
For CMake 3.8 and newer you can use
target_compile_features(target PUBLIC cxx_std_11)
edited Apr 1 '18 at 11:46
answered Apr 1 '18 at 11:37
eyelasheyelash
1,3151519
1,3151519
1
this is the way recommended of latest version of CMake
– camino
May 19 '18 at 17:36
What is the function ofPUBLIC
here?
– Rotsiser Mho
Jun 12 '18 at 3:16
2
@RotsiserMhoPUBLIC
means that other targets that depend on your target will use C++11 as well. For example if your target is a library then all targets that link against your library withtarget_link_libraries
will be compiled with C++11 support.
– eyelash
Jun 12 '18 at 7:05
add a comment |
1
this is the way recommended of latest version of CMake
– camino
May 19 '18 at 17:36
What is the function ofPUBLIC
here?
– Rotsiser Mho
Jun 12 '18 at 3:16
2
@RotsiserMhoPUBLIC
means that other targets that depend on your target will use C++11 as well. For example if your target is a library then all targets that link against your library withtarget_link_libraries
will be compiled with C++11 support.
– eyelash
Jun 12 '18 at 7:05
1
1
this is the way recommended of latest version of CMake
– camino
May 19 '18 at 17:36
this is the way recommended of latest version of CMake
– camino
May 19 '18 at 17:36
What is the function of
PUBLIC
here?– Rotsiser Mho
Jun 12 '18 at 3:16
What is the function of
PUBLIC
here?– Rotsiser Mho
Jun 12 '18 at 3:16
2
2
@RotsiserMho
PUBLIC
means that other targets that depend on your target will use C++11 as well. For example if your target is a library then all targets that link against your library with target_link_libraries
will be compiled with C++11 support.– eyelash
Jun 12 '18 at 7:05
@RotsiserMho
PUBLIC
means that other targets that depend on your target will use C++11 as well. For example if your target is a library then all targets that link against your library with target_link_libraries
will be compiled with C++11 support.– eyelash
Jun 12 '18 at 7:05
add a comment |
On modern CMake (>= 3.1) best way to set global requirement is:
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
It translates to "I want C++11 for all targets, it's not optional, I dont't want to use any GNU or MS extensions."
As of c++17, this still is IMHO the best way.
Source:
https://crascit.com/2015/03/28/enabling-cxx11-in-cmake/
This way is not ideal for newest versions of C++ if you don't have the latest CMake. E.g. you can't enable C++2a this way until CMake 3.12.
– Ruslan
Jan 29 at 11:41
add a comment |
On modern CMake (>= 3.1) best way to set global requirement is:
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
It translates to "I want C++11 for all targets, it's not optional, I dont't want to use any GNU or MS extensions."
As of c++17, this still is IMHO the best way.
Source:
https://crascit.com/2015/03/28/enabling-cxx11-in-cmake/
This way is not ideal for newest versions of C++ if you don't have the latest CMake. E.g. you can't enable C++2a this way until CMake 3.12.
– Ruslan
Jan 29 at 11:41
add a comment |
On modern CMake (>= 3.1) best way to set global requirement is:
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
It translates to "I want C++11 for all targets, it's not optional, I dont't want to use any GNU or MS extensions."
As of c++17, this still is IMHO the best way.
Source:
https://crascit.com/2015/03/28/enabling-cxx11-in-cmake/
On modern CMake (>= 3.1) best way to set global requirement is:
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
It translates to "I want C++11 for all targets, it's not optional, I dont't want to use any GNU or MS extensions."
As of c++17, this still is IMHO the best way.
Source:
https://crascit.com/2015/03/28/enabling-cxx11-in-cmake/
answered Sep 18 '18 at 8:42
MateuszLMateuszL
785719
785719
This way is not ideal for newest versions of C++ if you don't have the latest CMake. E.g. you can't enable C++2a this way until CMake 3.12.
– Ruslan
Jan 29 at 11:41
add a comment |
This way is not ideal for newest versions of C++ if you don't have the latest CMake. E.g. you can't enable C++2a this way until CMake 3.12.
– Ruslan
Jan 29 at 11:41
This way is not ideal for newest versions of C++ if you don't have the latest CMake. E.g. you can't enable C++2a this way until CMake 3.12.
– Ruslan
Jan 29 at 11:41
This way is not ideal for newest versions of C++ if you don't have the latest CMake. E.g. you can't enable C++2a this way until CMake 3.12.
– Ruslan
Jan 29 at 11:41
add a comment |
What works for me is to set the following line in your CMakeLists.txt:
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
Setting this command activates the C++11 features for the compiler and after executing the cmake ..
command, you should be able to use range based for loops
in your code and compile it without any errors.
This is in the end the best answer if you want exactly-std=c++11
, asset (CMAKE_CXX_STANDARD 11)
will use the flag-std=gnu++11
, which might be undesirable.
– Antonio
May 31 '17 at 10:08
1
@Antonioset (CMAKE_CXX_EXTENSIONS OFF)
– mloskot
Aug 29 '17 at 15:14
add a comment |
What works for me is to set the following line in your CMakeLists.txt:
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
Setting this command activates the C++11 features for the compiler and after executing the cmake ..
command, you should be able to use range based for loops
in your code and compile it without any errors.
This is in the end the best answer if you want exactly-std=c++11
, asset (CMAKE_CXX_STANDARD 11)
will use the flag-std=gnu++11
, which might be undesirable.
– Antonio
May 31 '17 at 10:08
1
@Antonioset (CMAKE_CXX_EXTENSIONS OFF)
– mloskot
Aug 29 '17 at 15:14
add a comment |
What works for me is to set the following line in your CMakeLists.txt:
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
Setting this command activates the C++11 features for the compiler and after executing the cmake ..
command, you should be able to use range based for loops
in your code and compile it without any errors.
What works for me is to set the following line in your CMakeLists.txt:
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
Setting this command activates the C++11 features for the compiler and after executing the cmake ..
command, you should be able to use range based for loops
in your code and compile it without any errors.
answered Aug 1 '16 at 8:27


Kevin KatzkeKevin Katzke
1,67032031
1,67032031
This is in the end the best answer if you want exactly-std=c++11
, asset (CMAKE_CXX_STANDARD 11)
will use the flag-std=gnu++11
, which might be undesirable.
– Antonio
May 31 '17 at 10:08
1
@Antonioset (CMAKE_CXX_EXTENSIONS OFF)
– mloskot
Aug 29 '17 at 15:14
add a comment |
This is in the end the best answer if you want exactly-std=c++11
, asset (CMAKE_CXX_STANDARD 11)
will use the flag-std=gnu++11
, which might be undesirable.
– Antonio
May 31 '17 at 10:08
1
@Antonioset (CMAKE_CXX_EXTENSIONS OFF)
– mloskot
Aug 29 '17 at 15:14
This is in the end the best answer if you want exactly
-std=c++11
, as set (CMAKE_CXX_STANDARD 11)
will use the flag -std=gnu++11
, which might be undesirable.– Antonio
May 31 '17 at 10:08
This is in the end the best answer if you want exactly
-std=c++11
, as set (CMAKE_CXX_STANDARD 11)
will use the flag -std=gnu++11
, which might be undesirable.– Antonio
May 31 '17 at 10:08
1
1
@Antonio
set (CMAKE_CXX_EXTENSIONS OFF)
– mloskot
Aug 29 '17 at 15:14
@Antonio
set (CMAKE_CXX_EXTENSIONS OFF)
– mloskot
Aug 29 '17 at 15:14
add a comment |
I think just these two lines are enough.
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
1
This makes sense when all targets in a project use the same C++ standard (all compiled libraries and executables use C++11, for example). Otherwise, the Cmaketarget_compile_features
function, applied for each individual target, as shown in other answers, is a more recommended approach.
– Allan
Aug 6 '18 at 10:45
add a comment |
I think just these two lines are enough.
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
1
This makes sense when all targets in a project use the same C++ standard (all compiled libraries and executables use C++11, for example). Otherwise, the Cmaketarget_compile_features
function, applied for each individual target, as shown in other answers, is a more recommended approach.
– Allan
Aug 6 '18 at 10:45
add a comment |
I think just these two lines are enough.
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
I think just these two lines are enough.
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
edited Jun 13 '18 at 9:41


Pang
6,9511664103
6,9511664103
answered Jun 13 '18 at 9:36


Ehsan PanahiEhsan Panahi
764
764
1
This makes sense when all targets in a project use the same C++ standard (all compiled libraries and executables use C++11, for example). Otherwise, the Cmaketarget_compile_features
function, applied for each individual target, as shown in other answers, is a more recommended approach.
– Allan
Aug 6 '18 at 10:45
add a comment |
1
This makes sense when all targets in a project use the same C++ standard (all compiled libraries and executables use C++11, for example). Otherwise, the Cmaketarget_compile_features
function, applied for each individual target, as shown in other answers, is a more recommended approach.
– Allan
Aug 6 '18 at 10:45
1
1
This makes sense when all targets in a project use the same C++ standard (all compiled libraries and executables use C++11, for example). Otherwise, the Cmake
target_compile_features
function, applied for each individual target, as shown in other answers, is a more recommended approach.– Allan
Aug 6 '18 at 10:45
This makes sense when all targets in a project use the same C++ standard (all compiled libraries and executables use C++11, for example). Otherwise, the Cmake
target_compile_features
function, applied for each individual target, as shown in other answers, is a more recommended approach.– Allan
Aug 6 '18 at 10:45
add a comment |
In case you want to activate always the latest C++ standard, here's my extension of David Grayson's answer, in light of the recent (CMake 3.8, CMake 3.11) additions of values of 17 and 20 for CMAKE_CXX_STANDARD):
IF (CMAKE_VERSION VERSION_LESS "3.8")
SET(CMAKE_CXX_STANDARD 14)
ELSEIF (CMAKE_VERSION VERSION_LESS "3.11")
SET(CMAKE_CXX_STANDARD 17)
ELSE() # CMake 3.11 or higher:
SET(CMAKE_CXX_STANDARD 20)
ENDIF()
# Typically, you'll also want to turn off compiler-specific extensions:
SET(CMAKE_CXX_EXTENSIONS OFF)
(use that code in the place of set (CMAKE_CXX_STANDARD 11)
in the linked anwswer).
add a comment |
In case you want to activate always the latest C++ standard, here's my extension of David Grayson's answer, in light of the recent (CMake 3.8, CMake 3.11) additions of values of 17 and 20 for CMAKE_CXX_STANDARD):
IF (CMAKE_VERSION VERSION_LESS "3.8")
SET(CMAKE_CXX_STANDARD 14)
ELSEIF (CMAKE_VERSION VERSION_LESS "3.11")
SET(CMAKE_CXX_STANDARD 17)
ELSE() # CMake 3.11 or higher:
SET(CMAKE_CXX_STANDARD 20)
ENDIF()
# Typically, you'll also want to turn off compiler-specific extensions:
SET(CMAKE_CXX_EXTENSIONS OFF)
(use that code in the place of set (CMAKE_CXX_STANDARD 11)
in the linked anwswer).
add a comment |
In case you want to activate always the latest C++ standard, here's my extension of David Grayson's answer, in light of the recent (CMake 3.8, CMake 3.11) additions of values of 17 and 20 for CMAKE_CXX_STANDARD):
IF (CMAKE_VERSION VERSION_LESS "3.8")
SET(CMAKE_CXX_STANDARD 14)
ELSEIF (CMAKE_VERSION VERSION_LESS "3.11")
SET(CMAKE_CXX_STANDARD 17)
ELSE() # CMake 3.11 or higher:
SET(CMAKE_CXX_STANDARD 20)
ENDIF()
# Typically, you'll also want to turn off compiler-specific extensions:
SET(CMAKE_CXX_EXTENSIONS OFF)
(use that code in the place of set (CMAKE_CXX_STANDARD 11)
in the linked anwswer).
In case you want to activate always the latest C++ standard, here's my extension of David Grayson's answer, in light of the recent (CMake 3.8, CMake 3.11) additions of values of 17 and 20 for CMAKE_CXX_STANDARD):
IF (CMAKE_VERSION VERSION_LESS "3.8")
SET(CMAKE_CXX_STANDARD 14)
ELSEIF (CMAKE_VERSION VERSION_LESS "3.11")
SET(CMAKE_CXX_STANDARD 17)
ELSE() # CMake 3.11 or higher:
SET(CMAKE_CXX_STANDARD 20)
ENDIF()
# Typically, you'll also want to turn off compiler-specific extensions:
SET(CMAKE_CXX_EXTENSIONS OFF)
(use that code in the place of set (CMAKE_CXX_STANDARD 11)
in the linked anwswer).
answered Dec 6 '18 at 18:28


codelingcodeling
8,26632556
8,26632556
add a comment |
add a comment |
OS X and Homebrew LLVM related:
Don't forget to call cmake_minimum_required(VERSION 3.3) and project() after it!
Or CMake will insert project()
implicitly before line 1, causing trouble with Clang version detection and possibly other sorts of troubles. Here is a related issue.
1
The question is not OSX or LVVM related, specifically. Also, there's no reason to require CMake v3.x for C++11 development. As for clang version detection - that's not what OP asked about.
– einpoklum
Jun 3 '16 at 19:52
add a comment |
OS X and Homebrew LLVM related:
Don't forget to call cmake_minimum_required(VERSION 3.3) and project() after it!
Or CMake will insert project()
implicitly before line 1, causing trouble with Clang version detection and possibly other sorts of troubles. Here is a related issue.
1
The question is not OSX or LVVM related, specifically. Also, there's no reason to require CMake v3.x for C++11 development. As for clang version detection - that's not what OP asked about.
– einpoklum
Jun 3 '16 at 19:52
add a comment |
OS X and Homebrew LLVM related:
Don't forget to call cmake_minimum_required(VERSION 3.3) and project() after it!
Or CMake will insert project()
implicitly before line 1, causing trouble with Clang version detection and possibly other sorts of troubles. Here is a related issue.
OS X and Homebrew LLVM related:
Don't forget to call cmake_minimum_required(VERSION 3.3) and project() after it!
Or CMake will insert project()
implicitly before line 1, causing trouble with Clang version detection and possibly other sorts of troubles. Here is a related issue.
edited Sep 12 '18 at 3:43


Peter Mortensen
13.7k1986113
13.7k1986113
answered Jan 29 '16 at 16:46
senzsenz
776714
776714
1
The question is not OSX or LVVM related, specifically. Also, there's no reason to require CMake v3.x for C++11 development. As for clang version detection - that's not what OP asked about.
– einpoklum
Jun 3 '16 at 19:52
add a comment |
1
The question is not OSX or LVVM related, specifically. Also, there's no reason to require CMake v3.x for C++11 development. As for clang version detection - that's not what OP asked about.
– einpoklum
Jun 3 '16 at 19:52
1
1
The question is not OSX or LVVM related, specifically. Also, there's no reason to require CMake v3.x for C++11 development. As for clang version detection - that's not what OP asked about.
– einpoklum
Jun 3 '16 at 19:52
The question is not OSX or LVVM related, specifically. Also, there's no reason to require CMake v3.x for C++11 development. As for clang version detection - that's not what OP asked about.
– einpoklum
Jun 3 '16 at 19:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10851247%2fhow-to-activate-c-11-in-cmake%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
11
The
SET(CMAKE_CXX_FLAGS "-std=c++0x")
works fine for me, so there is probably a problem somewhere else in the CMakeLists file. Make sure you don't accidentally overwrite the contents of CMAKE_CXX_FLAGS later on.– ComicSansMS
Jun 1 '12 at 14:11
7
add_definitions(-std=c++11) works for me with CMake 2.8.8
– kyku
Jun 2 '12 at 8:49
@ComicSansMS: You are totally right! I overwrote it, which was my own mistake. I have corrected it, and now it is working fine! C++11 stuff is very cool! I wanted to loop on a vector of structures, which would require iterator and needless coding noise if I did not have range based for loops. I guess I could use BOOST_FOREACH though, but oh well...
– TheBicentennialMan
Jun 3 '12 at 10:31
24
For CMake ≥3.1,
set(CMAKE_CXX_STANDARD 11)
(before defining the target) is the best way.– emlai
Jun 13 '15 at 2:26
@tuple_cat You can do it target-based as well. But be aware that
CXX_STANDARD
does not work on MSVC, so basically you have to fall back totarget_compile_features
if you want something that works cross-platform.– Ela782
Dec 9 '16 at 20:21