1

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).
stillQuestioning
  • 105
  • 3
  • 10
  • You are not running your "Custom Build" task in `launch.json`. – sweenish Aug 31 '21 at 20:54
  • [Here](https://code.visualstudio.com/docs/editor/tasks) is generic information about `tasks.json` (I literally just searched for tasks.json). Using the navigation tree on that page, I get [this](https://code.visualstudio.com/docs/cpp/config-linux) and [this](https://code.visualstudio.com/docs/cpp/cpp-debug). – sweenish Aug 31 '21 at 20:59

1 Answers1

4

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
AzuxirenLeadGuy
  • 2,470
  • 1
  • 16
  • 27
  • Thanks! This helped me greatly! My problem is a bit different than this debug case, and I'm not sure if Visual Studio can accommodate it. I need to build my changes by calling a particular program, not g++. I can do this externally, although it would be better to be able to do this in Visual Studio. With this config, it tries to build the current file, then runs the debugger, but it tries to build the current file rather than run the required build job. – stillQuestioning Sep 01 '21 at 20:15
  • @stillQuestioning In that case, you need to edit your current "preLaunchTask" attribute to your debug instructions. In your case, it is debugging the "current file" because that's what you've set in your launch.json file. Check [this StackOverflow answer](https://stackoverflow.com/a/57892509/6488350) or [this doc](https://code.visualstudio.com/Docs/editor/debugging) of VS Code for "preLanuchTask" attribute. – AzuxirenLeadGuy Sep 02 '21 at 02:58
  • Thanks again for the help! Your answer helped me get very close to the solution. I've recently updated my question again to show what I have. I'm having some trouble getting the environment variable to work with GDB and Visual Studio debugger in launch.json, but I'm pretty sure it's the last step to getting this custom debug config to work. Do you know what is causing the issue? – stillQuestioning Sep 02 '21 at 21:08
  • @stillQuestioning I see your edits, and I have edited my answer to address them. Hope this is what you need – AzuxirenLeadGuy Sep 03 '21 at 05:10