我正在尝试实现一个 malloc 类型的函数,但我不知道该用什么来代替 unistd.h 中为 C 找到的 sbrk 函数。有没有办法将 FFI unistd.h 转换为 Rust 程序?
最佳答案
The Rust Programming Language预订有关 FFI 的一些好信息。如果你使用 libc
, 和 cargo你可以使用类似下面的东西。
extern crate libc;
use libc;
extern {
fn sbrk(x: usize) -> *mut libc::c_void;
}
fn call_sbrk(x: usize) -> *mut libc::c_void {
unsafe {
sbrk(x)
}
}
fn main() {
let x = call_sbrk(42);
println!("{:p}", x);
}
在您的 Cargo.toml
中使用类似以下的内容
[dependencies]
libc = "^0.2.7"
关于rust - 有没有办法在 Rust 代码中使用 unistd.h 中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35582527/