user3236879
user3236879

Reputation: 601

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?

Upvotes: 5

Views: 41177

Answers (3)

Aruliya Asokan
Aruliya Asokan

Reputation: 1

Maybe your "Run in Terminal" box is not checked.

You might wanna go to the hamburger icon on the top left corner of VScode > then select "File" > select "Preferences" > then click on "Settings" > choose "Extensions" from the Commonly Used > then from there only scroll down and open "Run Code Configuration" > After that, now come under that and scroll slowly and you'll find "Run In Terminal" unchecked, just check that box.

After that, your C/C++ code will run in the terminal when you click on Run Code.

Upvotes: 0

congdc
congdc

Reputation: 65

I also got the same error as above. However, when I mark debug in the main function and debug some other lines in the program. Then, ran "start debugging (F5)" then debug worked for me.

Helpful solution for those experiencing similar problems.

Upvotes: 0

Ignatij
Ignatij

Reputation: 86

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",
    "test.cpp",
    "-o",
    "libs/test.o"
],
"options": {
    "cwd": "${fileDirname}"
}

And change type of build from cppBuild to shell.

Upvotes: 4

Related Questions