Home > Software engineering >  Configuring a custom C build and GDB debug in Visual Studio Code for Linux
Configuring a custom C build and GDB debug in Visual Studio Code for Linux

Time:09-17

I recently have been using GDB to debug a C program. For standard usage, I usually do:

$ cd compiledir
$ compilescript 
$ gdb compiled.out
  $ run inputfile

compilescript is a program that compiles the code for the particular software I am working on, and only works in compiledir. It reads an external file for compiler flags. For gdb, I include the neccisary -g flag. This works for me to debug via the text interface. However, this text interface is becoming increasingly frustrating to use compared to a IDE, and I know that Visual Studio uses gdb as a backend to debug C files by default.

To get started, I let visual studio generate default C debugging configurations and tried to change the commands, but I have no idea what it's doing, and there doesn't appear to be much documentation on making custom build/debug configurations, particularly for for the linux version of VScode.

Currently I have:

launch.json (default)

{
    // 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": "g   - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C  : g   build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "Custom Build",
            "command": "compilescript" ,
            "options": {
                "cwd": "compiledir"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compile using the custom build script."
        }
    ],
    "version": "2.0.0"
}

Running this task, and running it indirectly through the launch.json file both fail. I get the following:

> Executing task: Custom Build<

Starting build...

and it hangs there. I tried to put echo into the build command to see if it was even running, or have it create a file. However, it's like nothing is being run at all and I can't see any printing or files being created anywhere.

What is the proper way to create a custom GDB build/debug task for Visual Studio (Linux Edition)?

Update I've edited both tasks.json and launch.json, and am able to compile successfully and run GDB with the inputfile I want. However, the environment variables are not configuring properly. I need environment variables to be configured to properly run the inputfile. I currently have:

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g   - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": compiled.out,
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [{"VAR1": "VAR1_VALUE", "VAR2": "VAR2_VALUE"}],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C  : g   build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

However, when I run the build/debug task, it builds, but doesn't debug, and GDB complains of an error. It appears the "environment" variable is setting something, but I get the following error:

Unable to start debugging. Unexpected GDB output from command "-interpreter-exec console "set env"".  Argument required (environment variable and value).

CodePudding user response:

This is how you can set up debugging in C without using any Task extensions. Only the launch.json file will be needed.

First, compile your program with the debug flag: -g, and keep it anywhere in your folder.

For example in your active folder (or workspace folder which you have currently opened), Let's say only your source code is present

Program.cpp
# Other files...

Compile your program using the -g flag and create an executable as shown:

gcc -g Program.cpp -o a.out

If your compilation is successful, the executable a.out will be created in the folder, and your root folder will look like

a.out
Program.cpp
# Other files...

Next, you need to create your launch.json file. Since you are not using any Tasks extensions, you can choose the default configuration which will create the said file with most properties filled with their default values.

Now edit the program path in the launch.json to the path of this executable. I've also seen that there's a bug for some Linux OSes for VS Code's C debugger that does not work with the internal console. So you might also need to set the "external console" option as true

in this case, you'll make two edits in your launch.json file as

    "configurations": [
        {
            #.... Other stuff

            "program": "${fileDirname}/a.out",
            
            #.... Other stuff

            "externalConsole": true,

            #.... Other stuff
        }
    ]
}

That's all you need. Set breakpoints in your program, and start debugging using the button in the Debug Panel. An external console window will pop up running your program, and the breakpoints should be working.

Also if you're new to debugging in general, remember that every time you change your source code (Program.cpp in this case), you will need to compile and build your debuggable executable again. If you do not do so, your debugging would be glitchy.


Edit:

I see you want to add environment variables. I have a hunch on what you're doing wrong.

It seems to me that you want to set two variables such that

VAR1 = VAR1_VALUE
VAR2 = VAR2_VALUE

Your syntax for this in your launch.json file is incorrect. It should be as shown:

            #... Other stuff 

            "cwd": "${fileDirname}",
            "environment": 
            [
                {
                    "name": "VAR1", "value": "VAR1_VALUE"
                },
                {
                    "name": "VAR2", "value": "VAR2_VALUE"
                }
            ],
            "externalConsole": false,

            #... Other stuff
  • Related