How to enable C++11 in Qt Creator?

C++QtC++11

C++ Problem Overview


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:

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 article this version of Qt Creator supports C++11. So how do I enable it?

C++ Solutions


Solution 1 - C++

According to this site add

CONFIG += c++11

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


The other answers, suggesting

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

also work with Qt 4.8 and gcc / clang.

Solution 2 - C++

Add this to your .pro file

QMAKE_CXXFLAGS += -std=c++11

or

CONFIG += c++11

Solution 3 - C++

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

# 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.

Solution 4 - C++

add to your qmake file

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

Solution 5 - C++

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

QMAKE_CXXFLAGS += -std=c++0x

Solution 6 - C++

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:

Then at the line:

QMAKE_CFLAGS           += -fno-keep-inline-dllexport

Edit :

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrey ChernukhaView Question on Stackoverflow
Solution 1 - C++AliView Answer on Stackoverflow
Solution 2 - C++LemonCoolView Answer on Stackoverflow
Solution 3 - C++ЯoisView Answer on Stackoverflow
Solution 4 - C++guardeziView Answer on Stackoverflow
Solution 5 - C++asloobView Answer on Stackoverflow
Solution 6 - C++BretzelusView Answer on Stackoverflow