一.配置mingw64编译器
1.安装编译器
百度网盘链接:https://pan.baidu.com/s/1rWGunr1D4-67EExWJvLPfg
提取码:n5g5
2.mingw64环境变量配置
(1)将下载文件解压,文件名为x86_64-8.1.0-release-win32-seh-rt_v6-rev0,打开该文件,只有一个名为mingw64的文件,然后复制mingw64目录下bin文件的地址(这里我习惯性的将mingw64文件直接复制到D盘下的Program目录中了);
(2)键盘输入win+R,再输入sysdm.cpl,进入系统属性;
(3)在“系统属性”中,依次单击 “高级”---->"环境变量“;
(4)在"系统变量"中,找到”Path" 单击,再单击编辑;
(5)单击"新建",当前页面最后一行弹出光标,将bin的地址粘贴到光标处,并单击"确定";
(6)键盘输入win+R,再输入cmd 进入cmd.exe,再cmd中输入gcc -v,如下图所示,即成功安装编译器。
二.在Visual Studio Code中安装插件及代码编写
1.打开Visual Studio Code,再”扩展“中输入c,安装插件”C/C++";
2.新建一个文件夹Project,用Visual Studio Code打开该文件夹,并在该文件夹下再建一个文件名为hello.c,其代码如下:
#include
int main() {
printf("Hello World!");
return 0;
}
3.在Project目录下新建.vscode文件夹,在.vscode下,新建文件c_cpp_properties.json,launch.json以及tasks.json,其代码如下:
(1)c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceRoot}",
//此处修改为自己mingw64的路径
"D:\\Program\\mingw64"
],
"defines": [
"_DEBUG",
"UNICODE",
"__GNUC__=6",
"__cdecl=__attribute__((__cdecl__))"
],
"intelliSenseMode": "windows-gcc-x64",
"browse": {
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": "",
"path": [
"${workspaceRoot}",
"D:\\Program\\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": "D:\\Program\\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
{
"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
}
}
4.项目结构如下:
注意:先在Project目录下新建.c文件,然后建.vscode文件夹,否则会出问题!
三.执行及结果
1.在Visual Studio Code中选择“运行” ---> "启动调试“,结果如下:
这样我们就成功了!