如何使用vscode快速配置C语言环境(简单实用)

需要用到的工具:

VSCode(Visual Studio Code)

一、首先打开官网链接,然后根据自己的电脑选择合适的安装程序进行下载。

二、在安装时默认点击下一步,最后记得勾选上添加path到系统变量里,方便鼠标右键直接用vscode打开相应文件。如图:

安装vscode

C语言编译包(MinGW-w64)

一、前面的工作做完后,然后是下载MinGW-w64编译工具,这里不推荐下载上面绿色部分的在线安装包,比较麻烦,直接下载下面的压缩包,如图所示:
mingw-w64

下载完成后解压文件,可以自行选择目录,我这里是选择D盘下的软件目录。这里一定不要忘了添加环境变量(鼠标右键此电脑——属性——高级系统设置——环境变量——系统变量path),需要进入bin和include文件夹并把他们的路径添加到path里面。由于我是把mingw64安装在D:\Users\data\software里面的,所以如图所示:

mingw64
path_position

环境变量配置好,就来验证一下,打开win+R,输入cmd命令,回车。第一步,输入gcc -v,回车。第二步,输入gdb,回车。若出现下面这些情况,说明你的环境变量已经没有问题。
gcc -v

gdb

前面的工作已经完成,这里就开始下面的工作。

安装vscode插件

这里推荐安装C/C++以及C/C++ Compile Run插件。如图:
C/C++
C/C++ Compile Run

安装好之后就开始我们的编译准备吧。
打开vscode的工作目录,新建一个hello.c文件,在这里输入:

#include <stdio.h>
#include <stdlib.h>
    
int main()
{
    printf("helloworld!");
    return 0;
}

由于没有进行vscode的C语言环境配置,所以会出现很多问题。这里就不一一阐述了,在工作目录的下建立一个.vscode的文件夹用于存储一些配置。由于是C语言,这里建立三个文件,分别是:c_cpp_properties.json,launch.json,tasks.json。具体内容如下:

// c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                "D:/Users/data/software/mingw64**",
                "D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                "D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                "D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                "D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                "D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                "D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=6",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            "intelliSenseMode": "msvc-x64",
            "browse": {
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": "",
                "path": [
                    "${workspaceRoot}",
                    "D:/Users/data/software/mingw64/include/**",
                    "D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                    "D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                    "D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                    "D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                    "D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                    "D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
                ]
            }
        }
    ],
    "version": 4
}
// launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(Windows) Launch",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "cmd",
            "preLaunchTask": "echo",
            "args": [
                "/C",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "&",
                "echo.",
                "&",
                "pause"
            ],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole":true
        },
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\Users\\data\\software\\mingw64\\bin\\gdb.exe",// 自己电脑的gdb
            "preLaunchTask": "echo",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
 
        }
    ]

}
// tasks.json
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "gcc",
            "args": [
                "-g", 
                "${file}", 
                "-o", 
                "${fileBasenameNoExtension}.exe",
                "-fexec-charset=GBK"//解决中文乱码
            ]
        }
    ],
    "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "shared", 
        "showReuseMessage": true,
        "clear": false
    }

}

其中,通过Ctrl+H快捷键,把D:/Users/data/software/mingw64替换成你自己的mingw64安装路径即可。好了,现在就能成功进行编译了。由于我这里在C/C++ Compile Run插件里面设置了Run in a new external terminal,所以会弹出新的cmd窗口。

配置C/C++ Compile Run

在我们刚刚写的c文件页面,按F6即可运行,运行结果如下:

运行结果

另外,为了节省大家的时间,附上我打包的vscode以及mingw64和.vscode的地址,有问题欢迎在下方留言!

posted @ 2021-01-26 10:44  likepoems  阅读(2161)  评论(0编辑  收藏  举报