使用cargo build
后,默认会产生.rlib
结尾的库文件。
但自己不知道怎么使用它。
查询资料可得:
--crate-type=rlib, #[crate_type = "rlib"] - A "Rust library" file will be produced. This is used as an intermediate artifact and can be thought of as a "static Rust library". These rlib files, unlike staticlib files, are interpreted by the compiler in future linkage. This essentially means that rustc will look for metadata in rlib files like it looks for metadata in dynamic libraries. This form of output is used to produce statically linked executables as well as staticlib outputs
参考网上所得,可以main.rs
里使用extern
的方式引用,再配置Cargo.toml
,如下:
[dependencies]
adder = { extern = "./libadder.rlib" }
extern crate adder;
fn main() {
println!("add:{}", adder::add(2, 2));
}
但cargo run
后,报: add not found in 'adder'
错误。
搜索一阵时间,没发现解决的方法,故来此求教,.rlib
库文件应该怎么使用它,谢谢。
评论区
写评论rustbyexample-link
rustbyexample-carte
add
函数是不是没有pub