< 返回版块

linjio 发表于 2020-03-11 14:45

Tags:rlib, 库文件, extern

使用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库文件应该怎么使用它,谢谢。

评论区

写评论
rdigua 2020-03-12 09:04
rustc xxx.rs --extern adder=xxx.rlib

rustbyexample-link

rustbyexample-carte

songzhi 2020-03-11 21:26

add函数是不是没有pub

1 共 2 条评论, 1 页