1.在VSCode中安装C/C++插件

2.下载mingw64

MinGW-w64官方网站首页

1、找到downloads

2、找到SourceForge

3、找到一个合适的版本(我这里是下拉找到免安装版)下载,其他的都试过了,都不行(可能是因为外网的关系连接不稳定)

4、直接解压到想要的位置

5.将mingw64添加到环境变量

将下载好的mingw64解压到指定的目录,我的路径是C:\Program Files\mingw64,然后配置环境变量 将目录C:\Program Files\mingw64\bin加入到环境变量path里(注意修改为自己的mingw64路径,具体到bin文件夹)

打开命令行输入gcc -v,出现以下信息说明mingw64配置成功

4.新建.vscode文件夹 在该文件夹创建三个文件

点这里可以直接下载 再修改路径

.vscode文件下载

注意:c_cpp_properties.json和launch.json 需要修改路径

(1)c_cpp_properties.json配置

{"configurations": [{"name": "Win32","includePath": ["${workspaceRoot}","C:\\Program Files\\mingw64"],"defines": ["_DEBUG","UNICODE","__GNUC__=6","__cdecl=__attribute__((__cdecl__))"],"intelliSenseMode": "windows-gcc-x64","browse": {"limitSymbolsToIncludedHeaders": true,"databaseFilename": "","path": ["${workspaceRoot}","C:\\Program Files\\mingw64"//此处修改为自己mingw64的路径]}}],"version": 4}

(2)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": "C:\\Program Files\\mingw64\\bin\\gdb.exe",//修改为自己电脑的gdb路径"preLaunchTask": "echo",//这里和task.json的label相对应"setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}] }]}

(3)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}}

5.代码编译与运行

#include  int main(){ printf("Hello, World! \n"); return 0;}

ctrl+alt+n 运行

vscode配置c语言环境就完成了!

如果打印中文乱码,请看下面这篇文章

vscode 解决C语言printf打印中文乱码问题