根据 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 协程吗?
最佳答案
请注意,第一个错误是在 GCC 标准库中,从中可以推断出 -fcoroutines
选项适用于 GCC 而不是 Clang。
要使用 Clang libc++ 构建,您需要添加选项 -stdlib=libc++
反而。但这会导致 <coroutine>
没有找到头文件。
由于 Clang 协程仍处于“实验”阶段,因此您必须包含 <experimental/coroutine>
.
所以有两件事你需要改变:
-stdlib=libc++
) #include <experimental/coroutine>
) 还要注意,由于协程是实验性的,头文件中定义的符号将在
std::experimental
中。命名空间。
关于c++ - Clang 中 C++20 协程支持的状态如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68540816/