I might not have described my question title properly, please edit it if needed.
I'm trying to crate a Rust interface to LXC library, which is written in C.
I have successfully called simple functions like lxc_get_version
or lxc_container_new
but I cannot get access to functions described in struct lxc_container
block.
Here is an part of my code:
#[link(name = "lxc")]
extern {
// LXC part
fn lxc_get_version() -> *const c_char;
fn lxc_container_new(name: *const c_char, configpath: *const c_char) -> LxcContainer;
// LXC container parts
fn is_defined(container: &LxcContainer) -> bool;
}
And here is an error:
note: test.o: In function `LxcContainer::is_defined::heb2f16a250ac7940Vba':
test.0.rs:(.text._ZN12LxcContainer10is_defined20heb2f16a250ac7940VbaE+0x3e): undefined reference to `is_defined'
EDIT: I have managed that functions inside C structs is called function pointers. I've tried to google something like "Rust C function pointer", but without luck.
When you see something like this (in C):
it means that struct
S
contains a field calledf
which is a pointer to a function. It does not mean that the library itself exposes a function calledf
. For example, this is valid:Here struct instance
s1
contains a pointer tosome_function_1
, ands2
contains a pointer tosome_function_2
.When you're writing FFI binding in Rust for some C library, you usually define Rust counterparts for C structures. Some tools like
rust-bindgen
can even do this automatically. In your case you will have to write something like this:That is, weird-looking C function pointer types correspond to
extern fn
function pointer types in Rust. You could also writeextern "C" fn(...) -> ...
, but"C"
qualifier is default so it is not required.You will have to write something like this to call these functions:
You need to cast a reference to a raw pointer and you also need to wrap
self.is_defined_f
in parentheses in order to disambiguate between method call and field access.You can find more on FFI in Rust here. Function pointers are explained very briefly there, though.