VS Code中C/CPP的完美配置(完成环境搭建、解决终端自动闪退、解决无法调试)
作者个人博客:https://cherishpassion.cn/
一、环境搭建
-
下载VS CODE,MinGW
VS CODE: https://code.visualstudio.com/
MINGW:链接:https://pan.baidu.com/s/1Fw-7-_q4waHgy_UmRKWSqQ 提取码:02i9
-
安装VS Code、部署MinGW
-
这里安装VS Code不再赘述,安装到任意目录即可(但不要存在中文!!!)
-
解压MinGW到任意目录(但不要存在中文!!!)
-
配置MinGW环境变量
将MinGW的bin目录配置到Path
此电脑—>属性—>高级系统设置—>环境变量—>编辑Path
添加{你的盘符(目录)}\mingw64\bin (如图)
并检查VS Code路径是否存在
-
-
安装C/CPP插件
- 打开VS Code,在插件市场中搜索C/C++并点击install安装(如图)
- 重启VS Code
- 打开VS Code,在插件市场中搜索C/C++并点击install安装(如图)
-
编辑配置文件
-
创建一个工作目录并在VS Code中打开
文件—>打开文件夹(如图)
-
创建一个.cpp文件(如图)
-
进行调试(快捷键F5)
选择C++(GDB/LLDB)—>g++.exe build and debug active file
-
编辑自动创建的launch.json文件
1.修改"externalConsole"为true
2.修改"preLaunchTask"为 “c/cpp task”
这里给出完整配置文件可对照修改
{ // 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "g++.exe build and debug active file", "type": "cppdbg", "request": "launch", "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "D:\\mingw64\\bin\\gdb.exe", "setupCommands": [ { "description": "为 gdb 启用整齐打印", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "c/cpp task" } ] }
-
进行调试(快捷键F5)—>配置任务—>g++.exe build and debug active file
-
-
编辑弹出的task.json
修改"label"为 “c/cpp task”
这里给出完整配置文件可对照修改
{ // 有关 tasks.json 格式的文档,请参见 // https://go.microsoft.com/fwlink/?LinkId=733558 "version": "2.0.0", "tasks": [ { "type": "shell", "label": "c/cpp task", "command": "D:\\mingw64\\bin\\g++.exe", "args": [ "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "D:\\mingw64\\bin" }, "problemMatcher": [ "$gcc" ], "group": "build" } ] }
-
至此环境搭建完成
二、解决终端自动闪退
-
打开launch.json文件
-
修改"program"为 “C:\Windows\system32\cmd.exe”
-
“args”: ["/C","${fileDirname}\${fileBasenameNoExtension}.exe","&",“pause”]
这里给出完整配置文件可对照修改
{ // 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "g++.exe build and debug active file", "type": "cppdbg", "request": "launch", "program": "C:\\Windows\\system32\\cmd.exe", "args": ["/C","${fileDirname}\\${fileBasenameNoExtension}.exe","&","pause"], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "D:\\mingw64\\bin\\gdb.exe", "setupCommands": [ { "description": "为 gdb 启用整齐打印", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "LaunchTask g++" } ] }
-
-
若你配置正确,现在终端将在程序执行完毕后暂停(即不会闪退)
三、解决无法调试
-
配置launch.json
这里直接给出配置文件,不做赘述,直接按照下文配置即可
{ // 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "g++.exe build and debug active file", "type": "cppdbg", "request": "launch", "program": "C:\\Windows\\system32\\cmd.exe", "args": ["/C","${fileDirname}\\${fileBasenameNoExtension}.exe","&","pause"], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "D:\\mingw64\\bin\\gdb.exe", "setupCommands": [ { "description": "为 gdb 启用整齐打印", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "c/cpp task" }, { "name": "debug", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "D:\\mingw64\\bin\\gdb.exe", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": false } ], "preLaunchTask": "c/cpp task" } ] }
-
配置tesk.json
这里直接给出配置文件,不做赘述,直接按照下文配置即可
{ // 有关 tasks.json 格式的文档,请参见 // https://go.microsoft.com/fwlink/?LinkId=733558 "version": "2.0.0", "tasks": [ { "type": "shell", "label": "c/cpp task", "command": "D:\\mingw64\\bin\\g++.exe", "args": [ "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "D:\\mingw64\\bin" }, "problemMatcher": [ "$gcc" ], "group": "build" }, { "type": "shell", "label": "c/cpp task", "command": "D:\\mingw64\\bin\\g++.exe", "args": [ "-o", "${fileBasenameNoExtension}", "${file}" ], "group": { "kind": "build", "isDefault": true } } ] }
-
在运行和调试中选择debug