26

I'm currently using C++11 features in my Qt applications. However, I'd like to use some of the new C++14 features in my applications.

To enable C++11 in a Qt application, one only needs to add one line in the qmake project file, namely:

CONFIG += c++11

or this for earlier versions:

QMAKE_CXXFLAGS += -std=c++1y

I already tried to do the same with C++14, but it didn't work. I changed the above mentioned line of the qmake project like this:

CONFIG += c++14

or this for earlier versions:

QMAKE_CXXFLAGS += -std=c++1y

After that, lots of compilation errors, that did not exist before, appear when trying to build the project. The project compiles fine, however, if I try to use any C++14 features, I get a compilation error. This is an example:

template<typename T>
constexpr T pi = T(3.1415926535897932385);

This is the corresponding error:

main.cpp:7: error: template declaration of 'constexpr const T pi'
constexpr T pi = T(3.1415926535897932385);  
          ^

How to enable C++14 features when using a qmake project in QtCreator?

I am using Qt 5.3.2, Qt Creator 3.2.1, and MinGW 4.8.2 32 bit.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
Rickforce
  • 317
  • 1
  • 5
  • 9
  • Also, I edited the question a bit to make it clearer what you are asking. If I understood wrong, please don't hesitate to roll-back. – hyde Sep 30 '14 at 18:36
  • Maybe worth a read too: http://woboq.com/blog/cpp14-in-qt.html – guruz Nov 17 '14 at 16:56

4 Answers4

33

This is now supported properly from Qt 5.4. You just type this into your qmake project file:

CONFIG += c++14

The necessary code change went in the middle of 2014 by Thiago:

Add support for CONFIG += c++14

I have just created the necessary documentation change:

Mention the c++14 CONFIG option in the qmake variable reference

Please note that variable templates are only supported from gcc 5.0, but then your code works just fine. This can be used for testing C++14 with older gcc:

main.cpp

#include <iostream>

int main()
{
    // Binary literals: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3472.pdf
    // Single-quotation-mark as a digit separator: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3781.pdf
    std::cout << 0b0001'0000'0001;
    return 0;
}

main.pro

TEMPLATE = app
TARGET = main
CONFIG -= qt
CONFIG += c++14
SOURCES += main.cpp

Build and Run

qmake && make && ./main

Output

257
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • Just tried this on a Mac with the about box info below and, looking at the compiler command line, oddly it's actually missing the -stdlib=libc++ flag? It looks like (redacted): `clang++ -c -pipe -g -isysroot /Applications/Xcode.app/... -mmacosx-version-min=10.7 -std=c++1y -Wall -W -fPIE` `Qt Creator 3.3.0 (opensource) Based on Qt 5.4.0 (Clang 6.0 (Apple), 64 bit) Built on Dec 8 2014 at 15:34:58 From revision d36c4d87db` Any ideas what's wrong? I added the flag as a QMAKE_CXXFLAGS and everything works just fine. (yay make_unique<>!) – Chris Subagio Feb 07 '15 at 19:46
9

Qt Creator is just an IDE.

You can think of IDEs as "smarter text editors" that aid the developer with debugging, building, code completion, file management and so on.

IDEs are irrelevant during compilation.

What matters is your compiler. And it is independent from your IDE.

g++ 4.8.x does not support many C++14 features: check out this page to learn what C++14 features are supported.

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • 4
    Actually this question is *not* so much about compiler, it is about *qmake*... (which does not have built-in support to enable C++14, at least not yet). – hyde Sep 30 '14 at 18:24
  • gcc actually did not support the full c++11 feature set. As example constexpr is until now not full implemented ( actual compiler throws error with message "is not yet implemented" in some cases ) – Klaus Sep 30 '14 at 18:31
  • @hyde, support of new standards is the compilers feature, it's not related to qmake – Max Go Sep 30 '14 at 18:31
  • 2
    @N1ghtLight: the OP is asking how to get it working with qmake! – László Papp Dec 13 '14 at 17:50
7

For some weird reason this is what Qt does: If the compiler is gcc or mingw, CONFIG+=C++14 is transformed to a compiler flag -std=c++14 (that what you expect)

If it's another compiler (like clang), CONFIG=C++14 is stupidly transformed to -std=c++11 (sic!), so you will get errors about unsupported features, even if your clang version correctly supports C++14.

To fix it, specify the flag explicitly:

QMAKE_CXXFLAGS += -std=c++14

This way, you're sure that your compiler (g++, mingw or clang) will receive the correct flags.

mindriot
  • 5,413
  • 1
  • 25
  • 34
pierre
  • 71
  • 1
  • 1
  • 1
    Nice find!!! Exactly my issue compiling for android, it was forcing over to C++11 which doesn't have make_unique<>. Thanks! – Luke Dupin Jun 25 '16 at 02:07
  • I tried `CONFIG=C++14` but that didn't work. Then I tried your answer and that fixed the issue. Thanks. – jignatius Nov 12 '19 at 16:31
4

To use C++14 with qmake with versions before Qt 5.4 (it doesn't make any difference wether you use it with Qt Creator or with some other IDE or from command line) and gcc, add this to your .pro file:

QMAKE_CXXFLAGS += -std=c++1y

qmake got support for CONFIG += c++14 with Qt 5.4, so you can use that for projects where you are comfortable with requiring at least that version of Qt. Be sure to either use the explicit compiler flags, or use the CONFIG option, but not both at the same time, to avoid conflicting switches.


Unfortunately gcc 4.8.2 supports very few C++14 features (see here), but you can test that with this code snippet:

#include <iostream>

auto f() { return 42; }

int main()
{
    std::cout << f() << std::endl;
}

That will compile fine with QMAKE_CXXFLAGS += -std=c++1y, but give warning with CONFIG += c++11 or QMAKE_CXXFLAGS += -std=c++1x.

hyde
  • 60,639
  • 21
  • 115
  • 176