VSCode 配置 C/C++ 环境

本文详细介绍了如何在VSCode上安装、配置C/C++编译环境,包括MingW安装、插件安装,以及关键配置文件如c_cpp_properties.json、launch.json和tasks.json的设置方法。
摘要由CSDN通过智能技术生成

1 安装 VSCode

直接去官网(https://code.visualstudio.com/)下载并安装即可。
在这里插入图片描述

2 配置C/C++编译环境

  • 方案一
    如果是在Windows,需要安装 MingW,可以去官网(https://sourceforge.net/projects/mingw-w64/)下载安装包。
    注意安装路径不要出现中文。
    在这里插入图片描述

    打开 windows shell 界面,输入mingw-get install gccmingw-get install g++
    如果安装的时候提示安装包被错误下载,可使用方案二安装。

  • 方案二
    进入网址https://sourceforge.net/projects/mingw-w64/files/,下滑,下载图中所示压缩包。
    在这里插入图片描述

    找一个无中文路径解压缩。
    添加环境变量。在Path中加入解压后的文件夹中的bin目录,如C:\ProgramFiles\MinGW\mingw64\bin。
    在这里插入图片描述

    在这里插入图片描述

  • 测试
    在 windows shell 界面输入gcc -vg++ -v,正确出现版本号则配置成功。

3 安装插件

搜索插件 C/C++ 和 Code Runner 并安装。
在这里插入图片描述

在这里插入图片描述

4 编辑配置文件

首先新建一个文件夹,并用 VSCode 打开。
在该文件夹中创建名为 .vscode 的文件夹。
在该文件夹中创建下面几个文件。以后的C/C++代码文件必须放在这个Code文件夹里,或者说有.vscode文件夹的文件夹里。

  1. c_cpp_properties.json
    将 compilerPath 换成你的g++路径。
    {
      "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
    }
    
  2. launch.json
    将 miDebuggerPath 换成你的gdb路径。
    {
      "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
            }
          ]
        }
      ]
    }
    
  3. settings.json
    {
      "json.schemaDownload.enable": false,
      "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
    }
    
  4. 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
          }
        }
      ]
    }
    
  • 19
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值