0

I wish to call the various functions defined in a Rust dylib using Crystal. I've gone through the manual, but I was not able to understand it. How do I properly include and call this dylib? Am I using the CLI flags wrong?

Here's the Crystal code:

@[Link("libmy_dylib")]
lib MyDylib
  # In C: double cos(double x)
  # In Rust: print_number(x:i32) 
  fun print_number(value : Int32)
end

MyDylib.print_number(10)

I compiled a dylib using this repo. The code compiles fine to a libmy_dylib.dylib:

extern crate libc;

use libc::uint32_t;

#[no_mangle]
pub extern "C" fn hello() {
    println!("Hello from Rust!");
}

#[no_mangle]
pub extern "C" fn print_number(x: i32) {
    println!("x is: {}", x);
}

#[no_mangle]
pub extern "C" fn addition(a: uint32_t, b: uint32_t) -> uint32_t {
    let c = a + b;
    println!("Sum : {}", c);

    return a + b;
}

#[allow(dead_code)]
pub extern "C" fn fix_linking_when_not_using_stdlib() {
    panic!()
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 1
    Without an error message it's hard to help you. – Stephie Jun 12 '17 at 14:24
  • 2
    It's likely you have to specify the full path to the dylib though, unless it's in the default linker search paths already (in which case you link it as `my_dylib` not `libmy_dylib`) – Stephie Jun 12 '17 at 14:26

1 Answers1

1

You need to specify the dylib with an absolute path, and pass it through ldflags. For instance, the following Rust file

extern crate libc;

#[no_mangle]
pub extern "C" fn hello() {
    println!("Hello from Rust!")
}

that compiles to libmy_dylib.dylib can be linked like this:

@[Link(ldflags: "/absolute/path/to/libmy_dylib.dylib")]
lib MyDylib
  fun hello : Void
end

MyDylib.hello

And the program will compile to print "Hello from Rust!"

hafiz
  • 151
  • 2
  • 6