前期准备
下载 VsCode
官网地址:Code Editing
下载 MinGW64编译器
官网地址:MinGW64
配置系统环境变量
Win+I 打开系统设置
找到 编辑系统环境变量
点击 高级 > 环境变量 > 选定 Path 进行编辑
添加进你安装的 MinGW64 编译器的 路径
例如我的就是:D:\Program\mingw-w64\mingw64\bin
配置相关文件
安装 C/C++ 扩展
进入 VsCode 找到左侧栏的 Extensions
一般安装如下扩展:
C/C++
Chinese (Simplified)(简体中文)
Code Runner
设置编译器路径
新建 Code 文件夹用来保存项目文件
在 Code 文件夹中右键点击 用Code打开 进入 VsCode
接下来配置编译器路径
按快捷键Ctrl+Shift+P调出命令面板,输入C/C++,选 Edit Configurations(UI) 进入配置
在里面找到 编译器路径 修改成 MinGW64路径,再将下方的 InteliSense模式 改为 gcc-x64 即可
这时我们可以发现文件夹中多了一个 .vscode 的文件夹,证明配置成功
接下来配置三个主要的文件
c_cpp_properties.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 { "configurations" : [ { "name" : "Win32" , "includePath" : [ "${workspaceFolder}/**" ], "defines" : [ "_DEBUG" , "UNICODE" , "_UNICODE" ], "compilerPath" : "D:/Program/mingw-w64/mingw64/bin/g++.exe" , "cStandard" : "c17" , "cppStandard" : "c++17" , "intelliSenseMode" : "gcc-x64" } ], "version" : 4 }
tasks.json
接下来,创建一个tasks.json文件来告诉VS Code如何构建(编译)程序
该任务将调用g编译器基于源代码创建可执行文件
按快捷键 Ctrl+Shift+P 调出命令面板,输入 tasks,选择 Tasks:Configure Default Build Task
再选择 **C/C : g++.exe build active file**
接下来在 .vscode 文件夹中就会出现 tasks.json 的配置文件
可以参考我的配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 { "version" : "2.0.0" , "tasks" : [ { "type" : "cppbuild" , "label" : "g++.exe build active file" , "command" : "D:/Program/mingw-w64/mingw64/bin/g++.exe" , "args" : [ "-fdiagnostics-color=always" , "-g" , "${file}" , "-o" , "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options" : { "cwd" : "D:/Program/mingw-w64/mingw64/bin" }, "problemMatcher" : [ "$gcc" ], "group" : { "kind" : "build" , "isDefault" : true }, "detail" : "编译器: D:/Program/mingw-w64/mingw64/bin/g++.exe" } ] }
launch.json
点击菜单栏的 Debug–>Start Debugging 或者直接按键盘 F5
会在 .vscode 文件夹中生成一个 launch.json 的配置文件
我的配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 { "version" : "0.2.0" , "configurations" : [ { "name" : "(gdb) Launch" , "preLaunchTask" : "g++.exe build active file" , "type" : "cppdbg" , "request" : "launch" , "program" : "${fileDirname}\\${fileBasenameNoExtension}.exe" , "args" : [], "stopAtEntry" : false , "cwd" : "${workspaceFolder}" , "environment" : [], "externalConsole" : true , "MIMode" : "gdb" , "miDebuggerPath" : "D:\\Program\\mingw-w64\\mingw64\\bin\\gdb.exe" , "setupCommands" : [ { "description" : "Enable pretty-printing for gdb" , "text" : "-enable-pretty-printing" , "ignoreFailures" : true } ] } ] }
运行测试
写一个 hello world! 的程序测试下环境配置是否成功
记得要在末尾插入 system(“pause”) 的代码,不然程序会闪一下就消失了
出现如图所示结果则成功: