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. 想要运行调试代码点击右上角的三角就可以了。
相关推荐
林开落L12 分钟前
前缀和算法习题篇(上)
c++·算法·leetcode
Prejudices25 分钟前
C++如何调用Python脚本
开发语言·c++·python
单音GG28 分钟前
推荐一个基于协程的C++(lua)游戏服务器
服务器·c++·游戏·lua
wind瑞32 分钟前
IntelliJ IDEA插件开发-代码补全插件入门开发
java·ide·intellij-idea
qing_04060344 分钟前
C++——多态
开发语言·c++·多态
孙同学_44 分钟前
【C++】—掌握STL vector 类:“Vector简介:动态数组的高效应用”
开发语言·c++
charlie1145141911 小时前
Qt Event事件系统小探2
c++·qt·拖放·事件系统
iiiiiankor1 小时前
C/C++内存管理 | new的机制 | 重载自己的operator new
java·c语言·c++
小辛学西嘎嘎1 小时前
C/C++精品项目之图床共享云存储(3):网络缓冲区类和main
c语言·开发语言·c++
阿乾之铭1 小时前
IDEA中创建多模块项目步骤
java·ide·intellij-idea