经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C 语言 » 查看文章
使用Vscode 开发调试 C/C++ 项目
来源:cnblogs  作者:microestc  时间:2021/4/12 9:56:31  对本文有异议
  1. 需要安装的扩展 C/C++

    如果是远程 Linux上开发还需要安装 Remote Development

  1. 创建工作目录后,代码远程克隆... 省略..

  2. 创建项目配置文件,主要的作用是代码智能提示,错误分析等等...
    按F1,输入 C/C++ 选择 编辑配置UI或者json 这个操作会生成 .vscode/c_cpp_properties.json 配置文件

修改相关的参数,如头文件路径,预定义参数,编译器等

  1. {
  2. "configurations": [
  3. {
  4. "name": "Linux",
  5. "includePath": [
  6. "${workspaceFolder}/**" // 包含了当前工作下所有头文件,如果没有在该目录下需要引入
  7. ],
  8. "defines": [ // 这里是项目中需要的预定义 LINUX环境通常需要加LINUX
  9. "LINUX",
  10. "ACI_TEST"
  11. ],
  12. "compilerPath": "/usr/bin/g++", // 编译器程序
  13. "cStandard": "gnu99",
  14. "cppStandard": "gnu++14",
  15. "intelliSenseMode": "linux-gcc-x64"
  16. }
  17. ],
  18. "version": 4
  19. }

(注意) 这个配置文件与编译和执行没有任何关系,它仅仅用于代码提示和错误提示

  1. 代码编译和调试配置
    选中打开一个.c 或者 .cpp 文件,按F5 选择 环境 和 编译器

生成了2个文件

.vscode/launch.json

  1. {
  2. // Use IntelliSense to learn about possible attributes.
  3. // Hover to view descriptions of existing attributes.
  4. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  5. "version": "0.2.0",
  6. "configurations": [
  7. {
  8. "name": "g++ - Build and debug active file",
  9. "type": "cppdbg",
  10. "request": "launch",
  11. "program": "${fileDirname}/${fileBasenameNoExtension}", // 需要调试的程序路径,如 ${workspaceFolder}/test
  12. "args": [], // 调试程序需要接收的参数,如 test -c => "args": ["-c"]
  13. "stopAtEntry": false,
  14. "cwd": "${workspaceFolder}",
  15. "environment": [],
  16. "externalConsole": false,
  17. "MIMode": "gdb",
  18. "setupCommands": [
  19. {
  20. "description": "Enable pretty-printing for gdb",
  21. "text": "-enable-pretty-printing",
  22. "ignoreFailures": true
  23. }
  24. ],
  25. "preLaunchTask": "C/C++: g++ build active file", // 在调试之前需要做什么, 就是当你按下f5, 首先会去不如编译程序... make 或者 gcc ,g++ 编译上面路径的程序,
  26. // "C/C++: g++ build active file" 只是一个名称,它会去 tasks.json 中找这个名称, 执行 这个任务之后开始调试
  27. "miDebuggerPath": "/usr/bin/gdb", // 调试的gdb ,要确保环境已经安装了gdb, sudo apt install gdb
  28. "envFile": "${workspaceFolder}/.env" // 配置环境变量的文件路径,下面会继续说明用处.
  29. }
  30. ]
  31. }

.vscode/tasks.json

  1. {
  2. "tasks": [
  3. {
  4. "type": "cppbuild", // shell 类型...
  5. "label": "C/C++: g++ build active file", // preLaunchTask 的名称
  6. "command": "/usr/bin/g++", // 编译器
  7. "args": [
  8. "-g",
  9. "${file}",
  10. "-o",
  11. "${fileDirname}/${fileBasenameNoExtension}"
  12. ],
  13. "options": {
  14. "cwd": "${workspaceFolder}"
  15. },
  16. "problemMatcher": [
  17. "$gcc"
  18. ],
  19. "group": {
  20. "kind": "build",
  21. "isDefault": true
  22. },
  23. "detail": "Task generated by Debugger."
  24. }
  25. ],
  26. "version": "2.0.0"
  27. }

这个文件就是怎么去编译你的程序,没什么可说 , make 或者 gcc g++ 编译
贴一个我的项目 tasks 用make 编译

  1. {
  2. "tasks": [
  3. {
  4. "type": "shell",
  5. "label": "make_1",
  6. "command":"make -j6 -f makefile_local.mk 2>build_error.log",
  7. "options": {
  8. "cwd": "${workspaceFolder}/UnitTesting/"
  9. },
  10. "problemMatcher": []
  11. },
  12. {
  13. "type": "shell",
  14. "label": "make_2",
  15. "command":"make -j6 -f makefile_local.mk 2>build_error.log",
  16. "options": {
  17. "cwd": "${workspaceFolder}/"
  18. },
  19. "problemMatcher": []
  20. },
  21. {
  22. "type": "shell",
  23. "label": "build",
  24. "dependsOrder": "sequence",
  25. "dependsOn": [
  26. "make_2",
  27. "make_1"
  28. ],
  29. "options": {
  30. "cwd": "${workspaceFolder}"
  31. },
  32. "problemMatcher": []
  33. }
  34. ],
  35. "version": "2.0.0"
  36. }
  1. 环境变量配置, 上面launch.json 中配置了.env 文件的路径

.env

  1. LD_LIBRARY_PATH=./lib:/lib64

用途是: 如果我的程序需要依赖某个动态库,比如,工作目录下lib和lib64里面是需要的动态库,可以在这里加入环境变量,可以是相对路径,或者绝对路径.
当前你也可以在系统环境变量里面设置

以上是 C/C++ 使用VScode的方法,如果对你有帮助,请点一个支持吧 _

原文链接:http://www.cnblogs.com/microestc/p/14633249.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号