NixOSでプログラムを静的にコンパイルする方法は?

NixOSでプログラムを静的にコンパイルする方法は?

単純なプログラムを静的実行可能ファイルにコンパイルしようとしています。

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

ただし、次のエラーが発生します。

$ 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

これは、 nixpkgs に static libc が別々に提供されるためです。この試み:

$ 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

もちろん、静的ライブラリが頻繁に必要な場合は、このパッケージを設定に追加できます。

おすすめ記事