c++ - 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.
解决方案
在撰写本文时,Clang 的当前版本并未完全实现此处描述的协程。
但是,最新版本支持自 C++20 以来已被取代的旧协程 TS。如果您只想临时使用协程,可以使用编译标志-std=c++20 -stdlib=libc++
并将包含替换为#include <experimental/coroutine>
. 我你想要一个非实验版本,你需要等待。
推荐阅读
- android - 尝试录制音频时出现运行时错误
- javascript - 函数式编程传递依赖
- javascript - Firestore startAt 返回 null
- r - 闪亮的逐字文本输出中的分词/分词
- python-3.x - 将字符串转换为元组列表的最快方法
- perl - DateTime 已从 cpan 和 cpanm 安装,但仍无法在 @INC 中找到
- android - RecyclerView 项目离开屏幕
- centos7 - 尝试在运行 WHM 和 CloudLinux 的 CentOS 7 服务器上安装 OpenCPU
- azure - 如何使用 azure kusto for python 运行 .show 操作命令
- r - 如何在 lapply (R) 中显式定义附加参数