ubuntu18.04下通过vscode调试c++代码

本文参考了:https://www.cnblogs.com/lidabo/p/5888997.html
感谢原作者分享。

前提:
1、已经有一段能够正确运行的c++代码;
2、已装好g++、cmake、make、gdb;

调试方法:
1、打开vscode、打开代码所在目录
2、在代码目录下创建build目录
3、按F5,此时会弹出对话框,上面有“修改Launch.json"、“修改Tasks.json"按钮,点击“修改Launch.json"按钮,此时会打开
Launch.json文件,将文件中的 “program”: 后面的内容改为实际程序路径,比如我的程序编译好后会在build目录下,名字叫做myapp,我这里就改成: “program”: “${fileDirname}/build/myapp”,
然后把 “preLaunchTask”:开头的行改成: “preLaunchTask”:“makeFile”,

如下所示:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
“version”: “0.2.0”,
“configurations”: [
{
“name”: “g++ build and debug active file”,
“type”: “cppdbg”,
“request”: “launch”,
“program”: “ f i l e D i r n a m e / b u i l d / m y a p p " , " a r g s " : [ ] , " s t o p A t E n t r y " : f a l s e , " c w d " : " {fileDirname}/build/myapp", "args": [], "stopAtEntry": false, "cwd": " fileDirname/build/myapp","args":[],"stopAtEntry":false,"cwd":"{workspaceFolder}”,
“environment”: [],
“externalConsole”: false,
“MIMode”: “gdb”,
“setupCommands”: [
{
“description”: “Enable pretty-printing for gdb”,
“text”: “-enable-pretty-printing”,
“ignoreFailures”: true
}
],
“preLaunchTask”: “makeFile”,
“miDebuggerPath”: “/usr/bin/gdb”
}
]
}

然后按Ctrl+s,保存文件;

再按F5,执行程序,发现仍然不能启动,会弹出之前的对话框,此时点击“修改Tasks.json"按钮,修改task.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”: “makeFile”,
“command”: “make -j12”,
“args”: [
],
“options”: {
“cwd”: “KaTeX parse error: Expected 'EOF', got '}' at position 34: …d" }̲, "…gcc”
]
}
]
}

上面文件改动部分有四处:
1) “label”: “makeFile”,
2) “command”: “make -j12”,
3) “args”: [
],
4) “options”: {
“cwd”: “${fileDirname}/build”
},

改完后保存文件,然后回到vscode主界面,此时再按F5按钮,程序即可执行。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值