Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use C++14 features when building qmake projects?

Tags:

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.

like image 818
Rickforce Avatar asked Sep 30 '14 18:09

Rickforce


People also ask

What version of C++ does Qt use?

Qt Creator is not a compiler. When you read that "Qt Creator supports C++11" it means that the code-completion engine (Clang in this case) supports C++11 syntax.

Does Qt support C ++ 14?

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.

Is qmake a compiler?

qmake will use spec as a path to platform and compiler information, and ignore the value of QMAKESPEC.

What is the use of qmake?

The qmake tool helps simplify the build process for development projects across different platforms. It automates the generation of Makefiles so that only a few lines of information are needed to create each Makefile. You can use qmake for any software project, whether it is written with Qt or not.


2 Answers

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.

like image 20
Vittorio Romeo Avatar answered Oct 28 '22 17:10

Vittorio Romeo


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 
like image 69
lpapp Avatar answered Oct 28 '22 17:10

lpapp