ubuntu 18.04+vscode 多文件C++编译环境搭建
按教程完成搭建后,发现针对有多个cpp文件,依然存在无法编译的问题,多次尝试后找到两个解决办法:
使用code runner插件
在拓展插件商店内下载code runner,重启vscode后在code runner拓展里打开setting.json,找到executorMap设置项,将c程序对应的一行中的gcc $filename修改为gcc *.c,将c++对应一行中的g++ filename修改为g++ *.cpp,这样即可保证文件夹下所有程序文件均可被编译,具体配置如下图。code runner是一个支持多种语言的插件,对新手相当友好。
修改launch.json与task.json
第二种方法较为传统,修改默认生成的launch.json与task.json
launch.json
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"preLaunchTask": "build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
task.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": ["-g", "*.cpp", "-std=c++11", "-o", "${fileBasenameNoExtension}.out"]
}
]
}