Cstdlib can't find stdlib.h (simple example!)

I asked about this topic earlier, but I didn’t have an MWE, so I think it scared everyone off. This time I’ve made it as simple as can be: a one-line C++ program that illustrates the problem.

❯ cat shell.nix
with (import <nixpkgs> {});
mkShell {
  buildInputs = [
    gtk2 gtk3 ncurses glib glibc pkg-config
  ];
}

❯ cat Accessor.cxx 
#include <cstdlib>

❯ g++ -Os -std=c++17 -pedantic -DNDEBUG -Wall -c Accessor.cxx -o Accessor.o
In file included from Accessor.cxx:1:
/nix/store/kiwdwd6zw4svi9jlr95yg1p5pgpjxn1v-gcc-11.3.0/include/c++/11.3.0/cstdlib:75:15: fatal error: stdlib.h: No such file or directory
   75 | #include_next <stdlib.h>
      |               ^~~~~~~~~~
compilation terminated.

Is there something I can add to my shell.nix so that cstdlib can find stdlib.h? Or is this an issue with cstdlib?

Just leave out glibc in buildInputs. I think what happens is that it’s already part of the compilers, so if you add it again it messes up the headers.

4 Likes

Thank you so much, that sorted it!

Thanks, I think you fixed my problem too!