当前位置:   article > 正文

VScode 运行调试c++程序_vscode 调试c++

vscode 调试c++

一、环境配置

  • 安装了vscode
  • 安装了 C/C++ 
  • 安装好了Mingw-w64工具,并设置好了环境变量
  1. Mingw-w64是Windows下的GCC工具,检查是否安装好可以用以下命令:
  2. g++ -v
  3. gdb -v

二、创建工程

  1. mkdir projects
  2. cd projects
  3. mkdir helloworld
  4. cd helloworld
  5. code

code . 命令在当前目录下的打开vscode,创建helloworld.cpp文件

  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5. int main()
  6. {
  7. vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
  8. for (const string& word : msg)
  9. {
  10. cout << word << " ";
  11. }
  12. cout << endl;
  13. }

三、编译 

1、tasks.json 文件

  • 主菜单栏选择 Terminal -> Configure Default Build Task.
  • 从下拉栏中选择 g++.exe build active file,来编译在编辑栏活跃的源文件。

  tasks.json 文件告诉vscode如何编译这个程序,调用 g++ 编译器将源文件编译成可执行文件。

  1. {
  2. "version": "2.0.0",
  3. "tasks": [
  4. {
  5. "type": "cppbuild",
  6. "label": "task g++",
  7. "command": "D:\\path\\c++\\bin\\g++.exe",
  8. "args": [
  9. "-fdiagnostics-color=always",
  10. "-g",
  11. "${file}",
  12. "-o",
  13. "${fileDirname}\\${fileBasenameNoExtension}.exe"
  14. ],
  15. "options": {
  16. "cwd": "${fileDirname}"
  17. },
  18. "problemMatcher": [
  19. "$gcc"
  20. ],
  21. "group": "build",
  22. "detail": "D:\\path\\c++\\bin\\g++.exe"
  23. },
  24. ]
  25. }

2、launch.json 文件 

  • 从主菜单,选择 Run -> Add Configuration ... 之后选择 C++(GDB/LLDB)
  • 从下拉栏中选择g++.exe build and debug active file.
  • program:调试入口文件的地址
  • cwd:程序启动调试的目录
  • miDebuggerPath:调试器的路径
  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "(gdb) Windows Bash ",
  6. "type": "cppdbg",
  7. "request": "launch",
  8. "program": "${workspaceFolder}",
  9. "args": [],
  10. "stopAtEntry": false,
  11. "cwd": "${fileDirname}",
  12. "environment": [],
  13. "externalConsole": true,
  14. "pipeTransport": {
  15. "debuggerPath": "/usr/bin/gdb",
  16. "pipeProgram": "${env:windir}\\system32\\bash.exe",
  17. "pipeArgs": ["-c"],
  18. "pipeCwd": ""
  19. },
  20. "setupCommands": [
  21. {
  22. "description": "gdb description",
  23. "text": "-enable-pretty-printing",
  24. "ignoreFailures": true
  25. }
  26. ],
  27. "preLaunchTask": "task g++"
  28. },
  29. {
  30. "name": "C/C++ Runner: Debug Session",
  31. "type": "cppdbg",
  32. "request": "launch",
  33. "args": [
  34. ""
  35. ],
  36. "stopAtEntry": true,
  37. "cwd": "d:/code/projects/helloworld",
  38. "environment": [],
  39. "program": "${fileDirname}\\${fileBasenameNoExtension}",
  40. "internalConsoleOptions": "openOnSessionStart",
  41. "MIMode": "gdb",
  42. "miDebuggerPath": "gdb",
  43. "externalConsole": true,
  44. "setupCommands": [
  45. {
  46. "description": "Enable pretty-printing for gdb",
  47. "text": "-enable-pretty-printing",
  48. "ignoreFailures": true
  49. }
  50. ],
  51. "preLaunchTask": "task g++"
  52. }
  53. ]
  54. }

注意:preLaunchTask 指定了在启动调试前应该执行的任务,因此应该和tasks.json文件中的label保持一致。 

四、开始调试

按 F5 ,或者Run > Start Debugging 。

五、参考

  1. VSCODE 运行调试c++程序 - 知乎 (zhihu.com)
  2. Get Started with C++ and Mingw-w64 in Visual Studio Code
  3. VsCode + gdb + gdbserver远程调试C++程序

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

闽ICP备14008679号