本文利用Dev-C++中现有的编译器,来配置VS Code的C++运行环境,并支持C++11。

将编译器添加到环境中

找到Dev-C++中的MinGW64编译器的位置,通常位置为

D:\Program Files (x86)\Dev-Cpp\MinGW64

打开系统的环境变量设置,打开用户变量的Path,添加 D:\Program Files (x86)\Dev-Cpp\MinGW64\bin

打开cmd,输入 gcc -v 来检测是否加入成功,如果添加失败,会输出

'gcc' 不是内部或外部命令,也不是可运行的程序或批处理文件。

添加成功后,在cmd中输入 gcc -v -E -x c++ - 来获取链接文件的地址,在cmd输出的结尾,能看到7行路径,保存下来这7行路径待会需要用到。

配置VS Code

VS Code 安装C/C++code runner 拓展

在VScode中打开一个工作文件夹,文件夹目录需要全英文且无特殊符号

打开后会在左侧文件夹中找到一个名叫 .vscode 的文件夹,打开里面的三个文件:launch.jsontasks.jsonc_cpp_properties.json注意要修改路径

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",//指默认文件操作的目录(如fopen的默认寻找位置),${fileDirname}是C/C++文件的目录,修改为${workspaceFolder}就是工作文件夹目录
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "D:/Program Files (x86)/Dev-Cpp/MinGW64/bin/gdb.exe",//路径要修改
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc.exe 生成活动文件"
        }
    ]
}

tasks.json

{
    "version": "2.0.0",
    "command": "gcc",
    "args": [
        "-std=c++11",//C++11
        "-static-libgcc",
        "-g",
        "${file}",
        "-o",
        "${file}.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
        }
    },
    "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "new",
        "showReuseMessage": true,
        "clear": false
    },
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe 生成活动文件",
            "command": "D:/Program Files (x86)/Dev-Cpp/MinGW64/bin/g++.exe",//路径要修改
            "args": [
                "-std=c++11",//C++11
                "-static-libgcc",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "D:/Program Files (x86)/Dev-Cpp/MinGW64/bin"//路径要修改
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "编译器: \"D:/Program Files (x86)/Dev-Cpp/MinGW64/bin/g++.exe\""//路径要修改
        },
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "D:/Program Files (x86)/Dev-Cpp/MinGW64/bin/g++.exe",//路径要修改
            "args": [
                "-std=c++11",//C++11
                "-static-libgcc",
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "D:/Program Files (x86)/Dev-Cpp/MinGW64/bin"//路径要修改
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ]
}

c_cpp_properties.json

在这个文件中,需要填写之前从cmd中得到的7行路径。

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "D:/Program Files (x86)/Dev-Cpp/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++",//路径要修改
                "D:/Program Files (x86)/Dev-Cpp/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/x86_64-w64-mingw32",//路径要修改
                "D:/Program Files (x86)/Dev-Cpp/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/backward",//路径要修改
                "D:/Program Files (x86)/Dev-Cpp/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include",//路径要修改
                "D:/Program Files (x86)/Dev-Cpp/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../include",//路径要修改
                "D:/Program Files (x86)/Dev-Cpp/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include-fixed",//路径要修改
                "D:/Program Files (x86)/Dev-Cpp/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/include"//路径要修改
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "D:/Program Files (x86)/Dev-Cpp/MinGW64/bin/g++.exe",
            "cStandard": "c99",
            "cppStandard": "c++11",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

保存成功后,编写一个程序并保存在工作文件夹下,按下 F5 编译运行,注意文件名不能出现中文!

运行后闪退的解决

上面的 launch.json 中是通过直接启动编译器生成的.exe文件来运行的,运行结束后程序会立刻退出。这里可以利用cmd来运行生成的.exe文件并添加上 pause ,使得程序运行结束后不会立刻退出。

launch.json 修改为以下内容:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "C:\\Windows\\system32\\cmd.exe",
            "args": [
                "/C",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "&",
                "pause"
            ],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",//指默认文件操作的目录(如fopen的默认寻找位置),${fileDirname}是C/C++文件的目录,修改为${workspaceFolder}就是工作文件夹目录
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "D:/Program Files (x86)/Dev-Cpp/MinGW64/bin/gdb.exe",//把这个路径改成自己电脑的gdb.exe路径
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc.exe 生成活动文件"
        }
    ]
}

然后再运行程序,程序运行结束后会出现 请按任意键继续. . .

程序输出中文时乱码的解决

因为控制台所用的编码为GBK,而VS Code所使用的编码为UTF-8,两者编码不一致导致的中文输出乱码。

这里将VS Code所使用的编码改为GBK,与控制台保持一致即可。

在VS Code中按 Ctrl+Shift+P ,输入 user ,点击 打开用户设置 ,在打开界面的最上方的搜索栏里输入 encoding ,在界面里的下拉框中选择 Simplified Chinese (GBK) 即可。

对于已打开的文件,在文件的右下角点击 UTF-8 ,在上方的弹出框中选择 通过编码重新打开 , 选择 Simplified Chinese (GBK) ,这时文件里所有的中文都会变成乱码,删除再重新输入即可。

然后再运行程序,程序将正常输出中文。


循之际,如星夜般的幻想。