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


#1

In this repo: https://github.com/fastgrin/croaring-rs.git

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*
./target/release/deps/libcroaring_sys-1bab9a72e22a0f17.rlib
./target/release/deps/libcroaring-56b5c0404dea2a0f.rlib

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.


#2

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;
}

#3

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:


#4

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.


#5

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?


#6

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.


#7

just remove it, then everything is OK.