1. 安装gcc、gdb
打开终端依次输入以下指令
1 2 3 4 5 6 7 8
| $ sudo apt update $ sudo apt install build-essential $ sudo apt install gdb
$ gcc -v $ g++ -v $ gdb -v
|
2. 安装VSCode插件
只需要安装最基本的C/C++与Code Runner即可
3. 创建文件夹并编写Hello World
在用户主目录(~)创建一个VSCode专用的文件夹vscode,用vscode打开并在其中创建一个c++文件夹用来存放C/C++代码
创建hello.cpp,点击右上角Run Code运行,可以看到控制台输出Hello World
4. 调试程序
在vscode文件夹中的.vscode
文件夹中创建两个文件launch.json
,tasks.json
,并写入以下内容
launch.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| { "version": "0.2.0", "configurations": [ { "name": "C/C++", "type": "cppdbg", "request": "launch", "program": "${fileDirname}/${fileBasenameNoExtension}", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "preLaunchTask": "compile", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
|
tasks.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| { "version": "2.0.0", "tasks": [{ "label": "compile", "command": "g++", "args": [ "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "problemMatcher": { "owner": "cpp", "fileLocation": [ "relative", "${workspaceRoot}" ], "pattern": { "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$", "file": 1, "line": 2, "column": 3, "severity": 4, "message": 5 } }, "group": { "kind": "build", "isDefault": true } } ] }
|
进入VSCode左侧的调试,打好断点后,点击运行和调试即可正常调试了