Windows下vscode配置c++20教程

Windows下vscode配置c++20

1.配置电脑上的C++环境

打开MSYS2的官网下载MSYS2

建议将安装在C盘,安装完后的路径应该为C:\msys64
安装完成后进入 https://packages.msys2.org/base/mingw-w64-gcc 选择适合自己电脑版本的gcc包

一般来说都是第一个
单击进入后选择安装命令

最后将下载好的MSYS2打开,输入以下命令
分别是安装gcc,make,gdb的命令

pacman -S mingw-w64-x86_64-gcc  --disable-download-timeout
pacman -S mingw-w64-x86_64-make  --disable-download-timeout
pacman -S mingw-w64-x86_64-gdb  --disable-download-timeout

安装完以后应该可以在电脑路径C:\msys64\mingw64下找到gcc的配置文件。
最后就是配置环境,使用win + Q搜索高级系统设置,单击环境变量

找到系统变量中的Path双击进入

新建,将C:\msys64\mingw64\bin添加进去即可,注意最后一步步确定退出回来。

然后使用win + R并输入cmd,打开命令提示符,输入gcc -v检查是否配置成功,如果像我下面一样代表配置成功了

2.安装vscode

官网下载或微软商店下载都可以。随便安装到哪个盘,注意这一步的时候建议都选上。

然后打开vscode,Ctrl + Shift + X打开扩展页面

安装以下插件


安装完后就是配置.vscode文件文件夹中的四个文件了,如果没有的话就自己创建(c_cpp_properties.jsonlaunch.jsonsettings.jsontasks.json)
c_cpp_properties.json文件

{
  "configurations": [
    {
      "name": "windows-gcc-x64",
      "includePath": [
        "${workspaceFolder}/**"
      ],
      "defines": [
        "_DEBUG",
        "UNICODE",
        "_UNICODE"
      ],
      "compilerPath": "C:/msys64/mingw64/bin/gcc.exe", // 改为自己的路径
      "cStandard": "${default}",
      "cppStandard": "${default}",
      "intelliSenseMode": "windows-gcc-x64"
    }
  ],
  "version": 4
}

launch.json文件

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "g++.exe - 生成和调试活动文件",
      "type": "cppdbg",
      "request": "launch",
      "program": "C:\\Windows\\system32\\cmd.exe", // 改为自己的路径
      // "program": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
      "args": [
        "/C",
        "${fileDirname}\\${fileBasenameNoExtension}.exe",
        "&",
        "pause"
      ],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      // "cwd": "${workspaceRoot}",
      "environment": [],
      "externalConsole": true,
      "MIMode": "gdb",
      "miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe", // 改为自己的路径
      "preLaunchTask": "C/C++: g++.exe 生成活动文件",
      "setupCommands": [
        {
          "description": "为 gdb 启用整齐打印",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        },
        {
          "description": "将反汇编风格设置为 Intel",
          "text": "-gdb-set disassembly-flavor intel",
          "ignoreFailures": true
        }
      ]
    }
  ]
}

settings.json文件

{
  "C_Cpp_Runner.msvcBatchPath": "",
  "C_Cpp_Runner.cCompilerPath": "gcc",
  "C_Cpp_Runner.cppCompilerPath": "g++",
  "C_Cpp_Runner.debuggerPath": "gdb",
  "C_Cpp_Runner.cStandard": "c17",
  "C_Cpp_Runner.cppStandard": "gnu++20",
  "C_Cpp_Runner.useMsvc": false,
  "C_Cpp_Runner.warnings": [
    "-Wall",
    "-Wextra",
    "-Wpedantic",
    "-Wshadow",
    "-Wformat=2",
    "-Wcast-align",
    "-Wconversion",
    "-Wsign-conversion",
    "-Wnull-dereference"
  ],
  "C_Cpp_Runner.msvcWarnings": [
    "/W4",
    "/permissive-",
    "/w14242",
    "/w14287",
    "/w14296",
    "/w14311",
    "/w14826",
    "/w44062",
    "/w44242",
    "/w14905",
    "/w14906",
    "/w14263",
    "/w44265",
    "/w14928"
  ],
  "C_Cpp_Runner.enableWarnings": true,
  "C_Cpp_Runner.warningsAsError": false,
  "C_Cpp_Runner.compilerArgs": [],
  "C_Cpp_Runner.linkerArgs": [],
  "C_Cpp_Runner.includePaths": [],
  "C_Cpp_Runner.includeSearch": [
    "*",
    "**/*"
  ],
  "C_Cpp_Runner.excludeSearch": [
    "**/build",
    "**/build/**",
    "**/.*",
    "**/.*/**",
    "**/.vscode",
    "**/.vscode/**"
  ],
  "C_Cpp_Runner.useAddressSanitizer": false,
  "C_Cpp_Runner.useUndefinedSanitizer": false,
  "C_Cpp_Runner.useLeakSanitizer": false,
  "C_Cpp_Runner.showCompilationTime": false,
  "C_Cpp_Runner.useLinkTimeOptimization": false,
  "C_Cpp_Runner.msvcSecureNoWarnings": false,
  "files.associations": {
    "iosfwd": "cpp",
    "iostream": "cpp",
    "array": "cpp",
    "cmath": "cpp",
    "deque": "cpp",
    "string": "cpp",
    "vector": "cpp",
    "any": "cpp",
    "atomic": "cpp",
    "barrier": "cpp",
    "bit": "cpp",
    "*.tcc": "cpp",
    "bitset": "cpp",
    "cctype": "cpp",
    "cfenv": "cpp",
    "charconv": "cpp",
    "chrono": "cpp",
    "cinttypes": "cpp",
    "clocale": "cpp",
    "codecvt": "cpp",
    "compare": "cpp",
    "complex": "cpp",
    "concepts": "cpp",
    "condition_variable": "cpp",
    "coroutine": "cpp",
    "csetjmp": "cpp",
    "csignal": "cpp",
    "cstdarg": "cpp",
    "cstddef": "cpp",
    "cstdint": "cpp",
    "cstdio": "cpp",
    "cstdlib": "cpp",
    "cstring": "cpp",
    "ctime": "cpp",
    "cuchar": "cpp",
    "cwchar": "cpp",
    "cwctype": "cpp",
    "forward_list": "cpp",
    "list": "cpp",
    "map": "cpp",
    "set": "cpp",
    "unordered_map": "cpp",
    "unordered_set": "cpp",
    "exception": "cpp",
    "expected": "cpp",
    "algorithm": "cpp",
    "functional": "cpp",
    "iterator": "cpp",
    "memory": "cpp",
    "memory_resource": "cpp",
    "numeric": "cpp",
    "optional": "cpp",
    "random": "cpp",
    "ratio": "cpp",
    "regex": "cpp",
    "source_location": "cpp",
    "string_view": "cpp",
    "system_error": "cpp",
    "tuple": "cpp",
    "type_traits": "cpp",
    "utility": "cpp",
    "fstream": "cpp",
    "future": "cpp",
    "initializer_list": "cpp",
    "iomanip": "cpp",
    "istream": "cpp",
    "latch": "cpp",
    "limits": "cpp",
    "mutex": "cpp",
    "new": "cpp",
    "numbers": "cpp",
    "ostream": "cpp",
    "ranges": "cpp",
    "scoped_allocator": "cpp",
    "semaphore": "cpp",
    "shared_mutex": "cpp",
    "span": "cpp",
    "spanstream": "cpp",
    "sstream": "cpp",
    "stacktrace": "cpp",
    "stdexcept": "cpp",
    "stop_token": "cpp",
    "streambuf": "cpp",
    "syncstream": "cpp",
    "thread": "cpp",
    "typeindex": "cpp",
    "typeinfo": "cpp",
    "valarray": "cpp",
    "variant": "cpp",
    "hash_map": "cpp",
    "hash_set": "cpp",
    "strstream": "cpp",
    "decimal": "cpp",
    "buffer": "cpp",
    "executor": "cpp",
    "internet": "cpp",
    "io_context": "cpp",
    "netfwd": "cpp",
    "propagate_const": "cpp",
    "socket": "cpp",
    "timer": "cpp",
    "rope": "cpp",
    "slist": "cpp",
    "bool_set": "cpp",
    "dynamic_bitset": "cpp",
    "cassert": "c",
    "cerrno": "c",
    "cfloat": "c",
    "ciso646": "c",
    "climits": "c",
    "queue": "c",
    "stack": "c",
    "version": "c",
    "stdio.h": "c",
    "string.h": "c",
    "loginsuper.h": "c",
    "stuview.h": "c",
    "generaladmin.h": "c",
    "supercore.h": "c",
    "generalview.h": "c",
    "generalimpl.h": "c",
    "user.h": "c",
    "stdbool.h": "c",
    "unistd.h": "c",
    "clientcore.h": "c",
    "read.h": "c",
    "tools.h": "c",
    "clientview.h": "c",
    "types.h": "c",
    "clientimpl.h": "c",
    "assert.h": "c",
    "winsock2.h": "c",
    "mysql.h": "c"
  },
  "code-runner.executorMap": {
    "python": "set PYTHONIOENCODING=utf8 && python"
  },
  "C_Cpp.errorSquiggles": "disabled",
}

tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "C:\\msys64\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "-std=c++2a",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-O3",
                "-Wl,--stack=268435456", // 扩栈
                "-lws2_32"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "编译器: C:\\msys64\\mingw64\\bin\\g++.exe"  // 改为自己的路径
        }
    ],
    "version": "2.0.0"
}

注意如果你是跟我前面说的一样将配置安装在C盘那么直接复制即可,不然需要更改一下上面代码汇总标注位置的路径

到此就配置完成了,可以创建文件进行写代码了。
代码编写完成后按F5进行编译运行,如果失败则看一下左上角带红框位置是否为g++.exe。成功编译后如下图所示,左边会生成一个exe文件。

最后可以按照我的代码运行一下检查一下你的版本是否为20。

最后提醒一下:如果想要使用F5执行文件的则文件路径中不能带有中文,否则会失败。

安利几款好用的插件:

  1. Competitive Programming Helper (cph)

    在这里可以方便的输入测试样例进行测试,注意版本!!!可以在设置里搜索cph将其改为20版本

  2. background
    可以更换背景图片,将背景图片换成自己喜欢的,当然右下角可能会爆出警告(直接无视即可)。

posted @ 2024-09-25 17:30  sleeeeeping  阅读(321)  评论(0编辑  收藏  举报