不能在QtCreator中使用g ++ 7.2使用c ++ 17功能

问题描述 投票:15回答:3

我最近更新了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的CONFIG调用吗?

编辑:

这是编译器输出,我的大多数文件名被清除:

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。他很快删除了他的评论,所以我无法亲自感谢他为我检查文档的努力。

c++ qt-creator c++17 variant
3个回答
19
投票

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


6
投票

编辑3/2019:从Qt 5.12开始,您可以使用CONFIG += c++17


实际的旗帜是c++1z,而不是c++17。简而言之,要获得C ++ 17支持,您不需要修改QMAKE_CXXFLAGS,而只需使用CONFIG += c++1z

讨论之所以可以成为found in this bug report的原因,但它几乎相当于“我们在C ++ 17标准化之前将它实现为c++1z,现在我们不会打扰别名了。”

注意:我意识到你只需要一个干净的重建。我正在回答“我需要使用哪些标志来启用C ++ 17支持?”的基本问题。


1
投票

对于Windows:我下载qt-opensource-windows-x86-5.11.1,icncluded MinGW32。对于MinGW64,我下载qt-5.5.0-x64-mingw510r0-seh-rev0并仅安装编译器。正如here所说,配置QtCreator。创建新项目并将QMAKE_CXXFLAGS + = -std = gnu ++ 1z添加到.pro文件(gcc doc)。对于测试,请尝试编译这个简单的代码:

#include <optional>

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

int main(int argc, char *argv[])
{
    foo();
}
© www.soinside.com 2019 - 2024. All rights reserved.