首页 > 解决方案 > 是 clang-11 的调用通过 -fcoroutines 标志标头?

问题描述

我正在尝试编译一个 .cpp 文件,该文件使用该coroutine库的命令。

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'

这是一个错误吗?

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

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

#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";
    }
}

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

解决方案


对于 clang,它应该是 -fcoroutines-ts。除非您使用与 libstdc++ 混合的 clang 构建协程。


推荐阅读