gpt4 book ai didi

rust - 将包含字符串数组的 C 符号从 Rust 公开到 C

转载 作者:行者123 更新时间:2023-11-29 08:30:07 24 4
gpt4 key购买 nike

我有以下 C 代码:

const char * const Vmod_Spec[] = {
"example.hello\0Vmod_Func_example.hello\0STRING\0STRING\0",
"INIT\0Vmod_Func_example._init",
0
};

从这段代码编译出 .so 后,我可以用 dlsym 加载这个符号并获取 Vmod_Spec 的内容并迭代它。我怎样才能从 Rust 中暴露出像这样的符号来获得相同的结果?

最佳答案

Rust 的等价物是将 [*const c_char;3] 暴露为 static 值。问题是,如果您这样声明您的值,您将收到错误:error: the trait core::marker::Sync is not implemented for the type *const i8 [E0277]。你不能为 *const c_char 实现这个特性,因为你不拥有这个类型。解决方法是在 *const c_char 周围声明一个包装器类型并改为使用它:

struct Wrapper(*const c_char)
unsafe impl Sync for Wrapper { }
#[no_mangle]
pub static Vmod_Spec: [Wrapper; 3] = etc..

然后我将有一个指向值数组的 Vmod_Spec 符号。

关于rust - 将包含字符串数组的 C 符号从 Rust 公开到 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36105579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com