VS Code + MinGW搭建C/C++编译环境
安装VS Code,将MinGW免安装版解压后配置环境变量,然后检测GCC编译是否生效gcc -v。
C:\SoftWare\mingw64\bin
GCC编译器生效后,在VS Code中创建C/C++工程,VS Code需要配置launch.json和tasks.json
分别如下:
{
// 使用 IntelliSense 了解相关属性。 `
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false, // 表示使用外部终端还是VS Code的终端界面
"MIMode": "gdb",
"miDebuggerPath": "C:\\SoftWare\\mingw64\\bin\\gdb.exe",
"preLaunchTask": "g++", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++", //这里注意一下,见下文
"command": "C:\\SoftWare\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\SoftWare\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
]
}
]
}
以上配置完成后,F5运行程序
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v(10, 5);
for (auto &i : v) {
std::cout << i << std::endl;
}
std::cout << "Hello World!" << std::endl;
return 0;
}
运行结果如下