Search code examples
cdebuggingcygwinvisual-studio-code

Setting up VS Code for C using Cygwin64 Compiler and Debugger on Windows (ERROR: Unable to start debugging)


I am trying to set up VSCODE to debug a C program on Windows using Cygwin64.

I used the config suggested by @stephw ( Setting up VS Code for C using Cygwin64 Compiler and Debugger on Windows ), but it did not work for me.

I cannot comment on the original post, because I do not have enough reputation points, and I cannot answer because I do not know the answer to the original question.

The name of the script is dirigir.c and I can compile. The file dirigir.exe is created. But...

I get the following error:

ERROR: Unable to start debugging. Unexpected GDB output from command "-exec-run". Error creating process /usr/bin/E:\cloud\Backup\Trabalhos com programas\C and Cpp\scripts/e:\cloud\Backup\Trabalhos com programas\C and Cpp\scripts\dirigir.exe, (error 2).

For some reason, /usr/bin/... / is inserted in the path and the path to the .exe is duplicated.

My launch.json file is as recommended:

{
    // 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": "gcc.exe build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [
                {
                    "name": "PATH",
                    "value": "%PATH%;z:\\cygwin64\\bin"
                }
            ],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\cygwin64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "logging": { "engineLogging": true }, //optional
            "preLaunchTask": "gcc.exe build active file"
        }
    ]
}

And my tasks.json is as follows:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "gcc.exe build active file",
            "command": "C:\\cygwin64\\bin\\gcc.exe",
            "args": [
                "-g",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-Werror", // Optional
                "-Wall", // Optional
                "-Wextra", // Optional
                "-ansi", // Optional
                "-pedantic", // Optional
                "${file}"
            ],
            "options": {
                "cwd": "C:\\cygwin64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
    ]
}

Do you have any idea how to proceed? Thank you in advance.


Solution

  • Finally, I got it working.

    Based on WardenGnaw's answers in this thread: [cppdbg] Cygwin 10.1-1: intergatedTerminal unable to take input #6475 and, of course, @stephw's answer in the original question this one is based, I can debug my C program.

    First, I saved a VSCODE workspace in the same folder where my c programs are.

    Then, I used the latest Cygwin gdb version (10.2-1). Did not work (I received the error message that made ask this question). Then, I tried 9.2-1 and now it is working.

    Cygwin Setup window and the current gdb version installed.

    It is important to remember to add "C:\cygwin64\bin" to PATH.

    Environment Varibles windows

    I changed my launch.json just a little bit. Notice the "preLaunchTask" key does not have the exact same value that the "label" key in the tasks.json has:

    {
        // 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": "gcc.exe build and debug active file",
                "type": "cppdbg",
                "request": "launch",
                "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${workspaceFolder}",
                "environment": [
                    {
                        "name": "PATH",
                        "value": "%PATH%;C:\\cygwin64\\bin"
                    }
                ],
                "externalConsole": false,
                "MIMode": "gdb",
                "miDebuggerPath": "C:\\cygwin64\\bin\\gdb.exe",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ],
                "logging": { "engineLogging": true }, //optional
                "preLaunchTask": "gcc.exe build active file"
            }
        ]
    }
    

    My tasks.json is this (notice my name in the end, in the "detail" key):

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

    I notice something a little bit strange: When I press F5, it does not work, because the task created is not found.

    Dialog saying the task was not found.

    When I edited my launch.json file (preLaunchTask key) to the exact same value in the tasks.json file, the "not found" error disappears, but the debugging process does not start.

    "preLaunchTask": "C/C++: gcc.exe build active file"
    

    If I press the Run and debug play button, the debug process does not work either.

    Visual Studio Code Run and debug play button.

    When I use the Command Palette (pressing CTRL+SHIFT+P), and click C/C++: Build and Debug Active File,

    Visual Studio Code Command Palette...

    and then choose the task I created (the second one appears to be automatic), A list of the tasks available to execute.

    the program is compiled and the debug process is started.

    This is my very simple test script:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
    
        /* declare and initialize string */
        char myString[] = "This is an example of string declaration!";
    
        /* print string */
        printf("%s", myString);
    
        int numberA = 5;
        int numberB = 10;
    
        int sum = numberA + numberB;
    
        /* Printing a number */
        printf("A soma de numberA + numberB é: %d", sum);
    
        return 0;
    }
    

    I hope this question and answer can help someone in the future. Peace.