当前位置:   article > 正文

vscode搭建C/C++环境

vscode搭建C/C++环境

文章目录

一、安装vscode

二、下载安装g++

三、安装VSCode插件

四、配置运行环境

一、安装vscode

直接官网免费下载:下载完成后进行安装,记得更换安装路径Visual Studio Code - Code Editing. RedefinedVisual Studio Code is a code editor redefined and optimized for building and debugging modern web and cloud applications.  Visual Studio Code is free and available on your favorite platform - Linux, macOS, and Windows.icon-default.png?t=N7T8https://code.visualstudio.com/

二、下载安装g++

1. 点击连接下载64位,下载载完成后进行安装,要记住安装路径(不要出现中文),一会还要用

MinGW Distro - nuwen.net

2. 找到复制刚才安装的路径

3. 接着打开编辑环境变量

4. 接着单击Path,再点击编辑

5. 点击右侧新建,然后将刚刚复制的路径添加上去。接着一直点击确定,直到退出。

6. 打开cmd验证是否安装成功,输入:g++ --version

7. 出现这个就代表安装成功了。

三、安装VSCode插件

1. 打开vscode,点击左侧的扩展按钮

2. 搜索C,安装第一个插件

3. 接着搜索安装Code Runner

4. 如果想更换中文,可以再搜索chinese

四、配置运行环境

1. 首先建一个文件夹,文件名不可以用中文,这个文件夹就是以后存放并运行调试C/C++代码的地方,只要放在这个文件夹里就可以正常调试。

2. 打开vscode,点击文件(file),再点击打开文件夹(Open Folder),然后打开刚刚创建的文件夹。

3. 接着点击文件名旁边的新建文件夹,文件夹命名为.vscode(不能修改)

4. 接着在这个文件夹中创建四个文件

  1. //c_cpp_properties.json
  2. //launch.json
  3. //settings.json
  4. //tasks.json

5. 然后在每个文件夹中输入以下内容。

5.1 c_cpp_properties.json

注意compilerPath这一项要把路径改成安装g++.exe的安装路径,和刚刚配置path时的地址类似,找到安装路径中的bin文件,然后打开选择g++.exe。

C:/Program Files/mingw64/bin/g++.exe

  1. {
  2. "configurations": [
  3. {
  4. "name": "Win64",
  5. "includePath": ["${workspaceFolder}/**"],
  6. "defines": ["_DEBUG", "UNICODE", "_UNICODE"],
  7. "windowsSdkVersion": "10.0.18362.0",
  8. "compilerPath": "C:/Program Files/mingw64/bin/g++.exe",
  9. "cStandard": "c17",
  10. "cppStandard": "c++17",
  11. "intelliSenseMode": "gcc-x64"
  12. }
  13. ],
  14. "version": 4
  15. }

5.2 launch.json

注意miDebuggerPath这一项也要把路径改成安装gdb.exe的安装路径,和刚刚地址一样,只是后面选择的文件不一样。

C:\\Program Files\\mingw64\\bin\\gdb.exe

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "(gdb) Launch",
  6. "type": "cppdbg",
  7. "request": "launch",
  8. "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
  9. "args": [],
  10. "stopAtEntry": false,
  11. "cwd": "${workspaceRoot}",
  12. "environment": [],
  13. "externalConsole": true,
  14. "MIMode": "gdb",
  15. "miDebuggerPath": "C:\\Program Files\\mingw64\\bin\\gdb.exe",
  16. "preLaunchTask": "g++",
  17. "setupCommands": [
  18. {
  19. "description": "Enable pretty-printing for gdb",
  20. "text": "-enable-pretty-printing",
  21. "ignoreFailures": true
  22. }
  23. ]
  24. }
  25. ]
  26. }

5.3 settings.json

这里没有要修改的

  1. {
  2. "files.associations": {
  3. "*.py": "python",
  4. "iostream": "cpp",
  5. "*.tcc": "cpp",
  6. "string": "cpp",
  7. "unordered_map": "cpp",
  8. "vector": "cpp",
  9. "ostream": "cpp",
  10. "new": "cpp",
  11. "typeinfo": "cpp",
  12. "deque": "cpp",
  13. "initializer_list": "cpp",
  14. "iosfwd": "cpp",
  15. "fstream": "cpp",
  16. "sstream": "cpp",
  17. "map": "c",
  18. "stdio.h": "c",
  19. "algorithm": "cpp",
  20. "atomic": "cpp",
  21. "bit": "cpp",
  22. "cctype": "cpp",
  23. "clocale": "cpp",
  24. "cmath": "cpp",
  25. "compare": "cpp",
  26. "concepts": "cpp",
  27. "cstddef": "cpp",
  28. "cstdint": "cpp",
  29. "cstdio": "cpp",
  30. "cstdlib": "cpp",
  31. "cstring": "cpp",
  32. "ctime": "cpp",
  33. "cwchar": "cpp",
  34. "exception": "cpp",
  35. "ios": "cpp",
  36. "istream": "cpp",
  37. "iterator": "cpp",
  38. "limits": "cpp",
  39. "memory": "cpp",
  40. "random": "cpp",
  41. "set": "cpp",
  42. "stack": "cpp",
  43. "stdexcept": "cpp",
  44. "streambuf": "cpp",
  45. "system_error": "cpp",
  46. "tuple": "cpp",
  47. "type_traits": "cpp",
  48. "utility": "cpp",
  49. "xfacet": "cpp",
  50. "xiosbase": "cpp",
  51. "xlocale": "cpp",
  52. "xlocinfo": "cpp",
  53. "xlocnum": "cpp",
  54. "xmemory": "cpp",
  55. "xstddef": "cpp",
  56. "xstring": "cpp",
  57. "xtr1common": "cpp",
  58. "xtree": "cpp",
  59. "xutility": "cpp",
  60. "stdlib.h": "c",
  61. "string.h": "c"
  62. },
  63. "editor.suggest.snippetsPreventQuickSuggestions": false,
  64. "aiXcoder.showTrayIcon": true
  65. }

5.4 tasks.json

这里也没有要修改的。

  1. {
  2. "version": "2.0.0",
  3. "tasks": [
  4. {
  5. "label": "g++",
  6. "command": "g++",
  7. "args": [
  8. "-g",
  9. "${file}",
  10. "-o",
  11. "${fileDirname}/${fileBasenameNoExtension}.exe"
  12. ],
  13. "problemMatcher": {
  14. "owner": "cpp",
  15. "fileLocation": [
  16. "relative",
  17. "${workspaceRoot}"
  18. ],
  19. "pattern": {
  20. "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
  21. "file": 1,
  22. "line": 2,
  23. "column": 3,
  24. "severity": 4,
  25. "message": 5
  26. }
  27. },
  28. "group": "build"
  29. },
  30. {
  31. "type": "cppbuild",
  32. "label": "C/C++: gcc.exe 生成活动文件",
  33. "command": "D:\\Qt\\Tools\\mingw1310_64\\bin\\gcc.exe",
  34. "args": [
  35. "-fdiagnostics-color=always",
  36. "-g",
  37. "${file}",
  38. "-o",
  39. "${fileDirname}\\${fileBasenameNoExtension}.exe"
  40. ],
  41. "options": {
  42. "cwd": "${fileDirname}"
  43. },
  44. "problemMatcher": [
  45. "$gcc"
  46. ],
  47. "group": {
  48. "kind": "build",
  49. "isDefault": true
  50. },
  51. "detail": "调试器生成的任务。"
  52. }
  53. ]
  54. }

6. 保存以后就完成了所有配置,以后所有的C/C++文件都要放到这个文件夹中运行调试。

7. 想要运行调试代码点击右上角的三角就可以了。

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

闽ICP备14008679号