Lib works in mac but not in Linux, what's the possible reason?

In this repo:

I can build it successfully both in mac and Ubuntu 17.10 x86_64. In macos, I can build app which is linking this lib w/o any problem, but in Linux, I got a lot of undefined reference error on linking:

undefined reference to `_roaring_bitmap_of_ptr'
undefined reference to `_roaring_bitmap_create'
undefined reference to `_roaring_bitmap_or_inplace'

And the lib is indeed there:

$ find ./ -name libcroaring*

Could somebody help to give a hint? What's the possible reason for the problem in Linux?

I try to dump the exported function list from rlib file to check, but seems nm command doesn't work on rust lib rlib file.

I find the bindgen generated ffi always give "\u{1}" as the prefix of the link_name. What's the meaning of "\u{1}" ? perhaps this is the root cause?

For example:

extern "C" {
    /// Creates a new bitmap from a pointer of uint32_t integers
    #[link_name = "\u{1}_roaring_bitmap_of_ptr"]
    pub fn roaring_bitmap_of_ptr(n_args: usize, vals: *const u32) -> *mut roaring_bitmap_t;

Finally! find the fix solution:

It's indeed caused by this link_name, after removing all those link_name lines, everything is fine then :grin:

Its due to the different calling conventions between OSX and Linux.
On Linux the leading underscore is not needed but it is needed on OSX and Windows.

1 Like

How can one cope with this?
Would something like a #[cfg(linux)] attribute work if combined with 1 extern block for windows and OSX, and another for Linux?

Thanks for the root cause explain :+1:
I removed this line and tested, both mac and linux are OK, so I think the compiler can handle this by itself, no need to manually give this link_name.

just remove it, then everything is OK.