58

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

5

5 Answers 5

78

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
7
  • 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 '{...}' Oct 16, 2013 at 8: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. Oct 16, 2013 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). Jan 22, 2015 at 7: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 8, 2015 at 2: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 Oct 21, 2015 at 13:05
23

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

3
  • @Wildling So you used CONFIG += c++11 and you got c++11 compilation issues?
    – Calaf
    Oct 26, 2014 at 14:23
  • Thanks for explaining why QMAKE_CXXFLAGS is insufficient. +1
    – blackbird
    May 6, 2015 at 18:25
  • Tweaking CONFIG does not work for Qt4. Haven't tested for Qt5. Aug 25, 2015 at 10:06
23

You can change CXX flags :

QMAKE_CXXFLAGS += -std=c++11

I usually set it as :

QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -pedantic
3
  • 4
    Definitely do not use this. Dec 13, 2014 at 12:50
  • @becko "Definitely" is a bit strong, but specifying switches like that will not be portable. It'll work on gcc, and probably clang, but not on MSVC.
    – hyde
    Sep 30, 2015 at 7:02
  • Worked for me! Centos, Qt4
    – Valentin H
    Sep 12, 2016 at 16:21
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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.