Re-export C functions


#1

Linking a C library with Rust allows using it in Rust. However, I realised that when Rust links against it, it also makes the functions symbols private (pub keyword won’t help), which means if I link the Rust library with C, I cannot access the functions.

I am wondering if I can re-export C functions.


#2

Maybe this is what you want? https://doc.rust-lang.org/book/ffi.html#calling-rust-code-from-c


#3

Hi emoon,

Thanks for the info. But the document only shows how to export Rust functions to C. What I expect is to expose imported C functions back to C again.

I spent some time on this, and realised that when building Rust library into cdylib, rustc would apply some link time optimizations and remove symbols that are not used or not exposed (e.g. imported C functions are not exposed). If compiling Rust library into dylib or staticlib, all symbols stay. I am building my project into staticlib for now.

Thanks a lot.


#4

Alright. Great that you solve your issue!