手把手教你VSCODE如何配置C/C++环境

1.首先去官网下载VSCODE

https://code.visualstudio.com/Download

2.下载并安装G++

https://nuwen.net/mingw.html

下载完成后进行安装,自己选择安装路径,安装的路径需要记住,马上就要用到

不能有中文路径

安装完成之后我们打开刚刚的安装路径,找到并打开MinGW -> bin,进入bin文件夹之后点一下这里,右键复制路径

然后修改电脑的环境变量

双击path

 然后我们再来看一看刚刚的操作有没有成功,按Win+R,输入cmd,在控制台中输入g++ --version

这样我们的g++就算安装好啦!

3.打开安装的VSCODE,开始配置软件

搜索C/C++ 安装第一个插件,再搜索安装Code Runner

4.配置调试功能,也是最复杂的部分

在桌面上新建一个code的文件夹,然后用VSCODE打开

然后点击这里新建四个文件为

//c_cpp_properties.json
//launch.json
//settings.json
//tasks.json

接下来复制粘贴这四个文件的内容 

首先是c_cpp_properties.json

{
  "configurations": [
    {
      "name": "Win64",
      "includePath": ["${workspaceFolder}/**"],
      "defines": ["_DEBUG", "UNICODE", "_UNICODE"],
      "windowsSdkVersion": "10.0.18362.0",
      "compilerPath": "C:/MinGW/bin/g++.exe",
      "cStandard": "c17",
      "cppStandard": "c++17",
      "intelliSenseMode": "gcc-x64"
    }
  ],
  "version": 4
}

注意compilerPath这一项要把路径改成刚才g++的安装路径:找到刚刚的安装文件夹->MinGW->bin->g++,exe ,然后复制或者手动把g++.exe的路径敲上去,格式要跟上面代码段一样

然后是launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(gdb) Launch", 
      "type": "cppdbg", 
      "request": "launch", 
      "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", 
      "args": [], 
      "stopAtEntry": false,
      "cwd": "${workspaceRoot}",
      "environment": [],
      "externalConsole": true, 
      "MIMode": "gdb",
      "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
      "preLaunchTask": "g++",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    }
  ]
}

注意miDebuggerPath这一项也要把路径改成刚才g++的安装路径:找到刚刚的安装文件夹->MinGW->bin->gdb,exe ,然后复制或者手动把gdb.exe的路径敲上去,格式要跟上面代码段一样

 接下来是settings.json

{
  "files.associations": {
    "*.py": "python",
    "iostream": "cpp",
    "*.tcc": "cpp",
    "string": "cpp",
    "unordered_map": "cpp",
    "vector": "cpp",
    "ostream": "cpp",
    "new": "cpp",
    "typeinfo": "cpp",
    "deque": "cpp",
    "initializer_list": "cpp",
    "iosfwd": "cpp",
    "fstream": "cpp",
    "sstream": "cpp",
    "map": "c",
    "stdio.h": "c",
    "algorithm": "cpp",
    "atomic": "cpp",
    "bit": "cpp",
    "cctype": "cpp",
    "clocale": "cpp",
    "cmath": "cpp",
    "compare": "cpp",
    "concepts": "cpp",
    "cstddef": "cpp",
    "cstdint": "cpp",
    "cstdio": "cpp",
    "cstdlib": "cpp",
    "cstring": "cpp",
    "ctime": "cpp",
    "cwchar": "cpp",
    "exception": "cpp",
    "ios": "cpp",
    "istream": "cpp",
    "iterator": "cpp",
    "limits": "cpp",
    "memory": "cpp",
    "random": "cpp",
    "set": "cpp",
    "stack": "cpp",
    "stdexcept": "cpp",
    "streambuf": "cpp",
    "system_error": "cpp",
    "tuple": "cpp",
    "type_traits": "cpp",
    "utility": "cpp",
    "xfacet": "cpp",
    "xiosbase": "cpp",
    "xlocale": "cpp",
    "xlocinfo": "cpp",
    "xlocnum": "cpp",
    "xmemory": "cpp",
    "xstddef": "cpp",
    "xstring": "cpp",
    "xtr1common": "cpp",
    "xtree": "cpp",
    "xutility": "cpp",
    "stdlib.h": "c",
    "string.h": "c"
  },
  "editor.suggest.snippetsPreventQuickSuggestions": false,
  "aiXcoder.showTrayIcon": true
}

 最后是tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "g++",
      "command": "g++",
      "args": [
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}.exe"
      ],
      "problemMatcher": {
        "owner": "cpp",
        "fileLocation": ["relative", "${workspaceRoot}"],
        "pattern": {
          "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
          "file": 1,
          "line": 2,
          "column": 3,
          "severity": 4,
          "message": 5
        }
      },
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

再次强调:以后的C/C++代码文件必须放在这个Code文件夹里,或者说有.vscode文件夹的文件夹里,如果调试放在其他位置的代码文件会报错!

可以像我这样在Code文件中建多个文件夹分类存放代码

如果上述流程你都完成了,那么现在你已经可以新建一个.c或者.cpp文件写代码测试一下你刚刚配置好的VSCode

5.然后你可以运行代码了

打开设置,搜索run in terminal 

当然也依旧可以选择按F5使用小黑框进行运行调试的

但是两个进程是冲突的,只是多了一种选择,是不可以同时用的!比如我正在终端运行代码的时候,再按F5调试代码就会出错

异常问题

1.调试时显示“找不到g++”
首先检查一下是不是g++的安装路径或者文件名里面存在中文,如果存在中文需要把中文名改掉或者更换其他路径安装,如果不存在中文的话,右键点击VSCode的图标,选择“属性”

然后选择“兼容性”,勾选“以管理员身份运行此程序” ,然后依次点击“应用”,‘确定’即可(部分电脑需要选择这个选项)

好了,然后你就可以使用VSCODE阅读代码和调试代码了

VSCODE配置C/C++环境还是相对比较麻烦的,而且有时候不能调试,建议大家使用Visual Studio 调试代码,虽然可能没VSCODE那么好的观感,VSOCODE适用于前端较多,Visual Studio 后端使用较多,其实都是微软一个妈生的,看大家喜好选择吧。

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 15:14:04       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 15:14:04       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 15:14:04       58 阅读
  4. Python语言-面向对象

    2024-07-18 15:14:04       69 阅读

热门阅读

  1. 【18】Android 线程间通信(三) - Handler

    2024-07-18 15:14:04       18 阅读
  2. SpinalHDL之Flow

    2024-07-18 15:14:04       23 阅读
  3. 精通JVM监控与调优:工具使用与命令指南

    2024-07-18 15:14:04       22 阅读
  4. C#配置文件中AppSettings的读写

    2024-07-18 15:14:04       18 阅读
  5. Flutter 开源库学习

    2024-07-18 15:14:04       21 阅读
  6. 白骑士的C++教学附加篇 5.2 代码规范与最佳实践

    2024-07-18 15:14:04       18 阅读
  7. 基于STM32设计的人体健康监测系统(华为云IOT)(189)

    2024-07-18 15:14:04       23 阅读
  8. x264 写入码流函数分析与介绍

    2024-07-18 15:14:04       23 阅读
  9. Spring Boot 动态多数据源配置

    2024-07-18 15:14:04       23 阅读
  10. react中状态管理useState

    2024-07-18 15:14:04       17 阅读
  11. 深度学习,人工智能

    2024-07-18 15:14:04       19 阅读