56

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

Ilya
  • 4,300
  • 4
  • 23
  • 44
Natalia Zoń
  • 921
  • 2
  • 12
  • 20
  • possible duplicate of [CXXFLAGS modification From My .pro File](http://stackoverflow.com/questions/5837106/cxxflags-modification-from-my-pro-file) – Basile Starynkevitch Oct 16 '13 at 08:24
  • 3
    add `QMAKE_CXXFLAGS += -std=c++11` in your .pro file. – thuga Oct 16 '13 at 10:34
  • possible duplicate of [How to enable c++11 in qt creator 2.7.0?](http://stackoverflow.com/questions/16948382/how-to-enable-c11-in-qt-creator-2-7-0) – Ali Oct 16 '13 at 10:57
  • 1
    Which Qt version are you using? – Frank Osterfeld Oct 16 '13 at 13:19
  • possible duplicate of [Configuring the GCC compiler switches in Qt, QtCreator, and QMake](http://stackoverflow.com/questions/2987062/configuring-the-gcc-compiler-switches-in-qt-qtcreator-and-qmake) – lpapp Oct 19 '13 at 06:04

5 Answers5

76

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
TheDarkKnight
  • 25,660
  • 4
  • 50
  • 79
  • 2
    I added this, ran qmake and rebuilded the project, but I'm still getting the same error (it's regarding vector initialization), and I don't see the c++11 in g++. ../SignalAnalyzer/TestConfiguration.cpp:12:61: error: in C++98 'TestConfiguration::measurementPoints' must be initialized by constructor, not by '{...}' – Natalia Zoń Oct 16 '13 at 08:48
  • Can you add the full error to the question? You only asked where to add the config command and stated nothing about an error. – TheDarkKnight Oct 16 '13 at 12:48
  • 1
    What if my project does not have a .pro file (when using qtcreator as editor only). Which of the qt generated files do I then modify (modification of .config does not seem to make a difference). – Werner Erasmus Jan 22 '15 at 07:18
  • 3
    Using CONFIG += c++11 appears to only be supported by qmake version 3.0 and later (the version included with Qt5). Earlier versions of qmake seem to ignore this value, so you may have to use QMAKE_CXXFLAGS += -std=c++11 in Qt4 and older. – josmith42 Aug 08 '15 at 02:50
  • I think I can confirm @josmith42's theory. If I have a project with a kit pointing to Qt4.x then I can't use `unique_ptr` unless I abuse `QMAKE_CXXFLAGS`. I change that to Qt5, then `unique_ptr` suddenly works, if I only have `CONFIG += c++11` – Adrian Ratnapala Oct 21 '15 at 13:05
  • With Qt 5.5.1 (from online installer) on Debian Wheezy with GCC 4.7.2-5 I get `Project ERROR: Unknown module(s) in QT: c++11`, probably because 4.7 has more like experimental C++11..? – Vincas Dargis Feb 23 '16 at 08:54
  • strange, I had QMake v3.0, qt5.2.1. The CONFIG+=c++11 was not enough, it actually required QMAKE_CXXFLAGS += -std=c++11 – Wang Sep 03 '17 at 13:07
23

You can change CXX flags :

QMAKE_CXXFLAGS += -std=c++11

I usually set it as :

QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -pedantic
BЈовић
  • 57,268
  • 38
  • 158
  • 253
20

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

Community
  • 1
  • 1
Calaf
  • 8,037
  • 8
  • 46
  • 96
3

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.

Wall-E
  • 491
  • 3
  • 14
0
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.