1.检查环境
1.1检查gcc,g++
根据自己所需gcc或者g++环境,进行检查:
如果没有gcc或者g++环境,需要下载
which gcc
1.2检查gdb
如果没有该环境,则需要下载
which gdb
2.vsCode创建简单的程序
test.cpp
3.生成文件
3.1生成c_cpp_properties.json
ctrl+ shift + p调出弹窗: 输入c/c++ 选择选择Edit Configurations(UI)进入配置。
step1:改成对应的系统环境 linux系统改为linux环境
step2: 将编译器路径改为 which gcc返回的 /usr/bin/gcc
则会在会在.vscode目录下生成c_cpp_properties.json文件
3.2生成tasks.json文件
1.点击代码页,test.cpp
2.在代码页当前位置按ctrl+ shift + p调出弹窗: 选择Tasks:configure Default Build Task进入配置
再选择 c/c++: gcc.exe build active file 会在.vscode目录下生成tasks.json
3.3生成launch.json文件
点击菜单栏运行(RUN)→添加配置,则会在.vscode目录下生成launch.json
launch.json
{ // 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [] }
此时launch.json文件是不完整的需要下载模板
将此文件内容更改为一下数据
{ // 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(gdb) 启动", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/dayo1.exe", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "/usr/bin/gdb", "setupCommands": [ { "description": "为 gdb 启用整齐打印", "text": "-enable-pretty-printing", "ignoreFailures": true }, { "description": "将反汇编风格设置为 Intel", "text": "-gdb-set disassembly-flavor intel", "ignoreFailures": true } ], "preLaunchTask": "Compile" } ] }
miDebuggerPath字段修改为which gdb返回的/usr/bin/gdb路径
注意:
launch.json文件需要到tasks.json查找数据,需要将launch.json文件中preLaunchTask字段和tasks.json文件中label字段值一致,建议都设置C为ompile
4.调试
打断点
点击运行->启动调试
执行下一步