Nix-shell下LLVM Clang交叉编译静态链接找不到llvm-libc问题
目标:搭建基于
shell.nix的LLVM交叉编译环境 需求为使用LLVM Clang搭配llvm-libc和libc++,实现从Linux到AArch64架构的C/C++交叉编译,且支持静态链接。
测试代码(hello.c)
#include <stdio.h> int main (void) { printf ("Hello, world!\n"); return 0; }
当前可正常工作的动态链接环境配置(shell.nix)
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 llvmPkgs.stdenv.mkDerivation { name = "clang-nix-shell"; nativeBuildInputs = with pkgs; [ pkg-config file ]; buildInputs = with pkgs; [ zlib # glibc.static ]; }
环境验证结果
- 执行
nix-shell --run '$CC --version',可正确显示Clang 17版本; - 执行
nix-shell --run '$CC hello.c -o hello',能成功生成AArch64架构的动态链接可执行文件。
问题:静态链接编译失败
执行静态链接编译命令:
nix-shell --run '$CC hello.c -o hello -static'
触发以下错误:
/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)
已尝试的无效方案
- 添加
glibc.static到buildInputs可解决错误,但会使用GNU libc,不符合使用llvm-libc的需求; - 尝试将以下llvmPackages子包加入
buildInputs,均无法解决问题:llvmPkgs.libcxxllvmPkgs.libcxxabillvmPkgs.libunwindllvmPkgs.stdenvllvmPkgs.libllvmllvmPkgs.libcxxStdenvllvmPkgs.libclcllvmPkgs.compiler-rt-libcllvmPkgs.bintools
疑问
请问我遗漏了什么配置?llvmPkgs.libcxx或llvmPkgs.libclc是否应该提供所需的库文件?
内容的提问来源于stack exchange,提问作者cguenther
相关产品推荐
相关产品推荐

