如何在 CMake 中激活 C++ 11? [英] How do I activate C++ 11 in CMake?
问题描述
当我尝试运行 CMake 生成的 makefile 来编译我的程序时,出现错误
When I try to run a CMake generated makefile to compile my program, I get the error that
C++ 98 模式不支持基于范围的 for 循环.
range based for loops are not supported in C++ 98 mode.
我尝试将 add_definitions(-std=c++0x)
添加到我的 CMakeLists.txt
,但没有帮助.
I tried adding add_definitions(-std=c++0x)
to my CMakeLists.txt
, but it did not help.
我也试过这个:
if(CMAKE_COMPILER_IS_GNUCXX)
add_definitions(-std=gnu++0x)
endif()
当我执行 g++ --version
时,我得到:
When I do g++ --version
, I get:
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
我也试过SET(CMAKE_CXX_FLAGS "-std=c++0x")
,同样不行.
我不明白如何使用 CMake 激活 C++ 11 功能.
I do not understand how I can activate C++ 11 features using CMake.
推荐答案
事实证明,SET(CMAKE_CXX_FLAGS "-std=c++0x")
确实激活了许多 C++11 功能.它不起作用的原因是该语句如下所示:
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")
按照这种方法,不知何故 -std=c++0x
标志被覆盖,它不起作用.一一设置标志或使用列表方法都有效.
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")
这篇关于如何在 CMake 中激活 C++ 11?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!