You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nix环境下32位Clang-10+ASAN编译缺失libclang_rt.lsan-i386.a的解决咨询

解决Nix下32位Clang-10链接LSAN库缺失的问题

你的问题核心是Nix提供的pkgsi686Linux.clang_10默认未包含LeakSanitizer(LSAN)的32位静态运行时库,需要手动补充对应的compiler-rt包并配置clang的资源路径。

以下是两种可行的解决方法:

方法一:配置CLANG_RESOURCE_ROOT指向compiler-rt库目录

修改你的shell.nix,添加pkgsi686Linux.compiler-rt包,并通过环境变量让clang找到正确的runtime库:

{ pkgs ? import <nixpkgs> {} }:

pkgs.pkgsi686Linux.mkShell {
  nativeBuildInputs = [
    pkgs.pkgsi686Linux.clang_10
    pkgs.pkgsi686Linux.compiler-rt
  ];

  shellHook = ''
    # 指定clang的资源根目录为compiler-rt提供的clang runtime库路径
    export CLANG_RESOURCE_ROOT="${pkgs.pkgsi686Linux.compiler-rt}/lib/clang/10.0.1"
  '';
}

保存后重新进入nix shell,原编译命令clang a.c -m32 -fsanitize=leak即可正常链接。

方法二:手动添加链接路径参数

如果方法一不生效,可以直接在编译命令中指定LSAN库的路径:

  1. 先确保compiler-rt已加入依赖:
{ pkgs ? import <nixpkgs> {} }:

pkgs.pkgsi686Linux.mkShell {
  nativeBuildInputs = [
    pkgs.pkgsi686Linux.clang_10
    pkgs.pkgsi686Linux.compiler-rt
  ];
}
  1. 编译时显式指定库路径和链接库:
clang a.c -m32 -fsanitize=leak -L$(nix-build '<nixpkgs>' -A pkgsi686Linux.compiler-rt --no-out-link)/lib/clang/10.0.1/lib/linux -lclang_rt.lsan-i386

或者在shellHook中预设CFLAGS,简化编译命令:

shellHook = ''
  export CFLAGS="-L${pkgs.pkgsi686Linux.compiler-rt}/lib/clang/10.0.1/lib/linux"
'';

验证库是否存在

可以通过以下命令确认compiler-rt包包含目标库:

nix-build '<nixpkgs>' -A pkgsi686Linux.compiler-rt && ls ./result/lib/clang/10.0.1/lib/linux/libclang_rt.lsan-i386.a

如果输出该文件路径,则说明库存在,配置后即可正常链接。

内容的提问来源于stack exchange,提问作者Michael

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 13:30:52