为什么 gcc10 看不到 -fcoroutines 标志?

问题描述

我正在尝试编译一个包含 #include <coroutine>文件的程序。但是 gcc10 抛出错误

/usr/include/c++/10/coroutine:295:2: error: #error "the coroutine header requires -fcoroutines"
  295 | #error "the coroutine header requires -fcoroutines"

我在 cmake 中设置了所有必要的标志

cmake_minimum_required(VERSION 3.17)
project(cortest1)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "-fcoroutines")
set(CMAKE_CXX_FLAGS "-pthread")

add_executable(cortest1 main.cpp)

但是编译器没有看到 -fcoroutines 标志。 我正在使用 g ++ 10.2.0

解决方法

set(CMAKE_CXX_FLAGS "-pthread")

将标志设置为 -pthread 后,它不再具有先前具有协程标志的值。

附言使用特定于目标的配置,即不要设置 CMAKE_CXX_FLAGS。请改用 target_compile_options。并使用 target_compile_features 而不是设置 CMAKE_CXX_STANDARD。