一、官网下载 VS code 并安装
二、下载 Mingw-w64。它包含有gcc、g++、gdb 等一系列工具
三、将 Mingw-w64 的安装目录下的 bin 目录完整路径添加到环境变量 Path 中
1. 拷贝 bin 目录的完整路径
2. 将 完整的 bin 目录 添加到 环境变量 Path 中
不清楚如何添加环境变量的可以看看我的这篇文章《win10 添加环境变量》
四、安装VS code插件
1. 点击左侧一栏的最后一个图标(扩展插件),然后在搜索框中搜索 C/C++。
2. 选中第一个并 “Install” 即可
3. 同样的搜索 “run” 也是选择第一个安装。这个插件可以让程序右键运行
4. 如果想要汉化,就搜索 chinese 安装第一个,然后重启即可。
五、建立 C/C++ 工作区
1. 选择一个文件位置创建一个空文件夹,假设命名为 “C”
2. 然后在 VS code 中打开这个文件夹
3. 接着点击菜单栏的 “文件”,并点击选项 “将工作区另存为”,输入一个文件名并点击 “保存” 会在 C 文件夹下出现一个以 "code-workspace"为后缀的文件
六、环境配置
1. 当前文件夹下新建一个名为 “.vscode” 文件夹
2. 在 “.vscode” 目录下创建四个文件名分别为 “c_cpp_properties.json”、“launch.json”、“settings.json”、“tasks.json”文件
3. “c_cpp_properties.json” 文件内容,注意修改属性值中 Mingw-w64 的目录为你设置的目录,若是 C++ ,就将 gcc 更改为 g++
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"D:/Mingw-w64/mingw64/include/**",
"D:/Mingw-w64/mingw64/lib/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "D:/Mingw-w64/mingw64/bin/gcc.exe",
"cStandard": "c11",
"cppStandard": "c++20",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
4. “launch.json” 文件内容。“preLaunchTask” 属性值一定要与 “tasks.json” 文件里的 “label” 属性值一致。
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:/Mingw-w64/mingw64/bin/gdb.exe",
"preLaunchTask": "gcc",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
5. settings.json 文件内容,让程序在终端 cmd 下运行,这样可以在程序运行时输入数据。因为默认在输出一栏输出,而它是不可编辑的。
{
"code-runner.runInTerminal": true
}
6. “tasks.json” 文件内容。若是 C++ ,就将 “command” 属性值更改为 g++,“owner”属性值改为 “cpp”。“label” 属性值是可以自定义的。
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "gcc",
"type": "shell",
"command": "gcc",
"args": ["-g", "${file}", "-o", "${fileBasenameNoExtension}.exe"],
"problemMatcher": {
"owner": "c",
"fileLocation": ["relative", "${workspaceFolder}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
]
}
七、运行程序,终端输出中文乱码
直接输入 chcp 65001 更改为中文活动代码页即可
到此就算结束了,开始你的编程生涯吧!