168

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?

Ali
  • 51,545
  • 25
  • 157
  • 246
Andrey Chernukha
  • 20,316
  • 16
  • 89
  • 153
  • 3
    Qt Creator is not a compiler. When you read that "Qt Creator supports C++11" it means that the code-completion engine (Clang in this case) supports C++11 syntax. – cmannett85 Jun 05 '13 at 20:55
  • @cmannett85 Qt Creator still does not use Clang as a C++ syntax parser. There were efforts, but Clang's API and general performance of this solution delayed this. Current work in this direction is located [here](https://qt.gitorious.org/qt-creator/qt-creator/commits/9f831dde07cb2411808534e76669b28a1b76e21d). – rubenvb Jan 04 '14 at 17:14

6 Answers6

263

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.

Ali
  • 51,545
  • 25
  • 157
  • 246
  • 4
    Anonymous downvotes aren't helping anybody. What's wrong with the answer? – Ali Aug 11 '14 at 20:58
  • The problem was, I wasn't able to delete your duplicate/incomplete answer, all I could do was to downvote it. Now that you have edited it to make it more presentable, I am happy with just the downvote. – nurettin Aug 21 '14 at 12:55
  • 9
    @nurettin Thanks for the feedback. If you examine carefully the edit histories of the answers (mine and the others), you will see that my original answer was not a duplicate; it was actually the other answer that shamelessly stole part of my answer, making my answer look like a duplicate. Then two more duplicate answers appeared this year. Check it for yourself in the edit histories. Given this information, would you reconsider your downvote? – Ali Aug 21 '14 at 15:23
  • For some reason Qt Creator still gives me this error even though I have configured my project to use C++11. So weird...Using Qt Creator 3.2.1 build with Qt 5.3.2. @guardezi 's answer however works for me. – rbaleksandar Aug 20 '15 at 09:26
  • @rbaleksandar Interesting. Could you provide more details regarding *"Qt Creator still gives me this error"*, please? It is hard to figure out what the problem might be without the error message. guardezi's answer adds `-std=c++11` to the **linker** flags which looks weird to me. – Ali Aug 20 '15 at 09:36
  • `ranged-based 'for' loops are not allowed in C++ 98 mode` appears when using `for(auto& x : xs)` (xs is a vector of strings). Also `std::vector xs = { "a", "b", "c" };` issues an error about being unable to convert from brace-enclosed initializer list to `std::vector >`.These along with other errors and warnings (`auto` -> `ISO C++ forbids declaration with no type`) are all C++11 related.It works only when both `QMAKE_CXXFLAGS+= -std=c++11` and `QMAKE_LFLAGS += -std=c++11` are in the project file. It's so much easier to do these things in CMake...Geeesh... – rbaleksandar Aug 20 '15 at 10:20
  • @rbaleksandar Very strange. The error messages that you give come from the **compiler** and not from the linker; you have not even reached the linker. It does not make any sense to me why adding `-std=c++11` to the **linker** flags resolves these error messages: It looks like a bug in qmake. Could you somehow post an [SSCCE](http://sscce.org/), please? (E.g. a github gist.) – Ali Aug 20 '15 at 10:42
  • Yeah, I noticed that about the linker. I'll visit the bugtracker for qmake later today and look around more to see if it's not something I'm doing wrong. Will also post a question here. As I said no such issues with CMake. Just wanted to check something using qmake. Thanks for elaborating on this issue. – rbaleksandar Aug 20 '15 at 10:47
  • @rbaleksandar OK, please let me know if you post a question on this issue. – Ali Aug 20 '15 at 10:49
  • Nevermind. The issue was that I was using the default Qt kit for my Debian - version 4.8.x. The moment I switched to Qt 5 kit for my project qmake 5 kicked in and it worked. LOL I guess that is what you mean by **Qt 5 only**. I forgot that this also include projects that don't use the library itself but rely on qmake. *facepalm* You might want to add this to your answer. I think it's a useful piece of information. – rbaleksandar Aug 20 '15 at 11:08
  • @rbaleksandar I am afraid I don't follow: The `QMAKE_CXXFLAGS += -std=c++11` should work with Qt 4.8 *without* `QMAKE_LFLAGS += -std=c++11`. I still think that it is a bug in qmake if you have to add `std=c++11` to the **linker** flags. Or I am missing your point... :( – Ali Aug 20 '15 at 12:03
  • The linker flags seemed not to be necessary even before. Dunno why I thought so. That was just me being stupid. I think I might have skipped one re-reading of the project file after I made a change testing which one works hence when rebuilding the old state was used where I was using CONFIG, which wasn't working. – rbaleksandar Aug 20 '15 at 12:10
  • `CONFIG += c++11` doesn't work, Qt still defaults to `-std=c++0x` as shown in the compile output – Troyseph Sep 15 '15 at 09:05
  • @Troyseph Yes, if you have an older compiler, then you have to use `-std=c++0x`. That's why I also mention that flag in my answer. – Ali Sep 15 '15 at 09:08
  • @Ali I don't have an old compiler, I do have a c++11 compiler and using `QMAKE_CXXFLAGS += -std=c++11` does help. So the question is, why is Qt defaulting to the wrong one when explicitly told through `CONFIG`? – Troyseph Sep 15 '15 at 09:12
  • @Troyseph OK, now I understand your first comment. It's still strange though. The C++11 compilers are supposed to be backward compatible and `-std=c++0x` should work even if the compiler supports `-std=c++11` too. So, in my opinion, Qt does the right thing and defaults to the flag that should always work (even with pre-C+11 compilers). What happens if you use `-std=c++0x`? Doesn't your C++11 compiler recognize that flag? Or what sort of error do you get? – Ali Sep 15 '15 at 10:08
  • @Ali I'm using features from `functional` that won't compile with `-std=c++0x` but will with `-std::c++11` so my errors are undefined functions and namespaces. Also since when has ignoring a users explicit make setting "the right thing to do"? I've had to override their value using `QMAKE_CXXFLAGS_CXX11 = -std=c++11` – Troyseph Sep 15 '15 at 10:12
  • 3
    @Troyseph Here is my understanding of the situation. I am assuming that you are using gcc. If a version of gcc supports `-std=c++11`, then it should also support (the deprecated) `-std=c++0x` flag as well, and both flags are supposed to have identical effects (which apparently isn't the case on your machine). If a compiler supports `-std=c++0x`, it doesn't mean that it understands `-std=c++11`. Therefore, picking `-std=c++0x` as default for C++11 compatibility mode is a reasonable choice. On my machine, *at least according to the man page*, `-std=c++0x` and `-std=c++11` are identical. – Ali Sep 15 '15 at 10:38
  • 1
    @Troyseph Now, it is true that it would be better to use `-std=c++11` if the compiler supports it, and Qt could be smart enough to do so. Well, if this issue hurts you that much, you could file a bug report... – Ali Sep 15 '15 at 10:42
32

Add this to your .pro file

QMAKE_CXXFLAGS += -std=c++11

or

CONFIG += c++11
Guilherme Nascimento
  • 7,990
  • 6
  • 41
  • 107
LemonCool
  • 1,190
  • 9
  • 17
18

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.

Яois
  • 3,563
  • 3
  • 21
  • 46
8

add to your qmake file

QMAKE_CXXFLAGS+= -std=c++11
QMAKE_LFLAGS +=  -std=c++11
guardezi
  • 171
  • 1
  • 8
4

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

QMAKE_CXXFLAGS += -std=c++0x
asloob
  • 1,258
  • 20
  • 34
1

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
Bretzelus
  • 165
  • 1
  • 7