Tag: rust

创建链接到Rust dylib的共享C对象以在R中使用

我正在尝试创建一个可以加载到R中的共享对象,它通过R的C API调用Rust函数。 要从C调用Rust,我正在关注此博文 。 当我尝试创建共享库并链接到Rust库时,我的问题出现了。 链接器抱怨它无法找到我的Rust函数。 我对编译语言很陌生,在转向SO之前已经花了几天的努力。 在那段时间里,我学到了很多关于编译器标志的知识,但却没有更接近解决方案。 我认为这可能是显而易见的事情。 我的C ++代码: #include “Rinternals.h” #include “Rh” #include “treble.h” // test.cpp extern “C” { SEXP triple(SEXP val) { int32_t ival = *INTEGER(val); Rprintf(“9 tripled is %d\n”, treble(ival)); return R_NilValue; } } treble.h: #include int32_t treble(int32_t value); 我的Rust代码: #![crate_type = “dylib”] #[no_mangle] pub extern fn treble(value: i32) -> […]

如何检查从C传递的函数指针是否为非NULL

示例代码如下 Rust部分: #[no_mangle] pub extern fn call_c_function(value: i32, fun: fn(i32) -> i32) -> i32 { fun(value) } 而C部分: int32_t call_c_function(int32_t value, int32_t (*fun)(int32_t)); int32_t triple(int32_t x) { return x*3; } int main(int argc, char *argv[]) { int32_t value = 3; int32_t result = call_c_function(value, triple); printf(“%d tripled is %d\n”, value, result); call_c_function(0, NULL); // Crash […]

在Rust中初始化sigset_t

我正在尝试更多地了解Rust中的FFI以及与C库(特别是libc )的链接。 在我的“任务”中,我遇到了以下问题。 C中的正常模式 void(* sig_set(int sig, void(*handler)(int))) { // uninitialized sigaction structs struct sigaction new_action, old_action; // assign options to new action new_action.sa_flags = SA_RESTART; new_action.sa_handler = handler; sigemptyset(&new_action.sa_mask); if(sigaction(sig, &new_action, &old_action) < 0) { fprintf(stderr, "Error: %s!\n", "signal error"); exit(1); } return old_action.sa_handler; } 在Rust中尝试 fn sig_init(sig: i32, handler: fn(i32)->()) -> usize { […]

Rust FFI将trait对象作为上下文传递给调用回调

好的,我正在努力实现以下目标: C召唤生锈 rust调用回c并在用户定义的trait对象上注册回调 c因上下文而生锈 rust调用上下文的回调(trait对象) 我一直在玩它很多。 我走得很远,但还是不太好。 C位: #include #include void *global_ctx; void c_function(void* ctx) { printf(“Called c_function\n”); global_ctx = ctx; } int main(void) { void *thing = dlopen(“thing/target/debug/libthing.dylib”, RTLD_NOW | RTLD_GLOBAL); if (!thing) { printf(“error: %s\n”, dlerror()); return 1; } void (*rust_function)(void) = dlsym(thing, “rust_function”); void (*rust_cb)(void*) = dlsym(thing, “rust_cb”); printf(“rust_function = %p\n”, rust_function); […]

是否可以在运行时生成并执行Rust代码?

在运行时使用C,我可以: 创建函数的源代码, 调用gcc将其编译为.so(Linux)(或使用llvm等), 加载.so,和 调用该function。 在Rust中类似的东西可能吗? 特别是我想使用代数数据类型,因此使用Rust的Cfunction子集是不够的。

使用数组参数从C调用Rust方法

我正在尝试从我的C项目中为嵌入式设备调用Rust代码。 设备在UART上打印,因此我可以看到通话结果是什么。 下面的C和Rust代码按预期工作(我省略了许多使编译时需要的样板代码)。 C: uint8_t input[] = {1,2,3}; uint8_t output[] = {4,5,6}; output = func(input, output); printf(“Sum: %d”, output[0]); 锈: #[no_mangle] pub extern fn func(input: &[u8], dst: &mut[u8]) -> u8 { 3 } 这按预期打印3。 但我坚持改变作为参考传入的数组: C: uint8_t input[] = {1,2,3}; uint8_t output[] = {4,5,6}; func(input, output); printf(“Sum: %d”, output[0]); 锈: #[no_mangle] pub extern fn func(input: […]

创建包含字符串的静态C结构

我正在尝试在Rust中创建一个动态库,它导出一个结构作为符号,将通过dlopen()加载到C程序中。 但是,当我访问结构中的第二个字符串时,我遇到了一些段错误,所以我做了一个小测试程序,试着弄清楚我做错了什么。 这是Rust代码(test.rs),使用“rustc –crate-type dylib test.rs”编译: #[repr(C)] pub struct PluginDesc { name: &’static str, version: &’static str, description: &’static str } #[no_mangle] pub static PLUGIN_DESC: PluginDesc = PluginDesc { name: “Test Plugin\0”, version: “1.0\0”, description: “Test Rust Plugin\0” }; 这是尝试加载库(test.c)的C程序,使用“gcc test.c -ldl -o test”编译: #include #include typedef struct { const char *name; const char *version; […]