C++ Qt - How to add "-std=c++11" to the makefile which is generated by qmake?

QtC++11MakefileQmake

Qt Problem Overview


I'm developing a program in Qt. Its makefile is generated automatically from the .pro file. I need to use some code which need the -std=c++11 flag to be set up for g++. Where in .pro should I add this flag? (changing only the Makefile won't work since it gets overwritten by the newly generated one, each time I build the project).

Qt Solutions


Solution 1 - Qt

You can add the following to the Qt .pro for C++11: -

CONFIG += c++11

As of Qt 5.4, C++14 can be enabled with

CONFIG += c++14

Solution 2 - Qt

You can change CXX flags :

QMAKE_CXXFLAGS += -std=c++11

I usually set it as :

QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -pedantic

Solution 3 - Qt

You may find it tempting to insert the specific flag (which you mention)

QMAKE_CXXFLAGS += -std=c++11

in your .pro file, but this will insert just that flag on your behalf.

That's insufficient. The right way is to insert instead

CONFIG += c++11

in your .pro file. Two or three necessary changes are then made by qmake:

  1. -std=c++11 is inserted.
  2. -stdlib=libc++ is inserted.
  3. If you're on a Mac, -mmacosx-version-min=10.6 becomes -mmacosx-version-min=10.7. (Perhaps some similar change is necessary on other OSes or OS versions.)

(A similar issue at 1 and 2.)

Solution 4 - Qt

I'm using Snow Leopad 10.6.8 and gcc 4.9, I had to use

CONFIG += c++11

instead of

QMAKE_CXXFLAGS += -std=c++11

The latter was simply not recognized.

Solution 5 - Qt

CONFIG += c++11 

in .pro file appears to work for me with the Qt4 SDK after installing qt5-default on my Ubuntu desktop:

sudo apt install qt5-default

Anyway the generated makefile contains a -std=c++0x option I suspect to be sufficient to compile my C++11 code.

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
QuestionNatalia ZońView Question on Stackoverflow
Solution 1 - QtTheDarkKnightView Answer on Stackoverflow
Solution 2 - QtBЈовићView Answer on Stackoverflow
Solution 3 - QtCalafView Answer on Stackoverflow
Solution 4 - QtWall-EView Answer on Stackoverflow
Solution 5 - QtPascal SéguyView Answer on Stackoverflow