如何解决Clang 中 C++20 协程支持的状态如何?
根据 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>
。
所以你需要改变两件事:
- 使用 Clang libc++ (
-stdlib=libc++
) - 包括实验头文件 (
#include <experimental/coroutine>
)
另请注意,由于协程是实验性的,头文件中定义的符号将位于 std::experimental
命名空间中。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。