Link 基本 rust 程序到子文件夹中的 rlib

Link basic rust program to rlib in a subfolder

这是我对 Rust 的第一次尝试,我有 C++ 背景,正在尝试开始。所以我开始在一个名为 .../rust/

的文件夹中创建我的项目

注意:我使用这个link开始使用工具:https://medium.com/@wizofe/cross-compiling-rust-for-arm-e-g-raspberry-pi-using-any-os-11711ebfc52b

到目前为止一切顺利。现在我想创建一个可以与其他项目共享的库。但我会在 rust_test 文件夹中创建它,格式为 .../rust/rust_test/utils:

同样,到目前为止一切都很好。对我来说,难题的最后一部分是使用我的 utils 库中的函数。里面有两个函数。一个叫做 adder(a,b) - 对模板函数的尝试,还有一个叫做 test123() 的基本函数。这就是我卡住的地方。我似乎无法制定正确的语法来调用这些函数中的任何一个。

这是我的主要文件:

rust_test

位置:.../rust/rust_test/

Cargo.toml

[package]
name = "rust_test"
version = "0.1.0"
authors = ["test@gmail.com <test@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
utils = { path = "utils" }

main.rs

mod utils;

fn main() {
    println!("Hello, world!");

    utils::test123();    // ??? - does not work
}

工具

位置:.../rust/rust_test/utils/

Cargo.toml

[package]
name = "utils"
version = "0.1.0"
authors = ["test@gmail.com <test@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

lib.rs

#[cfg(test)]
mod tests {
    #[test]
    fn adder<T>(a: T, b: T) -> T {
        return a + b;
    }
}

#[cfg(utils)]
mod utils {
    #[utils]

    fn test123() {
        println!("test!");
    }
}

输出

~/bbb/development/rust/rust_test$ cargo build
   Compiling rust_test v0.1.0 (/home/user/bbb/development/rust_test)
error[E0583]: file not found for module `utils`
 --> src/main.rs:1:1
  |
1 | mod utils;
  | ^^^^^^^^^^
  |
  = help: to create the module `utils`, create file "src/utils.rs"

error[E0425]: cannot find function `test123` in module `utils`
 --> src/main.rs:6:12
  |
6 |     utils::test123();    // ??? - does not work
  |            ^^^^^^^ not found in `utils`

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0425, E0583.
For more information about an error, try `rustc --explain E0425`.
error: could not compile `rust_test`.

我不得不承认我不太明白 #[cfg...]#[...] 行的作用。但是根据我的阅读,我认为 main.rs 中的 mod utils; 告诉 rust compiler/linker 在其他地方寻找 test123() 函数。

也许我什至还没有 link 编辑这些文件 - 我只是构建了它们?

所以问题是我现在需要做什么才能 link 我的库到我的应用程序以便我可以使用 lib 函数 test123()?

更新

如果我删除 mod utils; 我会收到错误消息:

user@user-VirtualBox:~/bbb/development/rust/rust_test$ cargo build
   Compiling rust_test v0.1.0 (/home/user/bbb/development/rust_test)
error[E0425]: cannot find function `test123` in crate `utils`
 --> src/main.rs:4:12
  |
4 |     utils::test123();    // ??? - does not work
  |            ^^^^^^^ not found in `utils`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0425`.
error: could not compile `rust_test`.

我认为在 main.rs 中删除 mod utils; 应该 解决你的问题。

mod utils; in main.rs 告诉编译器 utils 是您应用程序的内部模块(但它 不存在所以不包含你的功能 寻找),虽然它实际上是一个板条箱(外部 到您的应用程序)。

模块系统有助于组织板条箱内的细节 而板条箱被视为图书馆。


编辑

你也应该去掉 lib.rs 中的 #[cfg(utils)] 因为 这意味着仅当 utils 时才存在以下项目 功能设置在您的 Cargo.toml 文件中(不是 案子)。 cfg() 指令用于条件编译。 ( https://doc.rust-lang.org/reference/conditional-compilation.html )

抱歉,我忘了,lib.rs 中的 mod utils {} 可能没有必要, 或者您需要将该函数称为 utils::utils::test123() 从你的应用程序。 第一个 utils 指的是板条箱,第二个 utils 指的是 这个板条箱的内部模块。