2

I'm trying to debug a C program using Visual Studio Code on Windows 10, which I have the C/C++ extension installed in.

My problem is that when I create Source.c in my workspace ( E:\Docs\c ), write some code then hit F5, it shows an error message launch: program 'E:\Docs\c\a.exe' does not exist, which means VSCode doesn't do the compiling thing.

Meanwhile when I go to the console and type gcc source.c, which creates a.exe in the same folder, and hit F5 again it starts with no problems, but doing that every time I want to run the code is annoying.

So, is there a way to compile the code from inside VSCode ?

Here is my c_cpp_properties.json :

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}",
                "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\lib\\gcc\\i686-w64-mingw32\\8.1.0\\include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "intelliSenseMode": "msvc-x64"
        }
    ],
    "version": 4
}

And this is launch.json :

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

1 Answer 1

2

I think you should add prelaunched task with label of your build task to launch.json like this:

"preLaunchTask": "build" // label of your build task

This means you should have in your tasks.json following task with label build e.g.

    "tasks": [
    {
        "label": "build",
        "type": "shell",
        "command": "gcc -g source.c"
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]

Also "-g" flag is important for enabling debugging

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

2 Comments

Hello, where should I add "preLaunchTask": "build" // label of your build task ?
@Pierre-LouisDeschamps add it to configuration in launch.json

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.