2

I'm trying to compile a simple program into a static executable:

$ cat hello.c
#include <stdio.h>
int main() {
    puts("Hello, world!");
}

However, I'm met with the following error:

$ gcc -static hello.c -o hello
/nix/store/p792j5f44l3f0xi7ai5jllwnxqwnka88-binutils-2.31.1/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status
1
  • I don't have a proper answer, but for fully static builds I'd suggest to look into our pkgsStatic attribute. Many packages already just work, simply by building e.g. the pkgsStatic.hello attribute. Commented Oct 21, 2020 at 19:20

1 Answer 1

3

This is because the static libc is provided separately in nixpkgs. Try this:

$ nix-shell -p gcc glibc.static
these paths will be fetched (1.37 MiB download, 9.12 MiB unpacked):
  /nix/store/7q44r8ps2yv9zr1bxhff49xb6hh3xrnn-glibc-2.31-static
copying path '/nix/store/7q44r8ps2yv9zr1bxhff49xb6hh3xrnn-glibc-2.31-static' from 'https://cache.nixos.org'...

[nix-shell:~]$ gcc -static hello.c -o hello

Of course, if you often find yourself needing the static libraries, you can add this package to your configuration.

1
  • 1
    AFAIK glibc doesn't fully support getting linked statically (i.e. it's a little buggy or something), but that's not specific to Nix(OS)... Commented Oct 21, 2020 at 19:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .