How to get clang to pick up sanitizers installed in non-standard location

Hello everyone,

I’m currently trying to build (and use) libc++ in various configurations (with enabled tsan, with enabled msan, with enabled debug mode, etc.). Since some of them have ABI implications and compiler-r/the sanitizers depend on libc++ I assumed it is best to build all of libunwind, libc++abi, libc++ and compiler-rt with the same configuration to avoid ABI issues.

I’ve thus first built clang, lld and the compiler-rt builtins + crt (no runtimes) as a bootstrap build and installed them to /opt/clang. I then built libunwind, libc++abi, libc++ and the sanitizers with the clang from /opt/clang and installed them to a subdirectory of /opt/clang-runtimes/ (e.g. /opt/clang-runtimes/debug, /opt/clang-runtimes/tsan, …)

I’m now able to compile, link and run a test executable with the correct version of libc++ by using the flags described at Using libc++ — libc++ documentation. However, once I add any of the sanitizer flags (e.g. -fsanitize=address), clang does complain that it can’t find the sanitizer libs:

/opt/clang/bin/clang++ -nostdinc++ -isystem /opt/clang-runtimes/debug/include/c++/v1   -m64 -stdlib=libc++ -O3 -DNDEBUG -m64 -fuse-ld=lld  -nostdlib++ -L /opt/clang-runtimes/debug/lib -L/opt/clang-runtimes/debug/lib/linux -Wl,-rpath,/opt/clang-runtimes/debug/lib -lc++ -unwindlib=libunwind -fuse-ld=lld  -fsanitize=address CMakeFiles/test.dir/src/main.cpp.o -o test:
clang-15: warning: argument unused during compilation: '-nostdinc++' [-Wunused-command-line-argument]
ld.lld: error: cannot open /opt/clang/lib/clang/15.0.7/lib/linux/libclang_rt.asan_static-x86_64.a: No such file or directory
ld.lld: error: cannot open /opt/clang/lib/clang/15.0.7/lib/linux/libclang_rt.asan-x86_64.a: No such file or directory
ld.lld: error: cannot open /opt/clang/lib/clang/15.0.7/lib/linux/libclang_rt.asan_cxx-x86_64.a: No such file or directory
clang-15: error: linker command failed with exit code 1 (use -v to see invocation)

This obviously fails because the sanitizers aren’t installed in that directory. I had hoped that -L/opt/clang-runtimes/debug/lib/linux would be sufficient for clang to find the libs, but apparently it uses some hard-coded absolute path. Is there a way to tell clang to look for the sanitizer libs in /opt/clang-runtimes/debug/lib/linux instead?

Thanks for your help

Manuel

I think your options are to either link the sanitizer libraries manually, or pass the -resource-dir argument to the compiler to make it look for its libraries there. The complication with the latter is that everything else Clang wants in the resource directory (e.g. the intrinsic headers and the builtins) will also need to be present there, so you might need to rework your layout.

1 Like

Thank you very much. This works for me. I’m happy I don’t have to have a separate copy of clang for every version of libc++ :slight_smile:

1 Like