c++ - 无法在 QtCreator 中使用 g++ 7.2 使用 c++17 功能

标签 c++ qt-creator c++17 variant

我最近将 gcc 和 g++ 更新到了 7.2 版。我想特别尝试 std::experimental::anystd::variant,我在 QtCreator 中使用 Qt 5.9.1。

到目前为止,我已经在项目文件中写了这个:

CONFIG += c++17

我已经在正确的地方添加了正确的标题:

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

任何工作都很好,没有问题。但是,当我包含变体头文件时,我收到此错误:

/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 此文件需要编译器和库支持\ ^~~~~

我在项目文件中尝试了很多东西,这里是完整列表:

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

这是我能想到的所有黑暗中的刺伤。我错过了什么?为什么 experimental::any 编译而变体不编译?

我知道我不应该以这种方式将 CONFIG += c++xxQMAKE_CXXFLAGS 一起使用,但我想我还是试试吧其他作品。对于奖励积分,我还想知道,当我已经配置 17 时,是否应该添加 14 和 11 的配置调用?

编辑:

这是我的大部分文件名被清除的编译器输出:

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.

回答:

正如 nwp 所说,我只需要清理并重建它。

另一位发帖人也评论说 CONFIG += c++17 似乎还不支持,所以需要使用 QMAKE_CXXFLAGS += -std=c++17 。不过他很快删除了他的评论,所以我无法亲自感谢他为我检查文档的努力。

最佳答案

CONFIG += c++17 可用于 Qt 5.12 及更高版本。

对于 Qt 5.11 及更早版本,it is not a recognized QMake flag而且你得弄脏你的手。

添加 QMAKE_CXXFLAGS += -std=c++17 为 GCC 和 Clang 完成工作;对于 MSVC,您将 probably need to specify /std:c++17/std:c++latest.

关于c++ - 无法在 QtCreator 中使用 g++ 7.2 使用 c++17 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46610996/

相关文章:

C++ 条件

c++ - 如何修复 Turbo C++ 错误 "Cannot open include file: graphics.h: no such files or director"

c++ - 什么会导致 "corrupted double-linked list"错误?

Qt Creator 2.7.1 和 Qt 4.8.4 无法远程调试库

c++ - 使用指向方法的指针的 MSVC 编译器 fatal error C1001

c++ - 负数的按位运算会导致ub吗?

c# - QNetworkAccessManager->在 DLL 中调用时卡住

c# - c++调用带有Hashtable参数的c#DLL

string - Qt creator 调试字符串(显示内容)

Qt 调试不会在断点处停止