简体   繁体   English

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

[英]Can't use c++17 features using g++ 7.2 in QtCreator

I have recently updated gcc and g++ to version 7.2.我最近将 gcc 和 g++ 更新到了 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.我想特别尝试std::experimental::anystd::variant ,我在 QtCreator 中使用 Qt 5.9.1。

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 \\ ^~~~~ #error 这个文件需要编译器和库支持\\^~~~~

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?为什么当变体不编译时experimental::any编译?

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.我知道我不应该以这种方式将CONFIG += c++xxQMAKE_CXXFLAGS一起使用,但我想我会QMAKE_CXXFLAGS ,因为其他方法都QMAKE_CXXFLAGS For bonus points, I'm also wondering, should I add the CONFIG calls for 14 and 11 when I already CONFIG for 17?对于奖励积分,我还想知道,当我已经 CONFIG 为 17 时,我是否应该添加 14 和 11 的 CONFIG 调用?

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.正如 nwp 所提到的,我只需要清理它并重建它。

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 .另一位发帖人还评论说,似乎尚不支持CONFIG += c++17 ,因此有必要使用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.不过他很快就删除了他的评论,所以我无法亲自感谢他为我检查文档的努力。

CONFIG += c++17 can be used with Qt 5.12 and later. CONFIG += c++17可用于 Qt 5.12 及更高版本。

For Qt 5.11 and earlier, it is not a recognized QMake flag and you have to get your hands a bit dirty.对于 Qt 5.11 及更早版本,它不是公认的 QMake 标志,您必须弄脏自己的手。

Adding QMAKE_CXXFLAGS += -std=c++17 does the job for GCC & Clang;添加QMAKE_CXXFLAGS += -std=c++17可以完成 GCC 和 Clang 的工作; for MSVC you will probably need to specify /std:c++17 or /std:c++latest .对于 MSVC,您可能需要指定/std:c++17/std:c++latest

Edit 3/2019: You can use CONFIG += c++17 since Qt 5.12.编辑 3/2019:从 Qt 5.12 开始,您可以使用CONFIG += c++17


The actual flag is c++1z , not c++17 .实际标志是c++1z ,而不是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 .简而言之,要获得 C++17 支持,您无需修改QMAKE_CXXFLAGS而是可以简单地使用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."关于原因的讨论可以在这个 bug 报告中找到,但它几乎相当于“在 C++17 标准化之前我们将它实现为c++1z ,现在我们不会打扰它的别名。”

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?"我正在回答“我需要使用哪些标志来启用 C++17 支持?”的基本问题。

For Windows: I'm download qt-opensource-windows-x86-5.11.1 , icncluded MinGW32.对于 Windows:我正在下载qt-opensource-windows-x86-5.11.1 ,包括 MinGW32。 For MinGW64 I'm download qt-5.5.0-x64-mingw510r0-seh-rev0 and install only compiler.对于 MinGW64,我下载qt-5.5.0-x64-mingw510r0-seh-rev0并仅安装编译器。 Configure QtCreator, as says here .配置 QtCreator,如此处所述 Create new project and add QMAKE_CXXFLAGS += -std=gnu++1z to .pro file ( gcc doc ).创建新项目并将QMAKE_CXXFLAGS += -std=gnu++1z 添加到 .pro 文件( 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();
}

By Example C++17 C++20 Builds with Qt5 version 5.15.3 (and above) on Linux (or similar on Windows)通过示例 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摘要:在 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)

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:一个 qt 项目配置,例如 mygame.pro 可能有 C++17 的c++1z或 C++20 的c++2a或旧项目的c++11

CONFIG += c++1z

On the command line of configure eg a C++20 project config, always use a shadow build:配置的命令行上,例如 C++20 项目配置,始终使用影子构建:

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

Windows or Linux compiler command arguments examples with or without qt;带有或不带有 qt 的 Windows 或 Linux 编译器命令参数示例; at time of writing C++20 is c++2a and C++17 is c++1z others to follow.在撰写本文时,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

On windows notice the forward /slash.在 Windows 上注意正斜杠。

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).更多详细信息:我发现,建立一个新的C ++项目,甚至建立QT5开源从而创造我自己的QT版本(这是艰难的)的所有可以通过C ++ 17或C ++ 20(QT6至少需要C + +17)。 I cloned and then checked out version qt5 5.15 ie ALL of qt5 open source and built it using C++20 - the build takes 23GB ... you don't need to do all that.我克隆并检查了 qt5 5.15 版本,即所有 qt5 开源并使用 C++20 构建它 - 构建需要 23GB ......你不需要做所有这些。

Note the config examples below, ... 4 hours of build on a fast PC are super fussy and I managed eventually.请注意下面的配置示例, ......在快速 PC 上构建 4 小时非常繁琐,我最终做到了。 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:无论如何,关键是 Qt5.15 很乐意使用 C++17 或 C++20 设置(在我的情况下使用 gcc g++-8 或 g++-9)构建自己,例如:

  1. Just take a look at this cmake Get Started (qt6 C++17 example) but 20 works just as well)看看这个cmake 入门(qt6 C++17 示例),但 20 也能正常工作)

  2. Qt5 open source code total build of qt5.15.3 with C++20 configure example:- qt5.15.3 的 Qt5 开源代码总构建与 C++20 配置示例:-

    mkdir qt5-build mkdir qt5-build

    cd qt5-build cd qt5-build

    ../configure -release -opensource -nomake examples -nomake tests ../configure -release -opensource -nomake 示例 -nomake 测试
    -skip qtdocgallery -c++std c++2a -confirm-license -opengl desktop -skip qtdocgallery -c++std c++2a -confirm-license -opengl desktop
    -platform linux-g++-64 # put this all on one-line please. -platform linux-g++-64 # 请把这一切都放在一行上。

    cmake --build .. --parallel cmake --build .. --parallel

OR use make -j4 as I did.或者像我一样使用make -j4

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!示例比非解决方案更重要,但是示例老化得更快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM