VSCode和mingw的安装就不介绍了,VSCode对单文件编译十分友好,但如果编写多文件项目的话,就比较复杂。
基本上有两种方式支持多文件编译
1.单纯使用g++命令(不太流行,但不需要安装插件)
g++ -g main.c 其它文件.c -o out.exe`
2.使用CMAKE(推荐)
Cmake和mingw安装网址如下:
链接: https://pan.baidu.com/s/1rgPcvq1ePNn44FiQk_tN9w
提取码: g69w
把CMAKE文件下载后解压,并添加路径(到bin目录止) 同时在VSCode中安装插件: CMAKE 和 CMAKETools
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "D:\\Code\\C++\\cfg\\mingw64\\bin\\g++.exe",
//command需更改为自己的mingw目录
"args": [
"-g",
//"${file}",//单文件编译
"${fileDirname}\\*.cpp",//多文件编译
"-o",
"${fileDirname}\\out.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: D:\\Code\\C++\\cfg\\mingw64\\bin\\g++.exe"
//需更改为自己的mingw目录
}
]
}
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}\\out.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\Code\\C++\\cfg\\mingw64\\bin\\gdb.exe",
//需更改为自己目录
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
详细教程也可参考视频
https://www.bilibili.com/video/BV13K411M78v?p=2
值得一提的是 在编程时 自己编写的.h文件使用相对路径必须用引号。
还有 以上步骤是使用F5(如果键盘已经对F5键盘有默认操作,则Fn+F5)运行调试才可,如果是安装了Code Runner插件一键运行时还是只能单文件编译,可通过更改其默认命令 使用g++ -g main.c 其它文件.c -o out.exe
的形式。
如果还不懂的话 可评论或私信。