Can Qt4 applications be built with C++11?
I'm trying to use C++14 (or C++11) in a small Qt4 application. I have, for example,
instead of the usual
Compiling with g++ (Arch Linux, upgraded about a month ago) gives errors (copied by hand; copy/paste not working with my xterm today):
Code:
x.cpp:31:2: warning: 'auto' changes meaning in C++11; please remove it
^
x.cpp:31:7: error: 'layout' does not name a type
^
Perhaps there's something wrong with the way I wrote the line with 'auto', or perhaps Qt4 isn't compatible with using C++11. I did find documentation for qmake (I'm actually using qmake-qt4) and the CONFIG variable, so I put this into the .pro file:
Is there more I need to do? Am I a fool to try using C++11 or C++14?
Does Qt5 deal with C++11 the same way as Qt4?
Re: Can Qt4 applications be built with C++11?
This doesn't look like it has to do with Qt4 or Q5, the compiler complains about auto.
But you can easily verify that by using "new int" instead.
What you need to ensure, however, is that the C++ compiler is aware of your usage of C++11/14.
For GCC that is the compiler flag -std=c++0x
Build system generators can have support for doing that correctly for each compiler they support, for example Qt5's qmake can do that by specifying
CONFIG += c++11
In CMake you can do
set(CMAKE_CXX_STANDARD 11)
Cheers,
_
Re: Can Qt4 applications be built with C++11?
Hello,
yes, you can use C++11 together with Qt4. Of course Qt4 classes do not support C++11 "features" like using initializer lists in constructors. However, it is possible to use e.g. range based loops on Qt4 containers like QVector or QList. To enable C++11 compiler flag (as anda_skoa already pointed out) via qmake use
Code:
QMAKE_CXXFLAGS += -std=c++11
or
Code:
QMAKE_CXXFLAGS += -std=c++0x
depending on your compiler.
Best regards
ars