Error message when running C++ programs in Visual Studio Code with "Run Without Debugging" option

Viewed 211

I'm using Visual Studio Code to learn C++, and have a program with the following code:

#include <cstdio>
    
class ClockOfTheLongNow {
private:
    int year;
public:
    void add_year(){
        year++;
    }
    bool set_year(int new_year){
        if (new_year < 2019) return false;
        year = new_year;
        return true;
    }
    int get_year(){
        return year;
    }
};

int main(){
    ClockOfTheLongNow clock;
    if(!clock.set_year(2019)){
        clock.set_year(2019);
    }
    clock.add_year();
    printf("year: %d", clock.get_year());
}

I go up to the bar at the top and press Run > Run Without Debugging: Option being selected

When I do that, I get the following error message: launch: program 'enter program name, for example C:\Users\drago\Desktop\cpp\a.exe' does not exist

When I open launch.json, it looks like this:

    {
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "enter program name, for example ${workspaceFolder}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/path/to/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

The error message makes me think the IDE isn't automatically using the path of the file. For a similar problem in C someone answered that the .json file should be edited, but it would be a pain to have to do that every time. Is there a way to make VS Code do it automatically?

I was able to work around it by installing the Code Runner extension, which allows me to run the code successfully with the arrow button in the upper-right hand corner of the screen, but I also want to be able to do it with the Visual Studio Code's builtin Run options.

I have MinGW installed and added to my PATH environment variable in Windows.

1 Answers

You may be experiencing this issue because you have defined the executable in file launch.json as follows:

"program": "enter program name, for example ${workspaceFolder}/a.exe"

Update the program field in the launch.json file as follows:

"program": "${fileDirname}\\${fileBasenameNoExtension}.exe"

In this case, you also need to update the args field in the tasks.json file as follows. In this way, when the C++ source code is compiled, the g++ command line tool will use the -o flag to generate the executable in the filename.

"args": [
    "-fdiagnostics-color=always",
    "-g",
    "${file}",
    "-o",
    "${fileDirname}\\${fileBasenameNoExtension}.exe"
]
Related