Search code examples
cvisual-studio-codemsys2

"The preLaunch Task 'build C program' terminated with exit code -1073741819" error when using MSYS2


I apologize in advance for any spelling or grammar mistakes; I haven't written in English in a while.

Summary of the problem

I installed the MSYS2 environment to program in C on VS Code (I dislike the online compiler provided by my uni), but I still can't debug my code. I'm using Windows btw.

Further context

So far, I have managed to install all the C extensions in VS Code, as well as the MSYS2 environment (and I added its route to the Windows PATH).

Then, I created a folder named "C Projects" and inside it, I created both a C file named "main.c" and another folder named ".vscode". Inside this last folder, I created two files: "launch.json" and "tasks.json".

The "main.c" file contains simple code to test the environment:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

Both the "launch.json" (first code) and the "tasks.json" (second code) files are meant to allow debugging in VS Code (note that I got help from ChatGPT to write this, so the mistake could be here):

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C/C++: gcc.exe build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files (x86)\\msys64\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build C program",
            "internalConsoleOptions": "neverOpen",
            "logging": { "engineLogging": true }
        }
    ]
}
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build C program",
            "type": "shell",
            "command": "C:\\Program Files (x86)\\msys64\\mingw64\\bin\\gcc.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": ["$gcc"],
            "detail": "Generated task by debugging."
        }
    ]
}

Despite having done all of this, every time I try to debug my code, I get the error "The preLaunch Task 'build C program' terminated with exit code -1073741819" and if I select the option "Debug Anyway", I then get the error "launch: program 'C:\Users\iandi\Downloads\C Projects\Main.exe' does not exist".

If I compile the code manually in the MSYS2 terminal, I get no errors and the "Hello World" message is correctly printed, so the problem is limited to debugging in VS Code.

Edit notes

When trying to compile the code from the VS terminal, I get the following messages:

 *  Executing task: & 'C:\Program Files (x86)\msys64\mingw64\bin\gcc.exe' -g 'C:\Users\iandi\Downloads\C Projects\Main.c' -o 'C:\Users\iandi\Downloads\C Projects\Main.exe' 

 *  The terminal process "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -Command & 'C:\Program Files (x86)\msys64\mingw64\bin\gcc.exe' -g 'C:\Users\iandi\Downloads\C Projects\Main.c' -o 'C:\Users\iandi\Downloads\C Projects\Main.exe'" terminated with exit code: -1073741819.

 *  Terminal will be reused by tasks, press any key to close it. 

What could be causing this problem? Thanks in advance for the help!


Solution

  • After a while, I finally figured out what was wrong and wanted to share it in case anyone else runs into the same issue.

    The main problem was that I hadn't configured the VS Code terminal correctly, which caused the build process to fail and prevented the .exe file from being generated.

    Every time I opened the terminal in VS, it would close automatically. I only realized this was happening after trying to manually compile the code in VS. Once I set Git Bash as the terminal's default shell, everything started working perfectly.

    Thanks a lot for helping me figure this out in the comments.