【问题标题】:What is the status of C++20 coroutines support in Clang?Clang 中对 C++20 协程的支持情况如何?
【发布时间】:2021-10-03 00:56:09
【问题描述】:

根据 cppreference.com (https://en.cppreference.com/w/cpp/compiler_support#C.2B.2B20_features) Clang 从版本 8 开始部分支持 C++20 协程:

但如果在 Clang 主干(即将发布的第 13 版)中,我会写

#include <coroutine>

导致错误(https://gcc.godbolt.org/z/rTfjbarKz):

/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/12.0.0/../../../../include/c++/12.0.0/coroutine:334:2: error: "the coroutine header requires -fcoroutines"
#error "the coroutine header requires -fcoroutines"

如果我在命令行中添加 -fcoroutines 标志,那么 Clang 会抱怨(https://gcc.godbolt.org/z/qMrv6nMzE):

clang-13: error: unknown argument: '-fcoroutines'

有什么方法可以在 Clang 中开始使用 C++20 协程?

【问题讨论】:

    标签: c++ c++20 clang++ c++-coroutine


    【解决方案1】:

    请注意,第一个错误出现在 GCC 标准库中,由此可以推断出 -fcoroutines 选项适用于 GCC 而不是 Clang。

    要使用 Clang libc++ 构建,您需要添加选项 -stdlib=libc++。但这会导致找不到 &lt;coroutine&gt; 头文件。

    由于 Clang 协程仍处于“实验”阶段,您必须包含 &lt;experimental/coroutine&gt;

    所以有两点你需要改变:

    • 使用 Clang libc++ (-stdlib=libc++)
    • 包含实验性头文件 (#include &lt;experimental/coroutine&gt;)

    还要注意,由于协程是实验性的,所以头文件中定义的符号将位于 std::experimental 命名空间中。

    【讨论】:

    • 谢谢。正确的包含是#include &lt;experimental/coroutine&gt;(末尾没有's')
    • 你知道clang最终会支持libstdc++的协程吗?或者使用协程是否意味着如果你使用 clang 编译,你总是必须使用 libc++?
    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 2015-09-22
    • 2022-01-07
    • 2012-11-01
    • 2020-12-20
    • 1970-01-01
    • 2018-06-10
    • 2019-05-02
    相关资源
    最近更新 更多