Debugging C++ with VSCode and Nix-Shell

Hello,

I’m trying to develop a c++ application on NixOS, I’ve created a nix shell script that i want to use for development, which provides me with the correct version of Ninja, CMake, GCC and GDB. Something along the lines of this snippet.

{
  pkgs ? import<nixpkgs> {}
}:

mkShellNoCC {
  packages = with pkgs; [
    cmake
    gcc13
    gdb
    ninja
  ];
}

For development I’m using VSCode with the CMake extension and Nix Environment Selector.
I can use the CMake extension without problem for building and launching my project, but as soon as i try to launch the debugger I get the following output.

[build] ninja: no work to do.
[driver] Build completed: 00:00:00.056
[build] Build finished with exit code 0
[proc] Executing command: /nix/store/ypf4h1vcw9pm93wqrfs9796mm01d59s3-gcc-wrapper-13.2.0/bin/gdb --version
[proc] The command: /nix/store/ypf4h1vcw9pm93wqrfs9796mm01d59s3-gcc-wrapper-13.2.0/bin/gdb --version exited with code: 127
[proc] Executing command: gdb --version

And then it simply stopped. When i try to run gdb --version in the nix-shell i get this output:

[nix-shell:~/my-project]$ gdb --version
GNU gdb (GDB) 13.2
Copyright (C) 2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

I also tried to launch the debugger directly with the VSCode launch files, but there it simply loads for a second, and the nothing happens.
Has anyone an idea on how to fix this?:smiley:

Using more/less that shell.nix, with a very simple C++ file:

#include <iostream>

int main() {
    int value = 10;
    std::cout << "Initial value: " << value << std::endl;
    value = 20;
    std::cout << "Updated value: " << value << std::endl;
    return 0;
}

and a simple CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
project(DebuggingExample)

set(CMAKE_CXX_STANDARD 11)

add_executable(HelloWorld main.cpp)

and VSCode with the Nix Enviroment Selector, CMake Tools, and C/C++ extensions, I was able to set a breakpoint and successfully run Debug (under Project Status, in the CMake tab).

Even doing so, the Output shown for CMake/Build was the same as what you have; so that’s not necessarily an issue:

[main] Building folder: nix-cpp-debug all
[build] Starting build
[proc] Executing command: /nix/store/0xpv46mb188z226j7cs211zmmi5xw9nj-cmake-3.29.1/bin/cmake --build /home/rgoulter/playground/nix-cpp-debug/build --config Debug --target all --
[build] ninja: no work to do.
[driver] Build completed: 00:00:00.030
[build] Build finished with exit code 0
[proc] Executing command: /nix/store/k9pxn9rdy5d2081612zx4i177kzzyvkn-gcc-wrapper-13.2.0/bin/gdb --version
[proc] The command: /nix/store/k9pxn9rdy5d2081612zx4i177kzzyvkn-gcc-wrapper-13.2.0/bin/gdb --version exited with code: 127
[proc] Executing command: gdb --version

I did this on NixOS. Are you able to debug this simple C++ project?

Hello,

thanks for your reply, unfortunally, I’m not able to debug even this simple example. When i try to launch the debugger, it simply redirects me to the Run and Debug window of VS Code but nothing more happens, I’m on NixOS 23.11 and VSCode version 1.85.2.