Can't run C++ Debugger in VSCode

I spent one day in the attempt of running a hello world cpp file with no success.

What is working

I have a task that builds main.cpp and produces a main file in the same folder which runs correctly when executed from the shell with ./main, I also tried to debug it directly with gdb which works correctly. The completion and highlighting also works after setting the proper paths.

What is not working

The problem is the Debugger, it works for other languages (python) but when I run it with this configuration it does nothing. The preLaunch task runs, creates main and then nothing. The debugger is not starting at all, no output in debug console, no output in terminal, all I see is a loading bar on the debugger for just a second. I activated all possible debugger logs but this was useless, it’s printing the configuration in my launch.json each time I run the debugger but is not printing anything useful.

Setup:

I installed vscode, gdb and gcc system-wide in my configuration.nix, I installed the extensions manually from vscode (C/C++ Extensions pack).
My main.cpp file is in the root of the project and the main binary is created in the same folder, no other files are involved to build it.

Settings

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/nix/store/75slks1wr3b3sxr5advswjzg9lvbv9jc-gcc-wrapper-12.3.0/bin/gcc",
            "compilerArgs": [],
            "cStandard": "c17",
            "cppStandard": "gnu++17",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++ build active file",
            "command": "/run/current-system/sw/bin/g++ -g ${file} -o ${fileDirname}/${fileBasenameNoExtension}",
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "Task generated by Debugger."
        }
    ],
}

launch.json

{
    "version": "0.2.0",
    "configurations": [
      {
        "name": "g++ build and debug active file",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}/${fileBasenameNoExtension}",
        "args": [],
        "stopAtEntry": true,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",

        "logging": {
          "moduleLoad": true,
          "trace": true,
          "engineLogging": true,
          "exceptions": true,
          "programOutput": true,
          "traceResponse": true,
        },
        "preLaunchTask": "g++ build active file",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ],
        "miDebuggerPath": "/nix/store/3xy76jkizzl5xvarhzdmm11r8flv5vqk-gdb-13.2/bin/gdb"
    }
    ]
  }

do you have any idea how to solve this?

2 Likes

Did you have any luck with this? Seem to have the same problem and have not been able to found out where it is failing.

I don’t know what was broken but my solution was to change the vscode package I was using.

Insead of usign vscode use vscode.fhs, you can learn more about it in the nixos wiki.

With this package you can:

  • install extension from the vscode gui as in any other operating system
  • access all the programs and libraries you need with the conventional filesystem structure of any other distro.

The last step of the wiki guide is very important, you have to specify all the packages your vscode will need or they will not be accessible from the conventional paths.

Other then vscode extensions is great also to run binaries that rely on shared libraries.

Cons:

  • I don’t know if you can specify the extension you want in your configuration if you use vscode.fhs. Anyway I have all my extensions synched so I don’t care.

  • You can’t run commands as root from vscode terminal, it’s annoying but just open another terminal app and you are fine.

Conclusion

Honestly I don’t know why this is not the first option presented in the wiki. Has some limitations but the other options are horribly broken.

2 Likes

That worked thanks. I completely ignored that section when I read through the wiki so would not have gotten there at all!

is this still a thing?
Having to do FHS for vscode is kind of lame and weird to run the debugger.

I made really good progress on this using strace

I did:

strace -f -s99999 -e trace=clone,execve code --wait .

Then I tried to click the debugger:

[pid 193326] execve("/home/fmzakari/.vscode/extensions/ms-vscode.cpptools-1.20.5-linux-x64/debugAdapters/bin/OpenDebugAD7", ["/home/fmzakari/.vscode/extensions/ms-vscode.cpptools-1.20.5-linux-x64/debugAdapters/bin/OpenDebugAD7"], 0x2aec0357bc00 /* 92 vars */) = 0
[pid 193326] +++ exited with 127 +++

It tries to run a binary OpenDebugAD7 which is not compatible with NixOS :frowning:

This needs to be rebuilt using NixOS