How to enable C++11 in Qt Creator?
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.
How to use C++11 in Qt creator 4.8
This is the simplest approach to creating a qt project:
- Open a new terminal and create an empty folder
Foo/
in your Desktop:~$ cd Desktop; mkdir Foo/
- Change to that folder:
~$ cd ~/Desktop/Foo
- Create 2 files:
~$ touch Foo.pro main.cpp
- If you have exported the correct paths in your
.profile
or.bashrc
, then you can type~$ qtcreator
in your terminal to run qt. Otherwise, click on the QtCreator App from the applications. - Go to
File -> Open project
and select theFoo.pro
file. - Keep the default paths to debug and release mode, and hit continue, or ok (can't remember exactly)
- You should have the
Foo.pro
file open in Qt. Now enter the following lines in the Foo.pro file and hit save (Ctrl+S).
CONFIG += c++11 console
TARGET = My_Foo
SOURCES += main.cpp
- Once you hit save, you should see the main.cpp file on the left pane that lists the project files. If so, click on the Projects button (spanner tool) on the left menu.
- Under the
Build option
, untickshadow build
and under theRun option
, untickRun in terminal
. - Go back to your main.cpp file and type a simple hello program. Press
Ctr+S
(to save),Ctrl+B
(to build the project) andCtrl+R
(to run).
Up until step 7, you can do it using the GUI, by selecting Create new Project and then select console application. Btw, on mac use Cmd instead of Ctrl.
Also make sure you have the correct compilers setup. (https://doc.qt.io/qtcreator/creator-tool-chains.html)
C++ Qt - How to add -std=c++11 to the makefile which is generated by qmake?
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.)
(A similar issue at 1 and 2.)
Qt: CONFIG += C++11, but -std=c++0x
Ok, thanks to your hints I have figured it out.
After I have tried any possible advice from above, with still no success, I have excluded any subproject I could think of in my project. Eventually I have found a QML sample .pro which did not have CONFIG += c++11
defined.
That was causing the error. So the root cause was not the project I was working on, but a subproject which however got compiled in the same step.
Building Qt project for C++11 standard
Use the qmake project file, add this line: QMAKE_CXXFLAGS += -std=c++11
LE: also 4.6.3 might not support C++11 (as far as i know 4.7 and higher support -std=c++11) so the option for the 0x features implemented might be: QMAKE_CXXFLAGS += -std=c++0x
C++11 in QtCreator 3.5.0 on ubuntu
Since qmake seems to use Qt 4.6 instead of Qt5, which supports Config += c++11
, the solution is to add
QMAKE_CXXFLAGS += -std=c++11
in the projects .pro file,as BoBTFish commented. This solves the problem.
Related Topics
Send Mail Using Smtp in C++ on Linux
Headers Including Each Other in C++
Is 'Long' Guaranteed to Be at Least 32 Bits
What Default Promotions of Types Are There in the Variadic Arguments List
What's the Meaning of Exception Code "Exc_I386_Gpflt"
What Can and Can't I Specialize in the Std Namespace
Why Does Gcc Compiler Output Pow(10,2) as 99 Not 100
How Std::Bind Works with Member Functions
C++ Portability Between Windows and Linux
How to Write Log Base(2) in C/C++
Disambiguate Class-Member in Multiple Inheritance
Purpose of Trigraph Sequences in C++
Difference Between *(Pointer + Index) and Pointer[]