在 VSCode 中 , 创建 cpp 源码 :
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
//strin
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
生成了 tasks.json 构建脚本 ;
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ 生成活动文件",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "编译器: /usr/bin/g++"
}
]
}
编译时报如下错误 :
> Executing task: C/C++: g++ 生成活动文件 <
无法生成和调试,因为活动文件不是 C 或 C++ 源文件。
终端进程启动失败(退出代码: -1)。
终端将被任务重用,按任意键关闭。
核心报错是 无法生成和调试,因为活动文件不是 C 或 C++ 源文件。
没有找到 C/C++ 文件 ;
在 tasks.json 构建脚本中 , 指定 C/C++ 文件路径的是 "tasks
下的 "args"
路径 ,
当前配置的 g++ 参数的 args 配置如下 :
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"
修改后的 tasks.json 构建脚本 如下 :
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ 生成活动文件",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${workspaceFolder}/*.cpp",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "编译器: /usr/bin/g++"
}
]
}
使用 Ctrl + Shift + B 快捷键 , 即可完成编译操作 ;
> Executing task: C/C++: g++ 生成活动文件 <
正在启动生成...
/usr/bin/g++ -fdiagnostics-color=always -g /home/octopus/vscode/helloworld/*.cpp -o /home/octopus/vscode/helloworld/.vscode/tasks
生成已成功完成。
终端将被任务重用,按任意键关闭。
生成的可执行文件在 .vscode 目录下 , 名称是 task ;
执行 cd .vscode
命令 , 进入 .vscode 目录中 ,
使用 ./task
命令 , 执行该 task 可执行文件 , 打印如下内容 ;
Hello C++ World from VS Code and the C++ extension!