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 Answers
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
-
2I 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
-
1What 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
-
3Using 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. 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 abuseQMAKE_CXXFLAGS
. I change that to Qt5, thenunique_ptr
suddenly works, if I only haveCONFIG += c++11
Oct 21, 2015 at 13:05
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
:
-std=c++11
is inserted.-stdlib=libc++
is inserted.- 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.)
-
@Wildling So you used CONFIG += c++11 and you got c++11 compilation issues?– CalafOct 26, 2014 at 14:23
-
-
Tweaking CONFIG does not work for Qt4. Haven't tested for Qt5. Aug 25, 2015 at 10:06
You can change CXX flags :
QMAKE_CXXFLAGS += -std=c++11
I usually set it as :
QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -pedantic
-
4
-
@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.– hydeSep 30, 2015 at 7:02
-
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.
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.
QMAKE_CXXFLAGS += -std=c++11
in your .pro file.