[英]How can I access a C global variable/constant in Rust FFI?
I need to access a value of a constant exported from C in Rust.我需要访问从 Rust 中的 C 导出的常量的值。
I want to read the value from the actual symbol, and not just copy'n'paste the value to Rust (in my case the value is a pointer, and C checks for pointer equality).我想从实际符号中读取值,而不仅仅是将值复制“n”粘贴到 Rust(在我的情况下,该值是一个指针,C 检查指针是否相等)。
extern void *magic;
What's the syntax to get magic: *const c_void
readable in Rust?获得
magic: *const c_void
的语法是什么magic: *const c_void
在 Rust 中可读?
use std::os::raw::c_void;
extern "C" {
#[no_mangle]
static magic: *const c_void;
}
Optionally, before the extern
there can be #[link(kind="static", name="<c library name>")]
to get the symbol actually linked.或者,在
extern
之前可以有#[link(kind="static", name="<c library name>")]
来获取实际链接的符号。
Externally linkable items, even if constant, need be declared with static
, not just const
keyword (otherwise you get "extern items cannot be const
").外部可链接的项目,即使是常量,也需要用
static
声明,而不仅仅是const
关键字(否则你会得到“外部项目不能是const
”)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.