vscode配置c/c++环境
作者:互联网
c/c++插件下载
搜索`C/C++ Extension Pack`下载这个扩展包集合
下载MinGW
下载地址:mingw-w64
1.双击安装exe二进制程序
2.在All Packages里面找到并勾选`mingw32-gcc.bin`,`mingw32-gcc-g++.bin`,`mingw32-gdb.bin`。第一个是c语言文件的编译器,第二个是c++的,第三个是用来调试编译后文件的
3.然后点击`Installation`->`Apply all changes`
4.等待安装,估计要一会
5.安装完后,配置环境变量。将MinGW的安装目录下的bin目录添加到系统变量PATH中,我的是`D:\MinGW\bin`
6.在cmd命令行上打`gcc -v`
c/c++ 拓展设置
找到这项,将bin目录\g++.exe添加进去
示例程序test.cpp
#include <stdio.h>
#include <windows.h>
int main()
{
printf("Hello World\n");
system("pause");
return 0;
}
运行和调试
1.vscode左侧菜单栏选择运行和调试图标
2.点击创建launch.json文件
3.编辑launch.json(代码在下面)
4.返回.cpp文件,按F5进行调试,会弹出找不到任务"task g++",选择 "配置任务",会自动生成 tasks.json 文件
5.编辑tasks.json文件(代码在下面)
6.调试运行
7.如下图则表示运行调试成功
launch.json
{
"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:\\MinGW\\bin\\gdb.exe", // 修改为本地gdb所在位置
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "task g++" //修改此项,跟tasks.json文件中的label对应
}
]
}
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "task g++", // 跟launch.json文件中的preLaunchTask对应
"command": "D:\\MinGW\\bin\\g++.exe", // 修改为你本地g++程序的位置
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
标签:bin,exe,launch,vscode,配置,gdb,c++,json,MinGW 来源: https://www.cnblogs.com/weiweivip666/p/16163597.html