Go: Difference between revisions
imported>Mic92 go static linking |
imported>Mic92 |
||
| Line 21: | Line 21: | ||
== Compile go program with static compile flag == | == Compile go program with static compile flag == | ||
If <code>go build -ldflags "-s -w -linkmode external -extldflags -static"</code> fails on NixOS, with the error message <code>cannot find `-lpthread</code> and <code>cannot find -lc</code> - it is because the linker cannot find static glibc to link with. You need to have glibc.static in your environment (and have CFLAGS/LDFLAGS adjusted accordingly). | If <code>go build -ldflags "-s -w -linkmode external -extldflags -static"</code> fails on NixOS, with the error message <code>cannot find `-lpthread</code> and <code>cannot find -lc</code> - it is because the linker cannot find static glibc to link with. You need to have glibc.static in your environment (and have CFLAGS/LDFLAGS adjusted accordingly). | ||
One way to achieve this is to have something like the following as | One way to achieve this is to have something like the following as <code>shell.nix</code> and run the compilation in a nix-shell: | ||
<syntaxHighlight lang=nix> | <syntaxHighlight lang=nix> | ||
| Line 27: | Line 27: | ||
devEnv = stdenv.mkDerivation { | devEnv = stdenv.mkDerivation { | ||
name = "dev"; | name = "dev"; | ||
buildInputs = [ stdenv | buildInputs = [ stdenv go glibc.static ]; | ||
CFLAGS="-I${pkgs.glibc.dev}/include"; | CFLAGS="-I${pkgs.glibc.dev}/include"; | ||
LDFLAGS="-L${pkgs.glibc}/lib"; | LDFLAGS="-L${pkgs.glibc}/lib"; | ||