visual-studio-code - 在 Linux 上配置 VS Code (tasks.json) 为 .c 文件调用 gcc,为 .cpp 调用 g++(默认)
问题描述
我一直在玩弄 Linux 上最新的 VS Code,我偶然发现了一个令人沮丧的场景,即默认情况下tasks.json
无法为文件调用 gcc 并在按下 时.c
默认调用 g++ 。在 VS Code 中的 Linux 上使用 C++有助于设置属性-- 但问题是 VS Code 似乎将 视为涵盖 C/C++ 的单一类型.cpp
Ctrl + Shift + B"isDefault": true
group
type
cppbuild
(可能由于它的起源是 with cl.exe
,单个编译器采用/Tp
并/Tc
区分类型作为选项)
我可以将其中一个设置为"isDefault": true
默认情况下将调用该任务,但是如果将其设置为默认值,这显然不适用于 cpp 文件gcc
。
我想要做的是gcc
当我按下Ctrl + Shift + B一个.c
文件处于活动状态以及g++
一个.cpp
文件在窗口中处于活动状态时调用。如上所述,tasks.json
允许在 with 中设置默认任务:group:
:
"group": {
"kind": "build",
"isDefault": true
},
但这仅适用于一项任务,C 或 C++。两者都包含它与无默认值相同,系统会提示您从下拉命令窗口中选择一个命令。听起来不错,"problemMatcher"
但这仅适用于解析编译器输出。
有没有其他方法可以让它选择正确的编译器${fileExtname}
?tasks.json
我目前tasks.json
有 3 个任务。第一个只是检查该bin/
目录是否存在于当前目录中,如果不存在,则将其符号链接到我在tmpfs/tmp/bin
上放置用于回答问题的可执行输出的位置,以便在重新启动时自动删除它们。和任务都依赖于在调用编译器之前调用它以确保可执行输出存在。gcc
g++
./bin
接下来的两个任务是我尝试根据文件扩展名默认使用的gcc
和任务。g++
该tasks.json
文件是:
{
"version": "2.0.0",
"tasks": [
{
"label": "symlink",
"type": "shell",
"command": "[ -d bin ] || ln -s /tmp/bin bin",
"options": {
"cwd": "${fileDirname}"
},
},
{
"type": "cppbuild",
"label": "gcc build w/full warnings, C11, Ofast",
"command": "/usr/bin/gcc",
"args": [
"-Wall",
"-Wextra",
"-pedantic",
"-std=c11",
"-Ofast",
"-Wl,-s",
"${file}",
"-o",
"${fileDirname}/bin/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "shared",
"showReuseMessage": true,
"clear": false
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/gcc",
"dependsOrder": "sequence",
"dependsOn": [ "symlink" ]
},
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-Wall",
"-Wextra",
"-pedantic",
"-std=c++17",
"-Ofast",
"-Wl,-s",
"${file}",
"-o",
"${fileDirname}/bin/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "shared",
"showReuseMessage": true,
"clear": false
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/g++",
"dependsOrder": "sequence",
"dependsOn": "symlink"
}
]
}
有没有办法根据文件扩展名选择任务,或者我只是tasks.json
想做一些它不应该做的事情?
解决方案
推荐阅读
- python-3.x - 为什么 npy 的大小比 csv 大?
- haskell - 尝试使用未加载的模块'main:Prelude'(.\Prelude.hs)时出现runhaskell错误
- express - NuxtJs serverMiddleware 导致生产错误
- c++ - 括号运算符在 C++ 中的作用是什么
- javascript - window.location.href 不提交表单
- generics - 我该如何处理这个“这不是被覆盖的子类型”错误
- javascript - selenium-webdriver 未使用 npm 安装在 ubuntu 中
- javascript - 如何使用 ajax 将带有图像文件的表单数据上传到 django 模型
- java - 为什么我在扩展类时会收到 ClassCastException?
- python - 无法从另一个脚本调用 py 脚本