Can't use c++17 features using g++ 7.2 in QtCr

2020-02-11 17:37发布

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.

3条回答
干净又极端
2楼-- · 2020-02-11 18:00

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.

查看更多
Anthone
3楼-- · 2020-02-11 18:08

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?"

查看更多
贪生不怕死
4楼-- · 2020-02-11 18:11

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();
}
查看更多
登录 后发表回答