2

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.
Nikos C.
  • 50,738
  • 9
  • 71
  • 96
code
  • 1,056
  • 10
  • 22

3 Answers3

1

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

QMAKE_CXXFLAGS += -std=c++1y
Eelke
  • 20,897
  • 4
  • 50
  • 76
  • Could you please elaborate. What exactly should I put where. I placed QMAKE_CXXFLAGS += -std=c++1y into the PRO file and it did not work. – code Feb 05 '18 at 13:36
  • It barks on make_unique...saying it is not a member of std. – code Feb 05 '18 at 14:28
  • Code works if take out QT lines and compile at command line with g++. – code Feb 05 '18 at 14:48
  • Did you run qmake after changing the pro file and before building your code? In QT Creator there is a run qmake option in the build menu and you must do this manually it won't do this for you. – Eelke Feb 05 '18 at 17:09
  • In general the order of things in pro file doesn't matter much but I prefer to keep these kind of flags fairly early in the file. – Eelke Feb 05 '18 at 17:12
  • I put it as first thing in pro file. I tired running the run qmake. I tried cleaning project and rebuild all. Didn't work. – code Feb 05 '18 at 17:21
  • When you are building your project the commands executed are shown in the Compile output pane. Could you copy paste atleast one full compiler command from this output into your original question? You could for instance have multiple conflicting standard specifications on the commandline and last one wins... – Eelke Feb 05 '18 at 17:30
  • Thanks, you posted the correct flag. Doesn't quite solve my problems since it seems you can't quite swap compilers despite what the Qt Creator GUI implies, because the it refuses to use a compiler other than the one in its bundle...paths are hard coded in the Qt compiled binaries. AT least for the versions I posted. – code Feb 08 '18 at 14:05
1

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.

Nikos C.
  • 50,738
  • 9
  • 71
  • 96
  • Nikos, where do I make these changes. What files should I put the contains block into? That's some impressive info. How did you come to this knowledge? :) – code Feb 06 '18 at 01:37
  • @code You put this in your project file, after the initial entries. Here's how it looks in mine: https://github.com/realnc/qtads/blob/master/qtads.pro. As to how I know this, I'm not sure. I've been using Qt since 2002 or so, which I guess means I know my way around it :-) – Nikos C. Feb 06 '18 at 02:18
  • Thanks Nikos. I'm still trying to fix this issue. Turns out there are deep seated issues with how Qt and QtCreator work. The Qt Creator GUI implies that you can create kits combining diff Qt builds with diff compilers (obviously there are limits...I was just trying to re-hook a QT lib built with gcc 4.8.2 with a newer GCC 4.9.3 compiler...ABI compatible). But this is not true. It can't do this. Perhaps newer QT Creators can...but my testing shows that Creator 3.0.1 is either buggy or ill conceived. The way Qt libs are packaged is with GCC bins and libs and headers...making it ... – code Feb 07 '18 at 19:23
  • not possible to re-combine with diff version of compiler. You can try to do so via Creator GUI, but it simply ignores compiler you point to and defaults to gcc bundled with its files. I even tried manuallly overwritting the gcc files bundled with it, but it still doesn't work. There is nothing in sys path. It's a mess. It should not be this hard. Qt packaging scheme looks like a mess with compiler files scattered all over tree hierarchy. Not very plug-n-play. Perhaps they improved this in newer versions. – code Feb 07 '18 at 19:27
  • @code I'd suggest completely uninstalling your current Creator and Qt, deleting any left-overs, also deleting configuration files in your appdata, and then downloading the online installer, which will keep you up to date with easy upgrades to new versions. To me, it sounds like you're using outdated versions and on top of that you're in a configuration mess. – Nikos C. Feb 07 '18 at 19:52
  • the prob with the online installer is that non VS compilers are out of date (big time) and gcc is only 32bit. 32bit is unacceptable in 2018. I'd have to recompile Qt and a dozen dependencies. Which is what I tried to avoid due to time restrictions. – code Feb 08 '18 at 14:02
0

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.

Alexander V
  • 8,351
  • 4
  • 38
  • 47
  • What you say is true. This is why I upgraded the compiler to 4.9.4, which does support the C++14 features I need (in particular make_unique). I've also read that Qt 5.4 did something to enable the CONFIG += c++14 configuration, but I'm unable to use Qt higher than 5.3.2 due to license restrictions. That is why I asked the question. It seems that the problem lies with Qmake or makespec or something. c++14 can't be enabled easily despite compiler support. – code Feb 05 '18 at 13:40
  • * but I'm unable to use Qt higher than 5.3.2 due to license restrictions * That sounds strange. What kind of license is that? I work for the organization with Commercial Qt license we just get the new version when we need it. That is second organization which has same Qt Commercial License I work for. – Alexander V Feb 05 '18 at 19:22
  • Not using it under commercial license here. LGPL 2.1. Which allows closed source development as long as you don't modify Qt and dynamically link to it. Can't afford $6000usd per year per developer to get commercial license. 5.3.2 is the last Qt to be under LGPL 2.1. – code Feb 05 '18 at 20:06
  • I just updated my answer with: Try with `-std=gnu++1y` which targets activating C++ 14 features in earlier GCC. – Alexander V Feb 05 '18 at 20:17