I’ve been trying to build Clang/LLVM for a different C standard library, namely musl. If I build Clang for my host system (x86_64 Linux, glibc), I can build a binary written in C for musl by passing --target=x86_64-unknown-linux-musl
and it works on a VM that has musl library installed. I thought I could use something similar to build Clang itself, effectively bootstrapping it, but no success.
I followed the answer from here and combined it with info from here, and this is what I ended up with:
cmake -DCMAKE_CROSSCOMPILING=True -DLLVM_DEFAULT_TARGET_TRIPLE=x86_64-unknown-linux-musl -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="clang" -DCMAKE_SYSTEM_NAME=Linux -DLLVM_HOST_TRIPLE=x86_64-unknown-linux-musl -G Ninja -DCMAKE_CXX_FLAGS='--target=x86_64-unknown-linux-musl' -DCMAKE_C_FLAGS='--target=x86_64-unknown-linux-musl' ../llvm
Before doing that, I export
-ed CC
and CXX
to point to my native clang
and clang++
binaries.
I checked the clang-19
binary via file
utility, and this is the output:
$ file bin/clang-19
bin/clang-19: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-x86_64.so.1, for GNU/Linux 3.2.0, not stripped
So far, that does look promising, but when I run ldd
, things are different:
$ ldd bin/clang-19
linux-vdso.so.1 (0x00007ffe651a4000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f0b34da8000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f0b34d89000)
libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f0b2ac00000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f0b34d69000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f0b2ae1f000)
/lib/ld-musl-x86_64.so.1 => /lib64/ld-linux-x86-64.so.2 (0x00007f0b34e9b000)
This is simply redirecting musl.
Interestingly, when I look at the “hello world” binary I successfully ran on the musl-based VM, I get the following bits:
$ file ....../musl-test/hello_dynamic
....../musl-test/hello_dynamic: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-x86_64.so.1, not stripped
$ ldd ....../musl-test/hello_dynamic
....../musl-test/hello_dynamic: error while loading shared libraries: /lib/x86_64-linux-gnu/libc.so: invalid ELF header
I don’t know honestly why ldd
would fail on this binary, presumably because musl isn’t installed on my home system, but the binary does correctly run inside the VM.
So my expectation here is that I would “cross compile” Clang (if this can even be called cross compilation, since it’s for the same arch, but different C library) for musl and I could ultimately run it on the musl-based Linux VM.
popovicu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.