在 Alpine Linux 上使用 Clang

Mar 02, 2025 • 预计阅读时间 1 分钟

Alpine Linux 的 libc 使用的是 musl ,Clang 是作为前端编译器,实际链接的时候是使用 musl 的库。

安装 Clang 和 lld:

$ apk add clang lld

编译 C 源码:

$ clang -fuse-ld=lld main.c
$ ldd a.out
    /lib/ld-musl-aarch64.so.1 (0xfff561cb0000)
    libc.musl-aarch64.so.1 => /lib/ld-musl-aarch64.so.1 (0xfff561cb0000)

可以看到依赖很干净,不像 glibc 那样。

如果想要静态链接,加上 -static 参数就行了:

$ clang -fuse-ld=lld -static main.c
$ ldd a.out
/lib/ld-musl-aarch64.so.1: a.out: Not a valid dynamic program
ClangAlpine
版权声明:如果转发请带上本文链接和注明来源。

lvv.me

iOS/macOS Developer

安装 Alpine Linux