如何在CMake中激活C ++ 11? [英] How do I activate C++ 11 in CMake?

查看:138
本文介绍了如何在CMake中激活C ++ 11?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试运行CMake生成的makefile来编译程序时,出现以下错误

When I try to run a CMake generated makefile to compile my program, I get the error that


基于循环的范围不正确在C ++ 98模式下受支持。

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 时,我得到:


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),这也行不通。

I have also tried SET(CMAKE_CXX_FLAGS "-std=c++0x"), which also does not work.

我不明白如何使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆