How to Enable C++11 in Qt Creator

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:

  1. Open a new terminal and create an empty folder Foo/ in your Desktop: ~$ cd Desktop; mkdir Foo/
  2. Change to that folder: ~$ cd ~/Desktop/Foo
  3. Create 2 files: ~$ touch Foo.pro main.cpp
  4. 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.
  5. Go to File -> Open project and select the Foo.pro file.
  6. Keep the default paths to debug and release mode, and hit continue, or ok (can't remember exactly)
  7. 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

  1. 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.
  2. Under the Build option, untick shadow build and under the Run option, untick Run in terminal.
  3. Go back to your main.cpp file and type a simple hello program. Press Ctr+S (to save), Ctrl+B (to build the project) and Ctrl+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:

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

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



Leave a reply



Submit