Can't use c++17 features using g++ 7.2 in QtCreator
Asked Answered
A

4

27

I have recently updated gcc and g++ to version 7.2. I would like to try out std::experimental::any and std::variant in particular, and I am using Qt 5.9.1 in QtCreator.

So far I have written this in the project file:

CONFIG += c++17

And I have added the correct headers in the correct places:

#include <variant>
#include <experimental/any>

Any works fine, no problems there. However, when I include the variant header file, I get this error:

/usr/include/c++/7/bits/c++17_warning.h:32: error: #error This file requires compiler and library support for the ISO C++ 2017 standard. This support must be enabled with the -std=c++17 or -std=gnu++17 compiler options.

#error This file requires compiler and library support \ ^~~~~

I have tried a variety of things in the project file, here is the full list:

CONFIG += c++17

&

CONFIG += c++1z

&

QMAKE_CXXFLAGS += -std=c++17

&

QMAKE_CXXFLAGS += -std=c++1z

&

CONFIG += c++17
QMAKE_CXXFLAGS += -std=c++17

&

CONFIG += c++1z
QMAKE_CXXFLAGS += -std=c++1z

&

CONFIG += c++11
CONFIG += c++14
CONFIG += c++17

That's every stab in the dark I could think of. What am I missing? And why does experimental::any compile when variant doesn't?

I know I shouldn't use CONFIG += c++xx and QMAKE_CXXFLAGS together in this way, but I thought I'd give it a go as nothing else works. For bonus points, I'm also wondering, should I add the CONFIG calls for 14 and 11 when I already CONFIG for 17?

EDIT:

Here is the compiler output with most of my filenames scrubbed out:

18:04:10: Running steps for project AIQt...
18:04:10: Configuration unchanged, skipping qmake step.
18:04:10: Starting: "/usr/bin/make" 
/home/pete/Qt/5.9.1/gcc_64/bin/qmake -o Makefile ../AIQt/AIQt.pro -spec linux-g++ CONFIG+=debug CONFIG+=qml_debug
WARNING: Failure to find: ../src/stdafx.h
WARNING: Failure to find: ../src/Csound/csd.h
g++ -c -pipe -g -Wall -W -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_QML_DEBUG -DQT_DATAVISUALIZATION_LIB -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../AIQt -I. -I../src -I../src/AIBase -I../src/Maths -I../src/Random -isystem /usr/local/include/csound -I../../../../Qt/5.9.1/gcc_64/include -I../../../../Qt/5.9.1/gcc_64/include/QtDataVisualization -I../../../../Qt/5.9.1/gcc_64/include/QtWidgets -I../../../../Qt/5.9.1/gcc_64/include/QtGui -I../../../../Qt/5.9.1/gcc_64/include/QtCore -I. -isystem /usr/include/libdrm -I. -I../../../../Qt/5.9.1/gcc_64/mkspecs/linux-g++ -o main.o ../AIQt/main.cpp
In file included from /usr/include/c++/7/variant:35:0,
                 from ..###,
                 from ..###,
                 from ..###,
                 from ..###,
                 from ..###,
                 from ..###,
                 from ..###,
                 from ..###:
/usr/include/c++/7/bits/c++17_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2017 standard. This support must be enabled with the -std=c++17 or -std=gnu++17 compiler options.
 #error This file requires compiler and library support \
  ^~~~~
In file included from ..###,
                 from ..###
                 from ..###,
                 from ..###,
                 from ..###,
                 from ..###,
                 from ..###:
../src/AIBase/Geno.h:70:18: error: ‘variant’ in namespace ‘std’ does not name a type
             std::variant m_valueVariant;
                  ^~~~~~~
In file included from ..###,
                 from ..###,
                 from ..###,
                 from ..###,
                 from ..###,
                 from ..###:
../src/AIBase/Pheno.h:22:13: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
             const double getGenoValue(size_t genoIndex) const;
             ^~~~~
../src/AIBase/Pheno.h:24:13: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
             const UserRating getRating() const;
             ^~~~~
In file included from ..###,
                 from ..###:
../AIRadioQt/GraphDialog.h:16:15: warning: declaration ‘struct ar::ai::ClusterList’ does not declare anything
 class ar::ai::ClusterList;
               ^~~~~~~~~~~
make: *** [main.o] Error 1
18:04:13: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project AIQt (kit: Qt 5.9.1 GCC 64bit)
The kit Qt 5.9.1 GCC 64bit has configuration issues which might be the root cause for this problem.
When executing step "Make"
18:04:13: Elapsed time: 00:03.

ANSWER:

As mentioned by nwp, I just had to clean it and rebuild.

Another poster also commented that CONFIG += c++17 doesn't appear to be supported yet, so it is necessary to use QMAKE_CXXFLAGS += -std=c++17. He quickly deleted his comment though, so I am unable to thank him personally for going to the effort of checking the docs for me.

Almeida answered 6/10, 2017 at 17:26 Comment(8)
In the bottom there should be a tab "Compile Output". What flags starting with "-std=" are there? Note that there may be multiple of them and only the last one counts.Duel
That's a good point, I didn't think to check. It mentions no -std= flags. Do I need to add the CONFIG line in a specific place? Compiler output is added to question...Almeida
Rightclick on your project and select "clean" and then "Run qmake". For reasons I will never understand changing the .pro file doesn't always update the makefile properly.Duel
Hot damn! As long as I'm using QMAKE_CXXFLAGS += -std=c++17, and not the CONFIG equivalent, it works after I cleaned and rebuild. Thanks man! Someone did briefly write a comment about the docs not showing support for CONFIG += c++17. He deleted his comment, but if he's reading this, that was a helpful thing to say. Feel free to write that comment again and I'll upvote it, haha. But yes, rebuilding was the answer, thanlk very much nwp.Almeida
Don't include the answer in the question. Instead, post an answer and accept it.Hypersthene
Right you are, I'll post it when I get home.Almeida
ClangCodeModel still has a lot of errors parsing C++17 headers. Hopefully they get clang 5.0 included in the build soon.Maharani
Another paranoic way is to delete all .o *.exe Makefile etc, check compiler (left bottom corner) and run qmake, rebuild project. It is better to clean do delete make-files IMHO ...Murphree
L
43

CONFIG += c++17 can be used with Qt 5.12 and later.

For Qt 5.11 and earlier, it is not a recognized QMake flag and you have to get your hands a bit dirty.

Adding QMAKE_CXXFLAGS += -std=c++17 does the job for GCC & Clang; for MSVC you will probably need to specify /std:c++17 or /std:c++latest.

Lotus answered 9/3, 2018 at 10:58 Comment(6)
This is true, but it was the clean and build again that actually fixed my issue, so anyone reading this, remember to do that tooAlmeida
QtCreator 4.6 and Qt 5.10 with both CONFIG += c++17 and QMAKE_CXXFLAGS += -std=c++17, works here.Shriner
@Shriner I can't use <filesystem> when I add those. Are you sure it works for things that only exist in C++17?Martensite
@Shriner maybe it's build-tools specific. For MSVC, I had to change the second one to QMAKE_CXXFLAGS += /std:c++17 -- I got the correctly formatted MSVC C++17 flag by inspecting the build output of a VS2017 C++ project that was explicitly set to c++17.Martensite
Needed to use <optional> with msvc and adding CONFIG += c++17 worked for me on Qt 5.12.1 and QtCreator 4.8.1Callum
CONFIG += c++17 doesn't get the job done for me with Qt 5.12.1 on Windows 10 with MinGW 7.3.0 64-bit. However, QMAKE_CXXFLAGS += -std=c++17 still gets the job done.Connoisseur
S
12

Edit 3/2019: You can use CONFIG += c++17 since Qt 5.12.


The actual flag is c++1z, not c++17. In short, to get C++17 support, you don't need to modify QMAKE_CXXFLAGS and can instead simply use CONFIG += c++1z.

Discussion on the reason why can be found in this bug report, but it pretty much amounts to "we implemented it as c++1z before C++17 was standardized, and now we won't bother aliasing it."

Note: I realize you just needed a clean and rebuild. I'm answering the underlying question of "what flags do I need to use to enable C++17 support?"

Suggestibility answered 10/7, 2018 at 11:30 Comment(4)
Neither work with Qt 5.12.4 and LLVM 8.0.0 using clang-cl. I don't know how to use the normal clang cli instead.Sealskin
According to gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html, c++1z is deprecated. c++17 is preferedConvoy
@Convoy That's for gcc. These are flags fed to QtCreator that determine what flags get sent to the compiler, be it gcc, MSVC, et cetera.Suggestibility
<thumbs_up>I have Qt 5.11.3 and the "CONFIG += c++1z" worked. Creator version 4.8.2 </thumbs_up>Gumption
F
3

By Example C++17 C++20 Builds with Qt5 version 5.15.3 (and above) on Linux (or similar on Windows)

Summary: Inside CMakeLists.txt file for an application written in C++17 and using Qt

cmake_minimum_required(VERSION 3.16.0)
project(helloworld VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

A qt project configuration eg mygame.pro may have c++1z for C++17 or c++2a for C++20 or c++11 for older projects:

CONFIG += c++1z

On the command line of configure eg a C++20 project config, always use a shadow build:

../configure -c++std c++2a
cmake --build .. --parallel

Windows or Linux compiler command arguments examples with or without qt; at time of writing C++20 is c++2a and C++17 is c++1z others to follow.

$ g++ -std=c++2a hello_linux_is_the_worlds_device_future.cpp  
c:\hello> cl /std=c++1z /EHsc hello_windows_is_in_decline.cpp

On windows notice the forward /slash.

More Detail: I have found that building a new C++ project or even building ALL of qt5 open source thus creating my own qt release (which is tough) is possible by C++17 or C++20 (Qt6 requires at least C++17). I cloned and then checked out version qt5 5.15 i.e. ALL of qt5 open source and built it using C++20 - the build takes 23GB ... you don't need to do all that.

Note the config examples below, ... 4 hours of build on a fast PC are super fussy and I managed eventually. Anyway the point is that Qt5.15 was happy to build itself with the C++17 or C++20 settings (in my case using gcc g++-8 or g++-9) by example:

  1. Just take a look at this cmake Get Started (qt6 C++17 example) but 20 works just as well)

  2. Qt5 open source code total build of qt5.15.3 with C++20 configure example:-

    mkdir qt5-build

    cd qt5-build

    ../configure -release -opensource -nomake examples -nomake tests
    -skip qtdocgallery -c++std c++2a -confirm-license -opengl desktop
    -platform linux-g++-64 # put this all on one-line please.

    cmake --build .. --parallel

OR use make -j4 as I did.

Considerable care was taken to write this for my own notes which I share, I am happy to make corrections myself and suggestions are welcome; never ever hack into an authors work and change it! Just as I do not go into a public library and remove pages from books. Examples count more than non-solutions, however examples age faster!

Fiertz answered 9/1, 2021 at 21:8 Comment(0)
R
2

For Windows: I'm download qt-opensource-windows-x86-5.11.1, icncluded MinGW32. For MinGW64 I'm download qt-5.5.0-x64-mingw510r0-seh-rev0 and install only compiler. Configure QtCreator, as says here. Create new project and add QMAKE_CXXFLAGS += -std=gnu++1z to .pro file (gcc doc). For test, try compile this simple code:

#include <optional>

std::optional<int> foo() 
{
    return std::nullopt;
}

int main(int argc, char *argv[])
{
    foo();
}
Rosemaryrosemond answered 7/7, 2018 at 21:49 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.