C++11 in QtCreator 3.5.0 on ubuntu - c++

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.

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.

Are there some ways to change settings of Qt Creator syntax highlighting?

I'm using Qt Creator 4.6.2 based on Qt 5.11.1 in Ubuntu 18.10 to C++ programming with CONFIG += console c++17 key in .pro file and I got problem with following code:
std::for_each(attributes.begin(), attributes.end(), [&,i{0}](auto it) mutable {compressed.col_id[i] = it.first; i++;});
Qt Creator underlines this code in red and says "expected token ';' got '{'" but compiler runs this code without problems.
Also when I tried to run this code:
int i = 0;
std::for_each(attributes.begin(), attributes.end(), [&](auto it) mutable {compressed.col_id[i] = it.first; i++;});
Qt Creator not underlined it.
I think it could be some problems with parsing in IDE so how I can deal with it?
Try to use the ClangCodeModel for C++. It might be experimental in your version but I´ve used it there as well. Might be slow though.
Clang Code Model for C++ was upgraded to libclang 6.0 and switched on by default in QtCreator 4.7.0, so anyone with 4.7.0 or higher shouldn't have this problem.
I find that the OPs code doesn't compile for a different reason... it.first should be just it.
But after changing that, the code compiles fine with:
CONFIG += c++14
or with
CONFIG += c++1z
without any complaints from the syntax highlighter

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++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!

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

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.