如何在 Qt Creator 中启用 C++11?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16948382/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:46:43  来源:igfitidea点击:

How to enable C++11 in Qt Creator?

c++qtc++11

提问by Andrey Chernukha

The title is pretty self-descriptive. I've downloaded Qt Creator 2.7.0, and I am trying to compile some basic C++11 code:

标题是非常自我描述的。我已经下载了 Qt Creator 2.7.0,我正在尝试编译一些基本的 C++11 代码:

int my_array[5] = {1, 2, 3, 4, 5};
for(int &x : my_array)
{
  x *= 2;
}

I'm receiving the following error:

我收到以下错误:

range based for loops are not allowed in c++ 98 mode

Yet, according to this articlethis version of Qt Creator supports C++11. So how do I enable it?

然而,根据这篇文章,这个版本的 Qt Creator 支持 C++11。那么我该如何启用它呢?

回答by Ali

According to this siteadd

根据本站添加

CONFIG += c++11

CONFIG += c++11

to your .pro file (see at the bottom of that web page). It requires Qt 5.

到您的 .pro 文件(参见该网页底部)。它需要 Qt 5。



The other answers, suggesting

其他答案,建议

QMAKE_CXXFLAGS += -std=c++11(or QMAKE_CXXFLAGS += -std=c++0x)

QMAKE_CXXFLAGS += -std=c++11(或QMAKE_CXXFLAGS += -std=c++0x

also work with Qt 4.8 and gcc / clang.

也适用于Qt 4.8 和 gcc/clang。

回答by LemonCool

Add this to your .pro file

将此添加到您的.pro 文件中

QMAKE_CXXFLAGS += -std=c++11

or

或者

CONFIG += c++11

回答by Яois

As an alternative for handling both cases addressed in Ali's excellent answer, I usually add

作为处理 Ali 出色回答中提到的两种情况的替代方法,我通常添加

# With C++11 support
greaterThan(QT_MAJOR_VERSION, 4){    
CONFIG += c++11
} else {
QMAKE_CXXFLAGS += -std=c++0x
}

to my project files. This can be handy when you don't really care much about which Qt version is people using in your team, but you want them to have C++11 enabled in any case.

到我的项目文件。当您不太关心团队中人们使用哪个 Qt 版本时,这会很方便,但您希望他们在任何情况下都启用 C++11。

回答by guardezi

add to your qmake file

添加到您的 qmake 文件

QMAKE_CXXFLAGS+= -std=c++11
QMAKE_LFLAGS +=  -std=c++11

回答by asloob

If you are using an earlier version of QT (<5) try this

如果您使用的是较早版本的 QT (<5) 试试这个

QMAKE_CXXFLAGS += -std=c++0x

回答by Bretzelus

The only place I have successfully make it work is by searching in:

我成功使它工作的唯一地方是通过搜索:

...\Qt\{5.9; or your version}\mingw{53_32; or your version}\mkspecs\win32-g++\qmake.conf:

...\Qt\{5.9; 或者你的版本}\mingw{53_32; 或您的版本}\mkspecs\win32-g++\qmake.conf:

Then at the line:

然后在这一行:

QMAKE_CFLAGS           += -fno-keep-inline-dllexport

Edit :

编辑 :

QMAKE_CFLAGS           += -fno-keep-inline-dllexport -std=c++11