文章目录



可以参考官方提供的文档 :​https://code.visualstudio.com/docs/cpp/config-linux​



使用 VSCode 开发 C/C++ 程序 , 涉及到 3 3 3 个配置文件 :

① tasks.json : 编译器构建 配置文件 ;

② launch.json : 调试器设置 配置文件 ;

③ c_cpp_properties.json : 编译器路径和智能代码提示 配置文件 ;

下面开始逐个 生成 上述配置文件 ;






一、创建 tasks.json 编译器构建配置文件



tasks.json 编译器构建配置文件 , 用于告诉 VSCode 如何去编译这个程序 ;

菜单栏选择 " 终端 / 配置默认生成任务 " ,

【开发环境】Ubuntu 中使用 VSCode 开发 C/C++ ④ ( 创建 tasks.json 编译器构建配置文件 | tasks.json 编译器构建配置文件分析 )_visual studio code

在弹出的对话框中 , 选择第 2 2 2 项 , " C/C++:g++ 生成活动文件 " 选项 ;

【开发环境】Ubuntu 中使用 VSCode 开发 C/C++ ④ ( 创建 tasks.json 编译器构建配置文件 | tasks.json 编译器构建配置文件分析 )_Ubuntu_02

点击该选项 , 即可在 .vscode 目录中生成 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++"
}
]
}

【开发环境】Ubuntu 中使用 VSCode 开发 C/C++ ④ ( 创建 tasks.json 编译器构建配置文件 | tasks.json 编译器构建配置文件分析 )_Ubuntu_03




二、tasks.json 编译器构建配置文件分析



​"label": "C/C++: g++ 生成活动文件",​​ 是编译 C/C++ 任务名称 , 该任务名称可以自定义 ;

​"command": "/usr/bin/g++",​​ 中的 command 配置 , 是指定编译器 , 一般是 gcc 或者 g++ 编译器 ;

​"args"​​ 数组 , 配置的是 command 指定的编译器后的编译选项 ;

"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],

​"group"​​​ 中的 ​​"isDefault": true​​ 指的是 , 使用 Ctrl + Shift + B 快捷键可以运行该任务 , 如果设置为 false , 需要从终端菜单中 , 选择 " 运行任务 " 来编译运行程序 ;

"group": {
"kind": "build",
"isDefault": true
},