C:\mingw64\bin //( 此处为 MinGW 安装目录下的 /bin 文件夹)
"code-runner.runInTerminal": true, //决定是否在 VSCode 内的终端执行
"code-runner.saveAllFilesBeforeRun": true, //自动保存所有打开的文件再编译
"code-runner.saveFileBeforeRun": true, ////自动保存当前打开的文件再编译
"code-runner.executorMap": {
"cpp": "cd $dir && g++ -Wall $fileName -o $workspaceRoot\\bin\\$fileNameWithoutExt.exe && ./\"bin\\$fileNameWithoutExt.exe\"",
"c": "cd $dir && gcc -Wall $fileName -o $workspaceRoot\\bin\\$fileNameWithoutExt.exe && ./\"bin\\$fileNameWithoutExt.exe\""
},
对 executorMap 中参数的解释 :
$dir : 当前 VSCode 工作目录
$fileName : 当前文件名 ( 含后缀 )
$workspaceRoot : 当前工作目录绝对路径
$fileNameWithoutExt : 无后缀的文件名
gcc / g++ 的参数 : 开启 -Wall 选项 ( 编译时确定更多警告内容, 譬如 printf/scanf 的参数个数问题 )
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "gcc.exe build active file",
"command": "C:\\mingw64\\bin\\gcc.exe",
"args": [
"-Wall", // 开启 -Wall 选项
"-g",
"${file}",
"-o",
"${fileDirname}\\bin\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\mingw64\\bin" // MinGW 的 bin 目录位置
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
一些解释:
该配置会把编译完成后的后缀为 .exe 的可执行文件 统一放置在 工作目录的 bin 文件夹下, 此举是为了
保持 VSCode 左边目录树的整洁, 可以自行修改
{
// 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": "gcc.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false, // 此项决定时候打开外部窗口来执行 .exe, 这里选了否
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe", //同样要设置为 MinGW 的安装目录内 bin/gdb.exe 的绝对路径
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "gcc.exe build active file"
}
]
}
在调试带有参数的 C/C++ 程序时需要进行的额外设置:
在上文的 launch.json 中有
"args":[],
每次调试带参数的程序时只需要修改为
"args":["参数1","参数2"],
即可(这样的确是有点麻烦QAQ不过还好啦).