visual-studio-codebuild

vscode report that the active file is not a C or C++ source file while building a c file


The vscode report an error while building a c program. The error message is as below.

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).

The task config file is as below.

{
    "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
            }
        }
    ]
}

I have a c file named main.c under the folder /home/xxx/tmp/test which is the workspace folder. What might be the cause of the problem?


Solution

  • As seen in this reply from Sean McManus, you need to get rid of all ${file} references in your tasks.json, for example:

    "args": [
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}"
    ],
    "options": {
        "cwd": "${fileDirname}"
    }
    

    change to

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

    And change type of build from cppBuild to shell.