当前位置:   article > 正文

vscode配置C/C++调试环境_vscode配置c++debug环境

vscode配置c++debug环境

1. Ctrl+Shift+P,输入 tasks,选择"Tasks:Configure Default Build Task",选择C/C++,这会生成tasks.json,如图:

 如下是生成的task.json:

  1. {
  2. "version": "2.0.0",
  3. "tasks": [
  4. {
  5. "type": "cppbuild",
  6. "label": "C/C++: gcc 生成活动文件",
  7. "command": "/usr/bin/gcc",
  8. "args": [
  9. "-fdiagnostics-color=always",
  10. "-g",
  11. "${file}",
  12. "-o",
  13. "${fileDirname}/${fileBasenameNoExtension}"
  14. ],
  15. "options": {
  16. "cwd": "${fileDirname}"
  17. },
  18. "problemMatcher": [
  19. "$gcc"
  20. ],
  21. "group": {
  22. "kind": "build",
  23. "isDefault": true
  24. },
  25. "detail": "编译器: /usr/bin/gcc"
  26. }
  27. ]
  28. }

修改其中参数"args"的值:删掉原来的"${file}",并将工程下的c文件添加进去,即"${fileDirname}/*.c"(或单个文件添加也行,笔者因为工程的所有的c文件都在一个路径下,所以用的*.c),如下:

  1. {
  2. "version": "2.0.0",
  3. "tasks": [
  4. {
  5. "type": "cppbuild",
  6. "label": "C/C++: gcc 生成活动文件",
  7. "command": "/usr/bin/gcc",
  8. "args": [
  9. "-fdiagnostics-color=always",
  10. "-g",
  11. "${fileDirname}/*.c",
  12. "-o",
  13. "${fileDirname}/${fileBasenameNoExtension}"
  14. ],
  15. "options": {
  16. "cwd": "${fileDirname}"
  17. },
  18. "problemMatcher": [
  19. "$gcc"
  20. ],
  21. "group": {
  22. "kind": "build",
  23. "isDefault": true
  24. },
  25. "detail": "编译器: /usr/bin/gcc"
  26. }
  27. ]
  28. }

2. 在debug按钮(即左侧的那个虫子按钮)下,点击创建launch.json,选择配置中的C/C++: gdb,如下图:

 

这会自动创建出一个launch.json文件,如下:

  1. {
  2. // 使用 IntelliSense 了解相关属性。
  3. // 悬停以查看现有属性的描述。
  4. // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
  5. "version": "0.2.0",
  6. "configurations": []
  7. }

然后,做如下图的操作:

 

这时,launch.json如下:

  1. {
  2. // 使用 IntelliSense 了解相关属性。
  3. // 悬停以查看现有属性的描述。
  4. // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
  5. "version": "0.2.0",
  6. "configurations": [
  7. {
  8. "name": "(gdb) 启动",
  9. "type": "cppdbg",
  10. "request": "launch",
  11. "program": "输入程序名称,例如 ${workspaceFolder}/a.exe",
  12. "args": [],
  13. "stopAtEntry": false,
  14. "cwd": "${fileDirname}",
  15. "environment": [],
  16. "externalConsole": false,
  17. "MIMode": "gdb",
  18. "miDebuggerPath": "/path/to/gdb",
  19. "setupCommands": [
  20. {
  21. "description": "为 gdb 启用整齐打印",
  22. "text": "-enable-pretty-printing",
  23. "ignoreFailures": true
  24. },
  25. {
  26. "description": "将反汇编风格设置为 Intel",
  27. "text": "-gdb-set disassembly-flavor intel",
  28. "ignoreFailures": true
  29. }
  30. ]
  31. }
  32. ]
  33. }

然后,修改参数"program"的值为task.json中参数"argus"值里的输出,即-o后的部分,修改后如下:

  1. {
  2. // 使用 IntelliSense 了解相关属性。
  3. // 悬停以查看现有属性的描述。
  4. // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
  5. "version": "0.2.0",
  6. "configurations": [
  7. {
  8. "name": "(gdb) 启动",
  9. "type": "cppdbg",
  10. "request": "launch",
  11. "program": "${workspaceFolder}/${fileBasenameNoExtension}",
  12. "args": [],
  13. "stopAtEntry": false,
  14. "cwd": "${fileDirname}",
  15. "environment": [],
  16. "externalConsole": false,
  17. "MIMode": "gdb",
  18. "miDebuggerPath": "/path/to/gdb",
  19. "setupCommands": [
  20. {
  21. "description": "为 gdb 启用整齐打印",
  22. "text": "-enable-pretty-printing",
  23. "ignoreFailures": true
  24. },
  25. {
  26. "description": "将反汇编风格设置为 Intel",
  27. "text": "-gdb-set disassembly-flavor intel",
  28. "ignoreFailures": true
  29. }
  30. ]
  31. }
  32. ]
  33. }

然后,可以自己手工添加配置参数"preLaunchTask",该参数的值与task.json中的"label参数"的值保持一致,以便使用OS自带的终端(需要把参数"externalConsole"的值设置为true),这时,launch.json如下:

  1. {
  2. // 使用 IntelliSense 了解相关属性。
  3. // 悬停以查看现有属性的描述。
  4. // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
  5. "version": "0.2.0",
  6. "configurations": [
  7. {
  8. "preLaunchTask": "C/C++: gcc 生成活动文件",
  9. "name": "(gdb) 启动",
  10. "type": "cppdbg",
  11. "request": "launch",
  12. "program": "${workspaceFolder}/${fileBasenameNoExtension}",
  13. "args": [],
  14. "stopAtEntry": false,
  15. "cwd": "${fileDirname}",
  16. "environment": [],
  17. "externalConsole": true,
  18. "MIMode": "gdb",
  19. "miDebuggerPath": "/path/to/gdb",
  20. "setupCommands": [
  21. {
  22. "description": "为 gdb 启用整齐打印",
  23. "text": "-enable-pretty-printing",
  24. "ignoreFailures": true
  25. },
  26. {
  27. "description": "将反汇编风格设置为 Intel",
  28. "text": "-gdb-set disassembly-flavor intel",
  29. "ignoreFailures": true
  30. }
  31. ]
  32. }
  33. ]
  34. }

3. Ctrl+Shift+P, 输入C/C++, 选择"C/C++: Edit Configurations(JSON)", 生成一个c_cpp_properties.json, 如下:

  1. {
  2. "configurations": [
  3. {
  4. "name": "Linux",
  5. "includePath": [
  6. "${workspaceFolder}/**"
  7. ],
  8. "defines": [],
  9. "compilerPath": "/usr/bin/gcc",
  10. "cStandard": "c17",
  11. "cppStandard": "gnu++14",
  12. "intelliSenseMode": "linux-gcc-x64"
  13. }
  14. ],
  15. "version": 4
  16. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/924228
推荐阅读
相关标签
  

闽ICP备14008679号