code
code

Reputation: 1204

How to enable C++14 with older Qt? Qmake incompatible with C++14

I have Qt 5.2.1 (compiled with gcc 4.8.2) and gcc 4.9.4 (mingw-w64) compiler.

C++11 works fine. Always has, even with 4.8.2 compiler.

I need some C++14 features. I would like to use newest compiler but concern for ABI changes made me get 4.9.2. gcc 4.9.2 supposedly supports make_unique and C++14.

The problem I have is that I can't seem to be able to get C++14 support in Qt Creator.

Tried adding to pro file...did not work.

CONFIG += c++14 

Tried adding to pro file...did not work:

QMAKE_CXXFLAGS_CXX14 = -std=c++14

Tried adding to pro file...did not work:

QMAKE_CXXFLAGS_CXX14 = -std=c++14
QMAKE_CXXFLAGS_GNUCXX14 = -std=c++14

Tried adding to pro file...did not work:

win32-g++ {
   QMAKE_CXXFLAGS_CXX14 = -std=c++14
   QMAKE_CXXFLAGS_GNUCXX14 = -std=c++14
}

Tried adding/editing to this the win32-g++ makespec file... did not work

QMAKE_CXXFLAGS_CXX11 = -std=c++0x
QMAKE_CXXFLAGS_CXX14 = -std=c++14
QMAKE_CXXFLAGS_GNUCXX14 = -std=c++14

Tried adding/editing to this the win32-g++ makespec file... did not work

QMAKE_CXXFLAGS_CXX14 = -std=c++14
QMAKE_CXXFLAGS_GNUCXX14 = -std=c++14

Tried adding/editing to this the win32-g++ makespec file... did not work

QMAKE_CXXFLAGS_CXX11 = -std=c++14
QMAKE_CXXFLAGS_CXX14 = -std=c++14
QMAKE_CXXFLAGS_GNUCXX14 = -std=c++14

Tried adding/editing to this the win32-g++ makespec file... did not work

QMAKE_CXXFLAGS_CXX11 = -std=c++14

I believe that QMake is the issue. Of course, I'm not sure.

EDIT -- ADDED MORE INPUT

Pro file

#-------------------------------------------------
#
# Project created by QtCreator 2018-02-05T13:02:30
#
#-------------------------------------------------

QMAKE_CXXFLAGS += -std=c++1y

QT       += core

QT       -= gui

TARGET = test2
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp

Test Code

#include <QCoreApplication>
#include <iostream>
#include <memory>

using namespace std;

class A{
public:
    A(){cout << "ctorA " << endl;}
    ~A(){cout << "dtorA " << endl;}
};

class X{
    A arr[10];
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    { unique_ptr<X> upX = make_unique<X>(); }

    return a.exec();
}

Compiler output

14:01:39: Running steps for project test2...
14:01:39: Configuration unchanged, skipping qmake step.
14:01:39: Starting: "C:\DTOOLS\QtLib5.2.1-QtCreator3.0.1__T-Win-64b_B-gcc4.8.2-seh-posix-opengl\QtSDK-x86_64\bin\mingw32-make.exe" 
C:/DTOOLS/QtLib5.2.1-QtCreator3.0.1__T-Win-64b_B-gcc4.8.2-seh-posix-opengl/QtSDK-x86_64/bin/mingw32-make -f Makefile.Release
mingw32-make[1]: Entering directory 'C:/DEV/test2'
g++ -c -march=nocona -mtune=core2 -pipe -fno-keep-inline-dllexport -std=c++1y -O2 -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_NO_DEBUG -DQT_CORE_LIB -I. -I"..\..\DTOOLS\QtLib5.2.1-QtCreator3.0.1__T-Win-64b_B-gcc4.8.2-seh-posix-opengl\QtSDK-x86_64\include" -I"..\..\DTOOLS\QtLib5.2.1-QtCreator3.0.1__T-Win-64b_B-gcc4.8.2-seh-posix-opengl\QtSDK-x86_64\include\QtCore" -I"release" -I"..\..\DTOOLS\QtLib5.2.1-QtCreator3.0.1__T-Win-64b_B-gcc4.8.2-seh-posix-opengl\QtSDK-x86_64\mkspecs\win32-g++" -o release\main.o main.cpp
main.cpp: In function 'int main(int, char**)':
main.cpp:21:27: error: 'make_unique' was not declared in this scope
     { unique_ptr<X> upX = make_unique<X>(); }
                           ^
main.cpp:21:40: error: expected primary-expression before '>' token
     { unique_ptr<X> upX = make_unique<X>(); }
                                        ^
main.cpp:21:42: error: expected primary-expression before ')' token
     { unique_ptr<X> upX = make_unique<X>(); }
                                          ^
Makefile.Release:177: recipe for target 'release/main.o' failed
mingw32-make[1]: *** [release/main.o] Error 1
mingw32-make[1]: Leaving directory 'C:/DEV/test2'
Makefile:34: recipe for target 'release' failed
mingw32-make: *** [release] Error 2
14:01:40: The process "C:\DTOOLS\QtLib5.2.1-QtCreator3.0.1__T-Win-64b_B-gcc4.8.2-seh-posix-opengl\QtSDK-x86_64\bin\mingw32-make.exe" exited with code 2.
Error while building/deploying project test2 (kit: QtLib5.2.1_t-Win-64b_B-gcc9.9.4)
When executing step 'Make'
14:01:40: Elapsed time: 00:01.

Upvotes: 2

Views: 1344

Answers (3)

Alexander V
Alexander V

Reputation: 8698

According to this schedule of Qt releases we have 5.2 prior to C++14 which was released on December 15, 2014. While GCC 4.8.2 supports some subset of C++14 that is not C++14 compliant compiler yet. Check on C++ Standard support in GCC.

Very likely you need to upgrade your Qt to at least 5.4 to enable CONFIG += c++14 configuration and also compiler needs to be newer than GCC 4.8.2 and better be at least GCC 5+.

Try with -std=gnu++1y which targets activating C++ 14 features in earlier GCC.

Upvotes: 0

Nikos C.
Nikos C.

Reputation: 51880

I use this in one of my projects. In the .pro file:

CONFIG += c++14

# Qt 5.3 and lower doesn't recognize "c++14". Use c++11 and then replace
# the compiler flags with c++14.
contains(QT_MAJOR_VERSION, 5):lessThan(QT_MINOR_VERSION, 4) {
    CONFIG += c++11
    QMAKE_CXXFLAGS_CXX11 = $$replace(QMAKE_CXXFLAGS_CXX11, "std=c\+\+11", "std=c++1y")
    QMAKE_CXXFLAGS_CXX11 = $$replace(QMAKE_CXXFLAGS_CXX11, "std=c\+\+0x", "std=c++1y")
}

# Qt 4 doesn't even know about C++11, so just add c++14 directly.
contains(QT_MAJOR_VERSION, 4) {
    QMAKE_CXXFLAGS += -std=c++1y
}

If you don't care about Qt4, remove the last part.

Obvious this only works with GCC and Clang. Other compilers would need different handling.

Upvotes: 1

Eelke
Eelke

Reputation: 22023

You are using the wrong variables, the following should work and is the highest supported by 4.9

QMAKE_CXXFLAGS += -std=c++1y

Upvotes: 1

Related Questions