Search code examples
qtc++14qt-creatorqmakeqt5.2

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.

Solution

  • 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.