我最近将gcc和g ++更新为7.2版。我想特别尝试std::experimental::any
和std::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++xx
和QMAKE_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
。他很快删除了他的评论,所以我无法亲自感谢他为我检查文档的努力。
答案 0 :(得分:18)
CONFIG += c++17
可以与Qt 5.12及更高版本一起使用。
对于Qt 5.11及更早版本,it is not a recognized QMake flag,你必须弄脏你的手。
添加QMAKE_CXXFLAGS += -std=c++17
可以完成GCC&amp;铛;对于MSVC,您将probably need to specify /std:c++17
或/std:c++latest
。
答案 1 :(得分:7)
编辑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支持我需要使用哪些标志?”这一潜在问题?
答案 2 :(得分:2)
总结: 使用 C++17 编写并使用 Qt 的应用程序的 CMakeLists.txt 文件内部
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)
一个 qt 项目配置,例如 mygame.pro 可能有 c++1z for C++17 或 c++2a for C++20 或 c++11 用于旧项目:
CONFIG += c++1z
在configure 的命令行中,例如 C++20 项目配置,始终使用影子构建:
../configure -c++std c++2a
cmake --build .. --parallel
带有或不带有 qt 的 Windows 或 Linux 编译器命令参数示例;在撰写本文时,C++20 是 c++2a,C++17 是 c++1z,其他要遵循。
$ 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
在 Windows 上注意正斜杠。
更多细节: 我发现通过 C++17 或 C++20(Qt6 需要至少 C++17)。我克隆并检查了 qt5 5.15 版本,即所有 qt5 开源并使用 C++20 构建它 - 构建需要 23GB ......你不需要做所有这些。>
请注意下面的配置示例, ... 在快速 PC 上构建 4 小时非常繁琐,我最终做到了。无论如何,关键是 Qt5.15 很乐意使用 C++17 或 C++20 设置(在我的情况下使用 gcc g++-8 或 g++-9)构建自己,例如:< /p>
看看这个 cmake Get Started (qt6 C++17 example) 但 20 也一样好)
qt5.15.3 的 Qt5 开源代码总构建与 C++20 配置示例:-
mkdir qt5-build
cd qt5-build
../configure -release -opensource -nomake 示例 -nomake 测试
-skip qtdocgallery -c++std c++2a -confirm-license -opengl desktop
-platform linux-g++-64 # 请把这些都放在一行。
cmake --build .. --parallel
或者像我一样使用 make -j4。
我非常谨慎地为我自己的笔记写这篇文章,我分享,我很乐意自己进行更正,欢迎提出建议;永远不要侵入作者的作品并改变它!就像我不会去公共图书馆翻书一样。示例比非解决方案更重要,但示例老化得更快!
答案 3 :(得分:1)
对于Windows: 我正在下载 qt-opensource-windows-x86-5.11.1 ,其中包括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();
}