Can rust -musl static library linked with -gnu executable?

I am using Rust to write infrastructures of my team, which includes some executables and some static libraries for others to use. Other members in my team do not use Rust, and develop with different Linux versions. So for better compatibility, I prefer to compile executables in my Rust workspace with x86_64-unknown-linux-musl target since it is statically linked and can be distributed to others with no dependencies.

However, I also need to distribute the static libraries (with FFIs I have written) for others to use. Since cargo does not support per-package target configurations, the whole packages in my workspaces are either be compiled with x86_64-unknown-linux-musl or x86_64-unknown-linux-gnu. In my understanding, the static library itself does not contain any actual codes of libc, and the musl and glibc have same interface, so does this mean we can link a Rust static library (which is compiled with x86_64-unknown-linux-musl) in a C program compiled with glibc (which is the default libc implementation in most Linux distributions)?

They do not. Each libc has their own ABI. Some functions will overlap between libc implementations, but especially structs have different layouts between libc implementations. This includes between glibc and musl. You can't compile static libraries for one libc against another libc.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.