Discussion:
[Nix-dev] How to make "gcc -static ..." work
Bjørn Forsman
2016-10-12 13:24:52 UTC
Permalink
Hi all,

On Ubuntu, I can compile static binaries with "gcc -static foo.c". But on NixOS:

$ nix-shell -p gcc
$ gcc -static foo.c
/nix/store/nm9r5lymydnsrlxjn30ym2kix6mbyr19-binutils-2.27/bin/ld:
cannot find -lc
collect2: error: ld returned 1 exit status

How to fix it?

Things I tried:

* Googling "nixpkgs static gcc". Found nothing.

* Grepping the nixpkgs tree, hoping to see stdenvStatic or something.
Found reference to dontDisableStatic for autotools packages. (Do I
have to rebuild GCC for static linking?)

* Looked in the NixOS and nixpkgs manual. Found references to static
Haskell binaries, but not for plain old C.

Best regards,
Bjørn Forsman
Игорь Пашев
2016-10-12 13:30:31 UTC
Permalink
It requires libc.a Maybe you have to add glibc into the environment.
This will work only if glibc is built with static library support, or
you are able to alter it.
Post by Bjørn Forsman
Hi all,
$ nix-shell -p gcc
$ gcc -static foo.c
cannot find -lc
collect2: error: ld returned 1 exit status
How to fix it?
* Googling "nixpkgs static gcc". Found nothing.
* Grepping the nixpkgs tree, hoping to see stdenvStatic or something.
Found reference to dontDisableStatic for autotools packages. (Do I
have to rebuild GCC for static linking?)
* Looked in the NixOS and nixpkgs manual. Found references to static
Haskell binaries, but not for plain old C.
Best regards,
Bjørn Forsman
_______________________________________________
nix-dev mailing list
http://lists.science.uu.nl/mailman/listinfo/nix-dev
Bjørn Forsman
2016-10-12 13:37:47 UTC
Permalink
Post by Игорь Пашев
It requires libc.a Maybe you have to add glibc into the environment.
Doh! Right... I thought libc was bundled with gcc, but of course, it's
in _glibc_.

Unfortunately, no celebration yet:

$ nix-shell -p gcc glibc
these paths will be fetched (9.83 MiB download, 19.98 MiB unpacked):
/nix/store/ia4g72xxpmnn9qj1v95af03ynll5addc-glibc-2.24-debug
/nix/store/wjzk9517pm6xl0pprdralf11hmxf4vkv-glibc-2.24-static
fetching path ‘/nix/store/ia4g72xxpmnn9qj1v95af03ynll5addc-glibc-2.24-debug’...
fetching path ‘/nix/store/wjzk9517pm6xl0pprdralf11hmxf4vkv-glibc-2.24-static’...

So I can see it pulling in glibc-2.24-static, which contains libc.a
(checked). Promising, but still:

$ gcc -static foo.c
/nix/store/nm9r5lymydnsrlxjn30ym2kix6mbyr19-binutils-2.27/bin/ld:
cannot find -lc
collect2: error: ld returned 1 exit status

Best regards,
Bjørn Forsman
Bjørn Forsman
2016-10-12 13:40:19 UTC
Permalink
Post by Bjørn Forsman
Post by Игорь Пашев
It requires libc.a Maybe you have to add glibc into the environment.
Doh! Right... I thought libc was bundled with gcc, but of course, it's
in _glibc_.
$ nix-shell -p gcc glibc
/nix/store/ia4g72xxpmnn9qj1v95af03ynll5addc-glibc-2.24-debug
/nix/store/wjzk9517pm6xl0pprdralf11hmxf4vkv-glibc-2.24-static
fetching path ‘/nix/store/ia4g72xxpmnn9qj1v95af03ynll5addc-glibc-2.24-debug’...
fetching path ‘/nix/store/wjzk9517pm6xl0pprdralf11hmxf4vkv-glibc-2.24-static’...
So I can see it pulling in glibc-2.24-static, which contains libc.a
$ gcc -static foo.c
cannot find -lc
collect2: error: ld returned 1 exit status
An ugly hack / workaround:

$ nix-shell -p gcc glibc
$ gcc -L/nix/store/wjzk9517pm6xl0pprdralf11hmxf4vkv-glibc-2.24-static/lib
-static

But I _really_ think nix/nixpkgs should be able to find the lib itself.

Best regards,
Bjørn Forsman
Bjørn Forsman
2016-10-12 13:45:52 UTC
Permalink
Try adding `glibc.static` to `buildInputs` (I'm not sure if `nix-shell
-p` would do the same thing, but you can try it too).
Thank you very much! It worked:

$ nix-shell -p gcc glibc.static
$ gcc -static foo.c
(success)

Best regards,
Bjørn Forsman

Continue reading on narkive:
Loading...