跳到正文

个人记录

配置 VS Code的 C++ 运行

Hu_Tianqi约 10 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月28日
篇幅
约 10 分钟

版本

  1. v4当前
  2. v3
  3. v2
  4. v1

配置 VS Code 的 C++ 运行

注意:程序目录中不可以含有中文或空格

下载小熊猫DEVC++

  1. 在 小熊猫DEVC++ 输入密码 f0tp 后选择 RedPanda.C++.3.3.win64.MinGW64_11.5.0.Setup.exe 并选择「普通下载」。
  2. 下载完成后「运行」 -> 选择语言「简体中文」-> 「我接受(I)」-> 「下一步(N) >」 -> 取消选择「快捷方式」,「下一步(N) >」 -> 将安装目录改为: D:\Program Files\RedPanda-Cpp 然后选择「安装(I)」。
  3. 等待安装完毕后取消勾选「运行 小熊猫C++ 3.2 (x64)(R)」并选择「完成(F)」。

下载 VS Code C++ 扩展

在「扩展(Ctrl+Shift+X)」中搜索扩展并安装 C/C++,等待其安装完毕。

配置路径

  1. 写一个头文件。
  2. 将鼠标悬浮在报错位置,点击报错信息中的「快速修复」(或按下 Ctrl+.)
    • 若使用的是万能头(bits/stdc++.h),点击「编辑 “includePath” 配置」,然后在「编辑器路径」一栏填入编译器位置 D:\Program Files\RedPanda-Cpp\mingw64\bin\g++.exe
    • 若使用的不是万能头,则点击「选择 Intelli Sense 配置以查找系统标头」,再点击「在我的计算机上选择另一个编译器…」,选择找到 D:\Program Files\RedPanda-Cpp\mingw64\bin\g++.exe 并选择「确定」
  3. 点击上方的「运行 C/C++ 文件」即可。

配置编译所需内容

在打开的文件中创建一个名为 .vscode 的文件夹,并在其中添加以下三个文件。

c_cpp_properties.json

JSON
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "D:\\Program Files\\RedPanda-Cpp\\mingw64\\bin\\g++.exe"
        }
    ],
    "version": 4
}

settings.json

JSON
{
    "C_Cpp.default.compilerPath": "D:\\Program Files\\RedPanda-Cpp\\mingw64\\bin\\g++.exe",
    "files.associations": {
        "iostream": "cpp",
        "array": "cpp",
        "bitset": "cpp",
        "string_view": "cpp",
        "initializer_list": "cpp",
        "ranges": "cpp",
        "span": "cpp",
        "regex": "cpp",
        "utility": "cpp",
        "valarray": "cpp",
        "ostream": "cpp"
    }
}

tasks.json

JSON
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "D:\\Program Files\\RedPanda-Cpp\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "D:\\Program Files\\RedPanda-Cpp\\mingw64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

代码片段

将其加入到此文件夹: C:\Users\username\AppData\Roaming\Code\User\snippets 其中的 username 需要替换为当前用户名。

缺省源(cpp.json)

JSON
{
"template for C++":{
    "prefix": "cpp",
    "body": [
        "#include<bits/stdc++.h>",
		"using namespace std;",
		"",
		// "#define MAX(x, y) (((x) >= (y)) ?(x) :(y))",
		// "#define MIN(x, y) (((x) <= (y)) ?(x) :(y))",
		// "#define ABS(x) (((x) >= 0) ?(x) :(-x))",/*wrong*/
		// "#define FOR(st, ed, i) for (int i = (st); i <= (ed); ++i)",
		// "#define RFOR(st, ed, i) for (int i = (ed); i >= (st); --i)",
		"#define fir first",
		"#define sec second",
		"",
		"// #define debug",
		"",
		"typedef long long ll;",
		"typedef double db;",
		"typedef pair<int, int> pii;$0",
		"",
		"void init () {",
		"}",
		"int main () {",
		"\tinit();",
		"\t",
		"\treturn 0;",
		"}",
    ],
    "description": "template"
}
}

题目信息(cpp.problem information.code-snippets)

JSON
{
"problem information": {
	"scope": "cpp",
	"prefix": "problem_information",
	"body": [
		"/*",
		"URL:$1",
		"id:$2",
		"difficulty:$3",
		"tags:$4",
		"*/"
	],
	"description": "print the information of the problem"
}
}

快读快输(cpp.fast read&write.code-snippets)

JSON
{
"fast read&write":{
	"scope": "cpp",
    "prefix": "fast r&w",
    "body": [
		"int readint () {",
		"\tint x = 0, w = 1;",
		"\tchar ch = '\\0';",
		"\twhile (ch < '0' || ch > '9') {",
		"\t\tif (ch == '-') w = -1;",
		"\t\tch = getchar();",
		"\t}",
		"\twhile (ch >= '0' && ch <= '9') {",
		"\t\tx = (x  <<  3) + (x  <<  1) + (ch - '0');",
		"\t\tch = getchar();",
		"\t}",
		"\treturn w*x;",
		"}",
		"void writeint (int x) {",
		"\tint top = 0;",
		"\tint sta[35];",
		"\tdo {",
		"\t\tsta[top++] = x%10;",
		"\t\tx /= 10;",
		"\t} while (x);",
		"\twhile (top) {",
		"\t\tputchar(sta[--top] + '0');",
		"\t}",
		"}",
    ],
    "description": "use 'fast read&write' to run fast"
}
}

使用 prama 开启优化(cpp.run fast by use pragma.code-snippets)

JSON
{
"fast by use pragma":{
	"scope": "cpp",
    "prefix": "pragma fast",
    "body": [
        "#pragma GCC target(\"avx\")",
        "#pragma GCC optimize(1, 2, 3, \"Ofast\", \"-ffast-math\")",
		"",
    ],
    "description": "use pragma to run fast"
}
}
原文