What is the status of C++20 coroutines support in Clang?
Asked Answered
N

1

9

According to cppreference.com ( https://en.cppreference.com/w/cpp/compiler_support#C.2B.2B20_features ) Clang has partial support of C++20 coroutines since version 8:

Clang coroutine support

But if in Clang trunk (which is upcoming version 13) I write

#include <coroutine>

it results in the error( 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"

And if I add -fcoroutines flag in the command line, then Clang complains( https://gcc.godbolt.org/z/qMrv6nMzE ):

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

Is there any way to start using C++20 coroutines in Clang?

Nolan answered 27/7, 2021 at 7:43 Comment(0)
W
14

Note that the first error is in the GCC standard library, and from that it can be deduced that the -fcoroutines option is for GCC not Clang.

To build with the Clang libc++ you need to add the option -stdlib=libc++ instead. But that will lead to the <coroutine> header file not being found instead.

Since Clang coroutines is still in the "experimental" stage you have to include <experimental/coroutine>.

So there are two things you need to change:

  • Use the Clang libc++ (-stdlib=libc++)
  • Include the experimental header file (#include <experimental/coroutine>)

Also note that since coroutines are experimental, the symbols defined in the header file will be in the std::experimental namespace.

Westsouthwest answered 27/7, 2021 at 7:51 Comment(3)
Thanks. The correct include is #include <experimental/coroutine> (without 's' at the end)Nolan
Do you know if clang will support libstdc++'s coroutines eventually? Or does using coroutines mean you always have to use libc++ if you're compiling with clang?Amorous
I don't think that Apple will ever support libstdc++ again, they said that this is legacy. They go with libc++ because this is not poisoned by the GPL license.Mcneal

© 2022 - 2024 — McMap. All rights reserved.