目录
[一、下载 mingw64](#一、下载 mingw64)
[3、左 { 不换行](#3、左 { 不换行)
++上了两年大学,都还没花心思去搭建 C 语言编程环境,惭愧,惭愧。++
一、下载 mingw64
mingw64 是著名的 C/C++ 编译器 GCC 的 Windows 版本。
下载地址:MinGW-w64 - for 32 and 64 bit Windows download | SourceForge.net
① 进入网页后选择 Files 选项卡
② 往下划,找到最新版本的 mingw64,下载如下版本
③ 解压压缩包得到 mingw64 文件夹
mingw64 应该是一款绿色软件,安装和卸载的方法:下载文件夹和删除文件夹。
二、配置环境变量
① 右击 "此电脑",选择 "属性"(如果你没有 "此电脑" 图标,那就去资源管理器找)
② 高级系统设置 << 环境变量...(在右下角)<< Path(双击或选中后点击 "编辑" 打开)
③ 新建环境变量,填入你的 mingw64 文件夹下 bin 目录的路径(不要照着写!)
④ 打开 cmd,输入以下指令,测试环境变量是否生效
bash
gcc -v
如下图所示,测试成功:
如果出现类似 "不认识 gcc" 的报错,那么可能是路径写错了或者需要重启一下电脑?
三、三个配置文件
在你放代码文件夹下创建一个名为 ".vscode" 的文件夹,并在其中创建三个文件:
- c_cpp_properties.json
- launch.json
- tasks.json
c_cpp_properties.json
注意:把所有的 C:/Program Files/MinGW 换成你的 mingw64 所在目录的路径!
bash
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceRoot}",
"C:/Program Files/MinGW/mingw64/include/**",
"C:/Program Files/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"C:/Program Files/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"C:/Program Files/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"C:/Program Files/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"C:/Program Files/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"C:/Program Files/MinGW/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}",
"C:/Program Files/MinGW/mingw64/include/**",
"C:/Program Files/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"C:/Program Files/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"C:/Program Files/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"C:/Program Files/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"C:/Program Files/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"C:/Program Files/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
]
}
}
],
"version": 4
}
launch.json
bash
{
"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": []
},
{
"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\\MinGW\\mingw64\\bin\\gdb.exe", // 自己电脑的gdb
"preLaunchTask": "echo", //这里和task.json的label相对应
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
tasks.json
bash
{
// 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": "new",
"showReuseMessage": true,
"clear": false
}
}
四、格式化代码
1、安装插件
2、保存时自动格式化
- 依次点击 VSCode 左下角齿轮 << 设置,然后输入:Format On Save
- 勾选
3、左 { 不换行
- 依次点击 VSCode 左下角齿轮 << 设置,然后输入:C_Cpp: Clang_format_style
- 将默认的 file 改为:{BasedOnStyle: Chromium, IndentWidth: 4}
参考博客: