how to add mingw compiler in vs code to run c prog

655 Views Asked by At

i am getting error stating launch program does not exist

i installed c compiler mingw that is required for windows still facing errors while compiling. how can i run c program? I have installed all extensions related to c, c extensions, in fact i too have installed code runner. Earlier it showed a run button at the top but now its not showing instead of it is showing config to add.

1

There are 1 best solutions below

1
Usman Mehmood On

You don't need a code runner. You can configure VSCode to allow you to run the code and also debug it in a much better way.

Check for GCC

Go to the terminal and type gcc --version into it.

If you get something like this:

gcc (Rev1, Built by MSYS2 project) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

It means GCC is installed and should be found by VSCode.

tasks.json

You'd have to give the correct paths to tasks.json to perform the builds and runs. Here's an example

{
    "version": "2.0.0",
    "tasks"  : 
    [
        {
            "label"  : "build release",
            "type"   : "shell",
            "command": "gcc main.c -O2 -o ${workspaceFolderBasename}.exe"
        },
        {
            "label"  : "build debug",
            "type"   : "shell",
            "command": "gcc main.c -g3 -o ${workspaceFolderBasename}.exe"
        },
        {
            "label"  : "run_executable",
            "type"   : "shell",
            "command": "./${workspaceFolderBasename}.exe"
        }
    ]
}

This assumes that the executable name you want is the same as your folder name. For example if the folder name is my_c_project then the executable would become my_c_project.exe. You can of course give custom names by replacing ${workspaceFolderBasename}.

To run it or debug it, you'd need GDB. To check for it, use gdb --version in a terminal and it should give you this output.

GNU gdb (GDB) 13.2
Copyright (C) 2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

And also you'd have to configure the launch configuration.

launch.json

Here's an example

{
    "configurations": [
    {
        "name"           : "Debug Config",
        "type"           : "cppdbg",
        "request"        : "launch",
        "program"        : "${workspaceRoot}/${workspaceFolderBasename}.exe",
        "args"           : [],
        "preLaunchTask"  : "build debug",
        "stopAtEntry"    : true,
        "cwd"            : "${workspaceRoot}",
        "environment"    : [],
        "externalConsole": false,
        "MIMode"         : "gdb",
    }
    ]
}

The first important bit in it is the program parameter which should point to the program created after building. You can see that the build debug and build release tasks both create the executable at the base folder, (the same place where your main.c is placed). And so the program property of launch.json is also pointing to the same executable.

And the second important bit is the preLaunchTask parameter which tells the debugger to perform a task before it starts debugging. It's been set to the build debug task, so every time you debug, it will rebuild to let you use the latest code.

Once you've set these up, just go to the debug tab and click on the green debug symbol on the top right.

debug button

And it would start debugging. Viola!