如何在 CLion 中启用 C++11?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26317981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to enable C++11 in CLion?
提问by Pavel
I'm trying to run C++11 code in CLion but it doesn't work. It says:
我正在尝试在 CLion 中运行 C++11 代码,但它不起作用。它说:
...
/projects/CLion/untitled/main.cpp:7:1: note: C++11 ‘constexpr' only available with -std=c++11 or -std=gnu++11
...
I tried to set CMAKE_C_FLAGS
to -std=c++11
or -std=gnu++11
but I still have the same problem. Regular C++ code compiles fine.
我尝试设置CMAKE_C_FLAGS
为-std=c++11
or-std=gnu++11
但我仍然遇到同样的问题。常规 C++ 代码编译良好。
What flag do I have to set in CLion's CMake window to compile my C++11 code?
我必须在 CLion 的 CMake 窗口中设置什么标志来编译我的 C++11 代码?
回答by Gluttton
I tried to set CMAKE_C_FLAGS
我试图设置 CMAKE_C_FLAGS
According to the documentationthe CMAKE_C_FLAGS
set C language flags for all build types. For C++ you need use CMAKE_CXX_FLAGS
instead:
根据该文件的CMAKE_C_FLAGS
所有集合C语言标志构建类型。对于 C++,您需要CMAKE_CXX_FLAGS
改用:
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
回答by Casey
For CMake 3.1 or later, you can set the CMAKE_CXX_STANDARD
variableto 11
:
对于 CMake 3.1 或更高版本,您可以将CMAKE_CXX_STANDARD
变量设置为11
:
Default value for
CXX_STANDARD
property of targets.This variable is used to initialize the
CXX_STANDARD
property on all targets.
CXX_STANDARD
目标属性的默认值。此变量用于初始化
CXX_STANDARD
所有目标上的属性。
The C++ standard whose features are requested to build this target.
This property specifies the C++ standard whose features are requested to build this target. For some compilers, this results in adding a flag such as
-std=gnu++11
to the compile line.Supported values are 98, 11 and 14.
If the value requested does not result in a compile flag being added for the compiler in use, a previous standard flag will be added instead. This means that using:
set_property(TARGET tgt PROPERTY CXX_STANDARD 11)
with a compiler which does not support
-std=gnu++11
or an equivalent flag will not result in an error or warning, but will instead add the-std=gnu++98
flag if supported. This “decay” behavior may be controlled with theCXX_STANDARD_REQUIRED
target property.See the cmake-compile-features(7)manual for information on compile features.
This property is initialized by the value of the
CMAKE_CXX_STANDARD
variable if it is set when a target is created.
要求其特性来构建此目标的 C++ 标准。
此属性指定构建此目标时请求其功能的 C++ 标准。对于某些编译器,这会导致在
-std=gnu++11
编译行中添加一个标志。支持的值为 98、11 和 14。
如果请求的值不会导致为正在使用的编译器添加编译标志,则会添加以前的标准标志。这意味着使用:
set_property(TARGET tgt PROPERTY CXX_STANDARD 11)
使用不支持
-std=gnu++11
或等效标志的编译器不会导致错误或警告,但会-std=gnu++98
在支持时添加标志。这种“衰减”行为可以通过CXX_STANDARD_REQUIRED
目标属性来控制。有关编译功能的信息,请参阅cmake-compile-features(7)手册。
CMAKE_CXX_STANDARD
如果在创建目标时设置了该属性,则该属性由变量的值初始化。