linuxclangaddress-sanitizer

clang -fsanitize=address can't find libclang_rt.asan


It was my first time building clang from source, so maybe I messed it up, but if I try to compile a "hello world" with -fsanitize=address, I get an error:

$ clang -fsanitize=address hello.c 
/usr/bin/ld: cannot find /usr/local/lib/clang/15.0.7/lib/linux/libclang_rt.asan_static-x86_64.a: No such file or directory
/usr/bin/ld: cannot find /usr/local/lib/clang/15.0.7/lib/linux/libclang_rt.asan-x86_64.a: No such file or directory
clang-15: error: linker command failed with exit code 1 (use -v to see invocation)

In fact, /usr/local/lib/clang/15.0.7/lib doesn't exist at all (only include/ there). Did I build clang incorrectly? These are the commands I used, on Debian Stable:

wget "https://github.com/llvm/llvm-project/releases/download/llvmorg-15.0.7/llvm-project-15.0.7.src.tar.xz"
tar xvf llvm-project-15.0.7.src.tar.xz
cd llvm-project-15.0.7.src
cmake -S llvm -B build -G Ninja -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" -DCMAKE_BUILD_TYPE=Release
ninja -C build check-llvm
sudo ninja -C build install

Solution

  • The comments helped. I'm going to leave an answer in case someone else has this problem and finds this post by googling.

    The solution is to add -DLLVM_ENABLE_RUNTIMES=compiler-rt to the cmake command. For example, the complete build instructions for version 19.1.0 can be:

    export CC=/usr/local/bin/gcc
    export CXX=/usr/local/bin/g++
    export LD_LIBRARY_PATH=/usr/local/lib64
    
    tar xf llvm-project-19.1.0.src.tar.xz
    cd llvm-project-19.1.0.src
    
    cmake -S llvm -B build -G Ninja -DLLVM_BINUTILS_INCDIR=/usr/include -DLLVM_ENABLE_PLUGINS=ON -DLLVM_ENABLE_BINDINGS=OFF -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;lld" -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_RUNTIMES=compiler-rt
    
    ninja -C build check-llvm
    ninja -C build install