概述
本文翻译自:How do I activate C++ 11 in CMake?
When I try to run a CMake generated makefile to compile my program, I get the error that 当我尝试运行CMake生成的makefile来编译程序时,出现以下错误:
range based for loops are not supported in C++ 98 mode. C ++ 98模式不支持基于范围的for循环。
I tried adding add_definitions(-std=c++0x)
to my CMakeLists.txt
, but it did not help. 我尝试将add_definitions(-std=c++0x)
到我的CMakeLists.txt
,但这没有帮助。
I tried this too: 我也尝试过这个:
if(CMAKE_COMPILER_IS_GNUCXX)
add_definitions(-std=gnu++0x)
endif()
When I do g++ --version
, I get: 当我做g++ --version
,我得到:
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1 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. 我也尝试过SET(CMAKE_CXX_FLAGS "-std=c++0x")
,这也行不通。
I do not understand how I can activate C++ 11 features using CMake. 我不明白如何使用CMake激活C ++ 11功能。
#1楼
参考:https://stackoom.com/question/jWu7/如何在CMake中激活C
#2楼
As it turns out, SET(CMAKE_CXX_FLAGS "-std=c++0x")
does activate many C++11 features. 事实证明, SET(CMAKE_CXX_FLAGS "-std=c++0x")
确实激活了许多C ++ 11功能。 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. 按照这种方法, -std=c++0x
标志以某种方式被覆盖了并且不起作用。 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")
#3楼
This is another way of enabling C++11 support, 这是启用C ++ 11支持的另一种方法,
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. 也许与最新版本的CMake有关。
#4楼
The CMake command target_compile_features()
is used to specify the required C++ feature cxx_range_for
. CMake命令target_compile_features()
用于指定所需的C ++功能cxx_range_for
。 CMake will then induce the C++ standard to be used. 然后,CMake将促使使用C ++标准。
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. 无需使用add_definitions(-std=c++11)
或修改CMake变量CMAKE_CXX_FLAGS
,因为CMake将确保使用适当的命令行标志调用C ++编译器。
Maybe your C++ program uses other C++ features than cxx_range_for
. 也许您的C ++程序使用了cxx_range_for
以外的其他C ++功能。 The CMake global property CMAKE_CXX_KNOWN_FEATURES
lists the C++ features you can choose from. CMake全局属性CMAKE_CXX_KNOWN_FEATURES
列出了您可以选择的C ++功能。
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. 除了使用target_compile_features()
还可以通过为CMake目标设置CMake属性CXX_STANDARD
和CXX_STANDARD_REQUIRED
来显式指定C ++标准。
See also my more detailed answer . 另请参阅我的更详细的答案 。
#5楼
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. 但是,如果您想使用C++11
,那么g++ 4.6.1
已经相当老了。 Try to get a newer g++
version. 尝试获取更新的g++
版本。
#6楼
The easiest way to set the Cxx standard is: 设置Cxx标准的最简单方法是:
set_property(TARGET tgt PROPERTY CXX_STANDARD 11)
See the CMake documentation for more details. 有关更多详细信息,请参见CMake文档 。
最后
以上就是留胡子芒果为你收集整理的如何在CMake中激活C ++ 11?的全部内容,希望文章能够帮你解决如何在CMake中激活C ++ 11?所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复