my goal is to setup an shell.nix for C and C++ cross-compiling using llvm's clang and it's corresponding libc and libc++ for static linking.
With following hello.c
#include <stdio.h>
int main (void)
{
printf ("Hello, world!\n");
return 0;
}
I try to have a nix-shell setup for cross-compile from linux to aarch64 using llvm clang compiler stack in static linkage mode, using llvm's libc.
Therefore i was able to setup an working environment (at least for shared linking) with the shell.nix:
# https://nix.dev/tutorials/cross-compilation.html#developer-environment-with-a-cross-compiler
let
nixpkgs = fetchTarball "https://nixos.org/channels/nixos-unstable/nixexprs.tar.xz";
pkgs = (import nixpkgs {}).pkgsCross.aarch64-multiplatform;
llvmPkgs = pkgs.llvmPackages_17;
stdenv = llvmPkgs.stdenv;
mkShell = pkgs.mkShell.override { inherit stdenv; };
in
#https://nixos.wiki/wiki/Using_Clang_instead_of_GCC
llvmPkgs.stdenv.mkDerivation {
name = "clang-nix-shell";
nativeBuildInputs = with pkgs; [
pkg-config
file
];
buildInputs = with pkgs; [
zlib
# glibc.static
];
}
nix-shell --run '$CC --version' correctly reports to see a clang v17
nix-shell --run '$CC hello.c -o hello' correctly compiles to an aarch64 dynamically linked file
Problem:
When i now go for static linkage with:
nix-shell --run '$CC hello.c -o hello -static'
i get the error that libc "-lc" is not found:
/nix/store/kvl0lyl8b4in5222x5gc3v33pkhzd76m-aarch64-unknown-linux-gnu-binutils-2.40/bin/aarch64-unknown-linux-gnu-ld: cannot find -lc: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I can fix it by adding glibc.static to buildInputs, but in my eyes is this not correct, since it uses than the gnu-libc instead of llvm's libc. I tried in contrast to add all llvmPackages sub-packages to buildInputs:
llvmPkgs.libcxx
llvmPkgs.libcxxabi
llvmPkgs.libunwind
llvmPkgs.stdenv
llvmPkgs.libllvm
llvmPkgs.libcxxStdenv
llvmPkgs.libcxxStdenv
llvmPkgs.libclc
llvmPkgs.compiler-rt-libc
llvmPkgs.bintools
but non of them did fix it.
What am i missing here? Shouldn't exactly llvmPkgs.libcxx or llvmPkgs.libclc exactly provide the necessary library here?