Tag: 生锈

将C预处理器转换为Rust

我正在将一些C代码移植到Rust,并且该代码包含很多这样的东西: #define CONFIG_FLAG_NUMBER_23 1 #define THIS 10 #define THAT 11 #define THIS_AND_THAT (THIS + THAT) #if CONFIG_FLAG_NUMBER_23 #define THIS_OR_THAT THIS #else #define THIS_OR_THAT THAT #endif #define ROOT_DIR “/root” #define FILE_ONE ROOT_DIR “/file_one” #define FILE_TWO ROOT_DIR “/file_two” 我决定删除宏并用常量表达式替换它们,但是在Rust中尝试这样做并不是很成功: static CONFIG_FLAG: bool = true; static THIS: int = 10; static THAT: int = 11; static THIS_AND_THAT: int […]

将C数组传递给Rust函数

我正在尝试制作Rust dylib并使用其他语言,如C,Python等。 我已成功调用了一个Rust函数和来自python的i32参数。 现在我正在尝试创建一个函数,它接受一个数组(指向它的指针,或者将数据集传递给Rust lib所需的任何东西)。 #![crate_type = “dylib”] #[no_mangle] pub extern fn rust_multiply(size: i32, arrayPointer: &i32) -> i32 { *(arrayPointer) } 这按预期工作。 但 #![crate_type = “dylib”] #[no_mangle] pub extern fn rust_multiply(size: i32, arrayPointer: &i32) -> i32 { *(arrayPointer + 1) // trying to get next element } 失败了 src/lib.rs:5:2: 6:2 error: type `i32` cannot be […]

在Rust中创建C函数指针的接口

我可能没有正确描述我的问题标题,如果需要请编辑它。 我正在尝试为LXC库创建一个Rust接口,这是用C语言编写的。 我已经成功调用了像lxc_get_version或lxc_container_new这样的简单函数,但我无法访问struct lxc_container块中描述的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结构中的函数称为函数指针。 我试过谷歌像“Rust C函数指针”,但没有运气。