Rust staticlib to a whole-archive shared lib?


#1

Hi, I’m trying to use musl to compile my rust library to a statically linked library.
And then converting it to a shared lib using:
musl-gcc -shared -o libexample.so -Wl,–whole-archive libexample.a

hoping to compile an executable library that can be portable on most linux platforms without glibc dependency, and let other languages call into the library.
Is this even possible?

I’m running into lots of errors when converting with multiple definition error.

i used CFLAGS=-fPIC for the musl compilation. everything else was pretty much the same as https://doc.rust-lang.org/book/advanced-linking.html#static-linking

im on:
ldd (Ubuntu GLIBC 2.23-0ubuntu3) 2.23
rustc 1.12.0-nightly (27e766d7b 2016-07-19)
cargo 0.13.0-nightly (664125b 2016-07-19)


#2

I’m not sure what you wanna do exactly, but rustc isn’t already able to produce shared and/or static libraries? (with --crate-type [bin | lib | rlib | dylib | staticlib] )


#3

Probably want cdylib actually. That’s basically designed to be used from other languages, and as long as you do it with the musl target of Rust, then it should be free of any dependence on glibc.


#4

@lazpeng
yeah, it can produce both, shared lib is executable, can be called from other languages dynamically, but it always depends on the glibc linked during compilation time. it is hard if you compile to a newer version of glibc and expect it work on older version unless you know what is oldest you gonna expect…
staticlib can’t be called from other languages dynamically, which is a requirement for me…

@retep998
cdylib is not supported for target: x86_64-unknown-linux-musl. it only supports staticlib and executable.
even it is supported, it probably will still have dynamic dependencies on musl?


#5

cdylib is not supported for target: x86_64-unknown-linux-musl. it only supports staticlib and executable.
even it is supported, it probably will still have dynamic dependencies on musl?

cdylib isn’t supported on musl targets? That seems like a bug. You should be able to create a cdylib for any target that you can create an executable. The cdylib should have dependencies linked the same way as an executable, so if an executable created for the musl target has musl statically linked in, then a cdylib will too.


#6

@retep998

not sure if it is a bug…
submitted issue: https://github.com/rust-lang/rust/issues/34987