首页 > 解决方案 > c++中的协程调试

问题描述

我正在尝试测试已包含必要标志的coroutine功能c++20,但是当#include <coroutine>我收到以下错误时:

cannot open source file "coroutine". 我敢肯定这是一个愚蠢的错误!

代码:

// lazyGenerator.cpp

#include <iostream>
#include <vector>
#include <coroutine>  // <---- the immediate error, which causes generator and co-yield not to work

using namespace std; 

generator<int> generatorForNumbers(int begin, int inc = 1) {
  
  for (int i = begin;; i += inc) {
    co_yield i;
  }
  
}

int main() {

    std::cout << std::endl;

    const auto numbers= generatorForNumbers(-10);                   // (2)
  
    for (int i= 1; i <= 20; ++i) std::cout << numbers << " ";       // (4)
  
    std::cout << "\n\n";
                                                         
    for (auto n: generatorForNumbers(0, 5)) std::cout << n << " ";  // (3)

    std::cout << "\n\n";

}

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-std=c++2a", // <---- changed from -std==c++21 by suggestion
                "-fcoroutines",
                "-stdlib=libc++",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

运行结果clang++ -v

clang version 11.1.0
Target: x86_64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin

带有 c++2a 标志的协程错误

/Users/lukeanglin/Desktop/C++/debug/hello_world.cpp:5:10: fatal error: 'coroutine' file not found
#include <coroutine> 
         ^~~~~~~~~~~
1 error generated.
The terminal process "/bin/zsh '-c', '/usr/bin/clang++ -std=c++2a -stdlib=libc++ -g /Users/lukeanglin/Desktop/C++/debug/hello_world.cpp -o /Users/lukeanglin/Desktop/C++/debug/hello_world'" terminated with exit code: 1.

如果我运行 c++20 标志,则会出错:

error: invalid value 'c++20' in '-std=c++20'
note: use 'c++98' or 'c++03' for 'ISO C++ 1998 with amendments' standard
note: use 'gnu++98' or 'gnu++03' for 'ISO C++ 1998 with amendments and GNU extensions' standard
note: use 'c++11' for 'ISO C++ 2011 with amendments' standard
note: use 'gnu++11' for 'ISO C++ 2011 with amendments and GNU extensions' standard
note: use 'c++14' for 'ISO C++ 2014 with amendments' standard
note: use 'gnu++14' for 'ISO C++ 2014 with amendments and GNU extensions' standard
note: use 'c++17' for 'ISO C++ 2017 with amendments' standard
note: use 'gnu++17' for 'ISO C++ 2017 with amendments and GNU extensions' standard
note: use 'c++2a' for 'Working draft for ISO C++ 2020' standard
note: use 'gnu++2a' for 'Working draft for ISO C++ 2020 with GNU extensions' standard
The terminal process "/bin/zsh '-c', '/usr/bin/clang++ -std=c++20 -stdlib=libc++ -g /Users/lukeanglin/Desktop/C++/debug/hello_world.cpp -o /Users/lukeanglin/Desktop/C++/debug/hello_world'" terminated with exit code: 1.

标签: c++debugging

解决方案


在撰写本文时,Clang 的当前版本并未完全实现此处描述的协程。

但是,最新版本支持自 C++20 以来已被取代的旧协程 TS。如果您只想临时使​​用协程,可以使用编译标志-std=c++20 -stdlib=libc++并将包含替换为#include <experimental/coroutine>. 我你想要一个非实验版本,你需要等待。


推荐阅读