5

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?

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Instead of test.cpp use "${fileBasenameNoExtension}.cpp"
I actually didn't notice this discrepancy with filenames, thank you! I will change it, however, to a main.c, to fit the post
0

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.

Comments

0

Make sure the "Run in Terminal" option inside the Code Runner extension settings is enabled.

This setting is not part of the C/C++ extension — it's from the Code Runner extension.
To enable it:
In Settings, expand Extensions -> Code Runner, then open Run Code Configuration and search for the Run In Terminal checkbox.

Here is a screenshot showing where the option appears

This option makes your C/C++ program run in the integrated terminal instead of the output window.

1 Comment

I couldn't find 'Run Code Configuration', is that an extension or a sub-heading? Also tried just searching for 'Run in Terminal'. No dice, please expand your answer to include any additional extensions, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.