'VScode compile C++ on windows the exe not found

I want to ask how to debug a simple hello world output from a C++ file, on the launch file I have to put the executable but I have only created a C++ file, how to compile it, I have tried everything, please help.

{
    "version": "0.1.0",
    "command": "g++",
    "isShellCommand": true,
    // compiles and links with debugger information
    "args": ["-g", "-o", "main.exe", "main.cpp"],
    // without debugger information
    // "args": ["-o", "hello.exe", "hello.cpp"],
    "showOutput": "always"
}


Solution 1:[1]

Pretty old question but here's a clear explanation for anyone in future.

Issue is that the debugger was looking for a.exe but your build file will probably be named different.

enter image description here

Changing the values of program variable to "${workspaceFolder}\\${fileBasenameNoExtension}.exe" will fix this issue. This should be in the tasks.json and the launch.json.

This env variable takes care that the right name is substituted. Now set a breakpoint and press f5.

Further details here and here.

Here's a full preview of a working launch.json

"configurations": [
{
    "name": "(gdb) Launch",
    "type": "cppdbg",
    "request": "launch",
    "program": "${workspaceFolder}\\${fileBasenameNoExtension}.exe",
    "args": [],
    "stopAtEntry": false,
    "cwd": "C:\\MinGw\\bin",
    "environment": [],
    "externalConsole": false,
    "MIMode": "gdb",
    "miDebuggerPath": "C:\\MinGw\\bin\\gdb.exe",
    "setupCommands": [
        {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
        },
        {
            "description":  "Set Disassembly Flavor to Intel",
            "text": "-gdb-set disassembly-flavor intel",
            "ignoreFailures": true
        }
    ]
}
    

And a full preview of tasks.json.

"tasks": [
    {
        "type": "cppbuild",
        "label": "C/C++: g++.exe build active file",
        "command": "C:\\MinGW\\bin\\g++.exe",
        "args": [
            "-fdiagnostics-color=always",
            "-g",
            "${file}",
            "-o",
            "${fileDirname}\\${fileBasenameNoExtension}.exe"
        ],
        "options": {
            "cwd": "${fileDirname}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "detail": "Task generated by Debugger."
    }
],
"version": "2.0.0"

NOTE: As explained here and here, you might want to set "externalConsole": true if your code needs input from user.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1