我是踩完坑后,按着之前的配置重做了一次,成功了。
有复现的时候出错了,可以在下面评论说一下
欢迎访问我的博客sunspotsinys.top
有时候代码太复杂,就需要调试看一下中间过程了,我感觉VSCode的调试功能很好用,踩了几天的坑终于解决了。
首先,吐槽一下VSCode官网的教程Using C++ and WSL in VS Code,按着上面的做,完全行不通,坑人。不过还是可以看一点的。
准备
首先你要有VSCode,WSL,C/C++扩展。
我用的WSL是Ubuntu-18.04。
安装环境
首先保证你的WSL已经安装好了环境
whereis g++
whereis gdb


这是我执行后的部分输出,如果是这样就是可以的。不是的话,参考一下上面链接的官方教程。
创建工作目录
mkdir Cpp
cd Cpp
code .
就是创建个目录然后在那个目录下执行code .
(注意之一有个点.
)。这会直接打开VSCode,并帮你远程连接WSL。

编辑配置
键入Ctrl + Shift + P
打开命令面板。输入C/C++ edit configurations
,选择UI
的直观简单些。

照着图上的搞就行了

创建Tasks
打开命令面板。输入task
,选择任务:配置默认生成任务
,使用模板创建tasks.json文件
、Others
。

将里面的内容替换成
{
"version": "2.0.0",
"windows": {
"options": {
"shell": {
"executable": "bash.exe",
"args": [
"-c"
]
}
}
},
"tasks": [
{
"label": "g++ build active file",
"type": "shell",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
开始调试
写一个C++文件,设置好断点,按F5
就可以开始调试了。
按下F5
会有个提示,选择C++(GDB/LLDB)

选择
g++ build and debug active file

接下来就可以调试了。

网友评论