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

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.

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

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.

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.

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.

Related

C++20 in Qt Creator

I want to use std::source_location in Qt Creator, I've built the GCC 11.1 and checked it using g++ main.cpp -std=c++20 that it works. I've created a kit in Qt Creator to use this compiler and I was able to build it by adding:
QMAKE_CXXFLAGS += -std=c++2a
It works and I can even debug it without issues but Qt keeps saying that I have an error (which is not an error because the build passes):
error: no type named 'source_location' in namespace 'std'
Is there something I can do to fix this?
It's all about clang's static analysis. The gcc's (or strictly libstdc++'s) header <source_location> contains following guard:
#if __cplusplus > 201703L && __has_builtin(__builtin_source_location)
The guard doesn't allow clang to work as it doesn't support __builtin_source_location. Please follow the clang PR that fixes this issue.

Adding MAKEFLAGS from MPC(The Makefile, Project, and Workspace Creator)

I am using MPC to generate my makefiles. I generate successfully the Makefile but when I run make I get the following error:
error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
My question is, how to add build flags (eg:-std=c++11) to my MPC?
For completion here is my MPC file:
project(Makefile) : dcpsexe, dcps_tcp {
requires += no_opendds_safety_profile
exename = start
after += *idl
TypeSupport_Files {
Communication.idl
}
Source_Files {
ListenerReader.cpp
Publisher.cpp
Subscriber.cpp
main.cpp
}
}
This can be done through the $ACE_ROOT/include/makeinclude/platform_macros.GNU file. Add a line with c++11=1 to the top of this file, that will enable C++11 support for the compiler.
Alternatively to enabling C++11 (or later) specifically for the platform this can be achieved on project basis, too, e.g.
specific(make) {
compile_flags += -std=c++20
}
make has been selected according to MPC documentation, I haven't tested myself (but did for a MSVC project where it worked seamlessly) – possibly the undocumented gcc (or clang) compiler options might be supported as well (the equally undocumented vs2019 and vs2022 ones definitely are so), you'll have to try yourself...

warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]

I'm using Qt5 with Clang on Debian Jessie. To experiment with generic lambdas, in the .pro file there is:
CONFIG += c++14
And after building I got:
warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
To get rid of this obvious message I did:
QMAKE_CXXFLAGS += -Wc++11-extensions
But I keep getting the obvious message. Why? How to hide it?
According to qmake's repository history, the CONFIG += c++14 stanza was added in qmake version 5.4: https://codereview.qt-project.org/#/c/87831/
However, it seems Debian Jessie only has qmake version 5.3 (https://packages.debian.org/jessie/qt5-qmake)
As a workaround, you can use
QMAKE_CXXFLAGS += -std=c++14
or
QMAKE_CXXFLAGS += -std=gnu++14
you want QMAKE_CXXFLAGS+=-Wno-c++11-extensions I suspect.
clang compiler documentation
pertinent part:
-Wfoo: Enable warning foo.
-Wno-foo: Disable warning foo.

C++11 in QtCreator 3.5.0 on ubuntu

Problem: QtCreator seems to not compile with C++11 standard though Config += c++11 is set in the projects .pro file.
Background: The global definition of the scoped enums are in a seperate header file global_definitions.h:
// ...
enum class dr_items { CROSSHAIR,
GRID,
LABELS,
DATA,
AMOUNT // count element
};
This file produces a warning for every scoped enum, but no error:
/path/global_definitions.h:7: warning: scoped enums only available with -std=c++11 or -std=gnu++11
enum class dr_items { CROSSHAIR,
^
The error occurs on the first usage of the scoped enum in file oscscene.cpp:
#include "oscscene.h"
#include "global_definitions.h"
// ...
for(int i=0;i++;i<dr_items::AMOUNT){
// ...
with the error output
/path/oscscene.cpp:9: error: 'dr_items' is not a class or namespace
for(int i=0;i++;i<dr_items::AMOUNT){
^
Projects .pro file:
QT += core gui
CONFIG += c++11
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
Versions:
OS is xubuntu 15.10
QtCreator version is 3.5.0
Qt version is 5.4.2
G++ version is 5.2.1
Gcc version is 5.2.1
make is version 4.0
qmake is version 2.01a, but seems to refer to Using Qt version 4.8.6 in /usr/lib/x86_64-linux-gnu when called from console.
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.

C++14 support in QtCreator with Clang

How can I enable C++14 support in QtCreator 3.3 using Clang 3.5? I have added a Clang kit and I have added CONFIG += c++14 in my project file. However when using e.g. return type deduction I get the following error:
error: 'auto' return without trailing return type; deduced return types are a C++1y extension
you can use CONFIG += c++14 in .pro file with Qt5.5
but there is a bug with clang, so we need to modify the Qt/5.5/clang_64/mkspecs/features/c++14.prf file,
add this code beforeinclude(c++11.prf) :
contains(QMAKE_LFLAGS_CXX11, -stdlib=libc++) {
QMAKE_CXXFLAGS_CXX11 += -stdlib=libc++
}
I had to go to the Makefile in the build folder and manually replace -std=c++11 with -std=c++14.
Thankfully the Makefile is only written once when you add the kit to the project. I only had to do this once and could build in QtCreator as often as I want.
So now I can use a Clang kit to use all the new c++14 features. As a bonus, I can also use all the c++17 features if I manually set -std=c++1z in the Makefile. Sweet!