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.https://code.visualstudio.com/

二、下载安装g++

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

MinGW Distro - nuwen.net

  1. 找到复制刚才安装的路径
  1. 接着打开编辑环境变量
  1. 接着单击Path,再点击编辑
  1. 点击右侧新建,然后将刚刚复制的路径添加上去。接着一直点击确定,直到退出。
  1. 打开cmd验证是否安装成功,输入:g++ --version
  1. 出现这个就代表安装成功了。

三、安装VSCode插件

  1. 打开vscode,点击左侧的扩展按钮
  1. 搜索C,安装第一个插件
  1. 接着搜索安装Code Runner
  1. 如果想更换中文,可以再搜索chinese

四、配置运行环境

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

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

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

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

cpp 复制代码
//c_cpp_properties.json
//launch.json
//settings.json
//tasks.json
  1. 然后在每个文件夹中输入以下内容。

5.1 c_cpp_properties.json

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

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

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

5.2 launch.json

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

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

cpp 复制代码
{
    "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:\\Program Files\\mingw64\\bin\\gdb.exe",
        "preLaunchTask": "g++",
        "setupCommands": [
          {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
          }
        ]
      }
    ]
  }

5.3 settings.json

这里没有要修改的

cpp 复制代码
{
    "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
  }

5.4 tasks.json

这里也没有要修改的。

cpp 复制代码
{
    "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": "build"
        },
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe 生成活动文件",
            "command": "D:\\Qt\\Tools\\mingw1310_64\\bin\\gcc.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ]
}
  1. 保存以后就完成了所有配置,以后所有的C/C++文件都要放到这个文件夹中运行调试。
  1. 想要运行调试代码点击右上角的三角就可以了。
相关推荐
爱吃西瓜的小菜鸡10 分钟前
【C语言】判断回文
c语言·学习·算法
别NULL12 分钟前
机试题——疯长的草
数据结构·c++·算法
CYBEREXP20081 小时前
MacOS M3源代码编译Qt6.8.1
c++·qt·macos
yuanbenshidiaos2 小时前
c++------------------函数
开发语言·c++
yuanbenshidiaos2 小时前
C++----------函数的调用机制
java·c++·算法
tianmu_sama2 小时前
[Effective C++]条款38-39 复合和private继承
开发语言·c++
羚羊角uou2 小时前
【C++】优先级队列以及仿函数
开发语言·c++
姚先生972 小时前
LeetCode 54. 螺旋矩阵 (C++实现)
c++·leetcode·矩阵
FeboReigns2 小时前
C++简明教程(文章要求学过一点C语言)(1)
c语言·开发语言·c++
FeboReigns2 小时前
C++简明教程(文章要求学过一点C语言)(2)
c语言·开发语言·c++