我有一个包含两个文件的项目:
src/lib.rs
src/rle.rs
rle.rs
包含以下内容(以及更多):
extern crate libc;
#[derive(Debug, PartialEq)]
pub struct Rle {
pub lengths: Vec<i32>,
pub values: Vec<i32>,
}
#[no_mangle]
pub extern "C" fn rle_new(blablabla...)
lib.rs
如下所示:
mod rle;
use rle::rle_new;
// blablabla
当我在Python中加载库时,出现错误:
Traceback (most recent call last):
File "compact_ranges.py", line 19, in <module>
lib.rle_new.restype = POINTER(RleS)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 378, in __getattr__
func = self.__getitem__(name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 383, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: dlsym(0x7f94ca700370, rle_new): symbol not found
看起来 Rust 已经明白这一点了(聪明啊,太聪明了),因为我的代码检查器显示:
17 1 warning function rle_new is marked #[no_mangle], but not exported, #[warn(private_no_mangle_fns)] on by default (rust-cargo)
我该如何解决这个问题并使我的函数 rle_new
从 target/debug/libranges.dylib
文件中可用?
在我的 Cargo.toml
中,crate-type
是 ["dylib"]
lib.rs
中使用pub use rle::rle_new;
?(rle
不是pub
...) - Matthieu M.mod rle
更改为pub mod rle
,但也没有起作用。无论如何,还是谢谢 :) - The Unfun Catrle_new
函数移动到lib.rs
文件中。然后就可以工作了 :) 保持这个状态开放,以防有人有答案。 - The Unfun Catpub use
)操作,使_rle_new
在库中以导出符号的形式出现,如nm
所示。对我有用。 - Shepmaster