首页 > 解决方案 > 在 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++ 的单一类型.cppCtrl + Shift + B"isDefault": truegrouptype 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上放置用于回答问题的可执行输出的位置,以便在重新启动时自动删除它们。和任务都依赖于在调用编译器之前调用它以确保可执行输出存在。gccg++./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想做一些它不应该做的事情?

标签: visual-studio-code

解决方案


推荐阅读