VSCode在构建C文件时报告活动文件不是C或C++源文件。

4

在构建 C 程序时,vscode 报告了一个错误。错误信息如下。

Cannot build and debug because the active file is not a C or C++ source file.
The terminal process failed to launch (exit code: -1).

任务配置文件如下。
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/gcc",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "/home/xxx/tmp/test/main.c"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

我有一个名为 main.c 的 c 文件,位于工作空间文件夹 /home/xxx/tmp/test 下。问题的原因可能是什么?

2个回答

1
根据Sean McManus在回复中所示,您需要删除tasks.json中所有的${file}引用,例如:
"args": [
    "-g",
    "${file}",
    "-o",
    "${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
    "cwd": "${fileDirname}"
}

更改为

"args": [
    "-g",
    "test.cpp",
    "-o",
    "libs/test.o"
],
"options": {
    "cwd": "${fileDirname}"
}

将构建类型从cppBuild更改为shell


0
我也遇到了与上述相同的错误。然而,当我在主函数中标记调试并调试程序中的其他行时,然后运行“开始调试(F5)”,调试就能正常工作了。
对于那些遇到类似问题的人来说,这是一个有帮助的解决方案。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接