在Rust中创建C函数指针的接口
我可能没有正确地描述我的问题标题,如果需要,请进行编辑.
我试图把一个Rust接口加入到LXC library中,它是用C写的 我已经成功地调用了简单的函数,如lxc_get_version或lxc_container_new,但是我无法访问struct lxc_container块中描述的函数. 这是我的代码的一部分: #[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; } 这里有一个错误: note: test.o: In function `LxcContainer::is_defined::heb2f16a250ac7940Vba': test.0.rs:(.text._ZN12LxcContainer10is_defined20heb2f16a250ac7940VbaE+0x3e): undefined reference to `is_defined' 编辑:我已经管理C语言中的函数被称为函数指针.我试图google的东西像“Rust C函数指针”,但没有运气. 解决方法
当你看到这样的东西(C):
struct S { void (*f)(int,long) } 这意味着struct S包含一个称为f的字段,它是一个指向函数的指针.这并不意味着图书馆本身暴露了一个名为f的函数.例如,这是有效的: void some_function_1(int x,long y) { ... } void some_function_2(int a,long b) { ... } int main() { struct S s1; s1.f = some_function_1; struct S s2; s2.f = some_function_2; } 这里struct instance s1包含一个指向some_function_1的指针,而s2包含一个指向some_function_2的指针. 当您在Rust中为一些C库编写FFI绑定时,通常会为C结构定义Rust对应物.一些工具,如rust-bindgen甚至可以自动执行.在你的情况下,你必须写这样的东西: #[repr(C)] struct LxcContainer { name: *mut c_char,configfile: *mut c_char,// ... numthreads: c_int,// ... is_defined_f: extern fn(c: *mut LxcContainer) -> bool,state_f: extern fn(c: *mut LxcContainer) -> *const c_char,// ... } 也就是说,奇怪的C函数指针类型对应于Rust中的extern fn函数指针类型.你也可以写extern“C”fn(…) – > …,但是“C”限定符是默认值,因此不是必需的. 你将不得不写这样的东西来调用这些功能: impl LxcContainer { fn is_defined_f(&mut self) -> bool { unsafe { (self.is_defined_f)(self as *mut LxcContainer) } } } 您需要对原始指针进行引用,并且还需要在括号中包含self.is_defined_f,以消除方法调用和字段访问之间的歧义. 您可以在Rust here中的FFI上找到更多内容.尽管功能指针很简单地解释. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |