我是靠谱客的博主 留胡子芒果,这篇文章主要介绍如何在CMake中激活C ++ 11?,现在分享给大家,希望可以做个参考。

本文翻译自: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: 我也尝试过这个:

复制代码
1
2
3
4
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: 它不起作用的原因是该语句看起来像这样:

复制代码
1
2
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. 逐一设置标记或使用列表方法可以正常工作。

复制代码
1
2
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支持的另一种方法,

复制代码
1
2
3
4
5
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 ++标准。

复制代码
1
2
3
4
5
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_STANDARDCXX_STANDARD_REQUIRED来显式指定C ++标准。

See also my more detailed answer . 另请参阅我的更详细的答案 。


#5楼

I am using 我在用

复制代码
1
2
3
4
5
6
7
8
9
10
11
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标准的最简单方法是:

复制代码
1
2
set_property(TARGET tgt PROPERTY CXX_STANDARD 11)

See the CMake documentation for more details. 有关更多详细信息,请参见CMake文档 。

最后

以上就是留胡子芒果最近收集整理的关于如何在CMake中激活C ++ 11?的全部内容,更多相关如何在CMake中激活C内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(79)

评论列表共有 0 条评论

立即
投稿
返回
顶部