【问题标题】:Is clang-11's invocation of <coroutine> headers via -fcoroutines flag?clang-11 是否通过 -fcoroutines 标志调用 <coroutine> 标头?
【发布时间】:2021-02-18 13:15:24
【问题描述】:

我正在尝试通过命令编译使用coroutine 库的.cpp 文件。

clang-11 -std=c++20 -stdlib=libstdc++ main.cpp 

我收到这样的错误:

/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/coroutine:295:2: error: "the coroutine header requires -fcoroutines"
#error "the coroutine header requires -fcoroutines"

所以,我添加了标志:

clang-11 -std=c++20 -stdlib=libstdc++ main.cpp -fcoroutines

现在,我得到了错误:

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

这是一个错误吗?

最接近的问题是here。但是,我无法断定是否存在错误。

关于它的价值,这里是来源:

#include <iostream>
#include <coroutine>

template<typename T>
bool is_prime(T number) {
    for(int i=2;i<number;i++) {
        if (not i%number) return true;
    }
    return false;
}

class prime_iterator {
    unsigned int number = 2;
public:
    auto operator*() const {
        return number;
    }

    prime_iterator& operator++() {
        ++number;
        if (not is_prime(number)) {
            co_yield number;    // Trying to invoke co_yield just to see if library works.
        }
        return *this;
    }
};

auto main() -> int {
    for(prime_iterator p; *p < 30; ++p) {
        std::cout << *p << " is prime";
    }
}

【问题讨论】:

  • 它适用于 gcc
  • 是的。但我正在努力寻找证据来为 clang 提交错误报告。
  • 对此的任何更新我在 clang 10 上遇到了同样的问题
  • 这是 Pavel 的回复。 bugs.llvm.org/show_bug.cgi?id=48172
  • 我在使用 clang 12 时遇到了这个问题。当我像 Pavel 所说的那样切换到 libc++ 时,甚至没有协程头文件。我得到相同的响应 -fcoroutines-ts clang-12 -std=c++20 -stdlib=libc++ main.cpp main.cpp:2:10: fatal error: 'coroutine' file not found #include ' '

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


【解决方案1】:

clang 应该是 -fcoroutines-ts。除非你正在使用与 libstdc++ 混合的 clang 构建协程。

【讨论】:

  • 这对我在 Godbolt 上不起作用
  • 这仅适用于 libc++ 而不是 stdlibc++
猜你喜欢
  • 1970-01-01
  • 2021-04-08
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 2012-10-15
  • 1970-01-01
  • 2015-11-28
  • 2014-01-14
相关资源
最近更新 更多