Question about `-lstdc++` and sandbox

Running nix-shell -p gcc and which gcc on my MacOS machine gives me /nix/store/kqpzwgfw1yw99536haqzg0jsa2lzxmqf-gcc-wrapper-9.3.0/bin/gcc. If I then clone GitHub - ikatyang/tree-sitter-yaml: YAML grammar for tree-sitter and run cc -o yaml.so -I./src -shared -Os -lstdc++ src/parser.c src/scanner.cc in its root, the build fails with

src/scanner.cc:2:10: fatal error: 'vector' file not found
#include <vector>
         ^~~~~~~~
1 error generated.

On my NixOS and my Arch machines, the same commands work flawlessly. The --pure flag doesn’t change anything.

The reason I’m bringing this up is because this derivation I have

  treesitterYaml = pkgs.vimUtils.buildVimPluginFrom2Nix rec {
    version = "latest";
    name = "tree-sitter-yaml-${version}";
    src = vimPluginsSources.treesitter-yaml;
    buildPhase = ''
      mkdir -p parser/
      cc -o parser/yaml.so -I./src -shared -Os -lstdc++ src/parser.c src/scanner.cc
    '';
  };

failed on my MacOS machine but works in NixOS

I know nothing about C toolchains but what surprises me here is that the same derivation works on one machine but not the other. I would have thought that buildVimPluginFrom2Nix, which just calls stdenv.mkDerivation would have the same environment on both architectures.

I can fix the issue by using ${pkgs.clang}/bin/clang++ instead of cc. Also, running cc ... outside of Nix using just what’s installed on my MacOS machine also works (some Apple clang). So… clearly the environment here affects my Nix build and I’d like to understand why.

1 Like

It Have you tried building this C++ code with g++ or c++? I guess that one should be aware of the C++ standard includes already?

Yup it works if I do ${pkgs.clang}/bin/clang++ -o yaml.so -I./src -shared -Os -lstdc++ src/parser.c src/scanner.cc

But on NixOS this also works with just cc in the derivation