Link the `rand` crate to C has missing symbols?


#1

I’m experimenting with calling Rust from C, however I’m hitting issues with linking when using the rand crate. I’m using crate-type=["staticlib"] to link to the C objects, but during the link phase I get missing symbols, possibly related to rust_core.

I’ve created a minimal example of the problem here: https://bitbucket.org/tarkasteve/rust-link-test/
On OS X it results in the follwing errors (amongst others):

Undefined symbols for architecture x86_64:
"_SecRandomCopyBytes", referenced from:
  _$LT$rand..rngs..os..OsRng$u20$as$u20$rand_core..RngCore$GT$::fill_bytes::hcd1750a2ddb4a65c in libutil_rust.a(rand-d74486f8715eee9b.rand7-b3367a8749909cd8752210e500fd6f79.rs.rcgu.o)

On Linux it looks like:

In function `std::sync::once::Once::call_once::{{closure}}':
rand13-77fb42e100814d54b0988a1a9a34ed23.rs:(.text._ZN3std4sync4once4Once9call_once28_$u7b$$u7b$closure$u7d$$u7d$17hb410b94add473a2cE+0x4c): undefined reference to `pthread_mutexattr_init'

Does anyone have any suggestions for making this work?


#2

You’ll need to link the C binary with Security.framework on OSX and libpthread on Linux.


#3

Perfect, thanks!