本文参考了: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按钮,程序即可执行。