【问题标题】:Link basic rust program to rlib in a subfolder将基本 rust 程序链接到子文件夹中的 rlib
【发布时间】:2020-12-18 18:29:18
【问题描述】:

这是我第一次尝试 rust,我来自 c++ 背景并试图开始。所以我开始在一个名为.../rust/的文件夹中创建我的项目

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

  • 我创建了默认的 rust 程序,使用:cargo new --bin rust_test。这将创建 .../rust/rust_test
  • 我可以使用:cargo buildcargo build --target=armv7-unknown-linux-gnueabihf(对于我的 BeagleBB)进行构建

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

  • 创建库时使用:cargo new --lib utils
  • 我可以使用cargo buildutils 目录中构建我的实用程序,这会生成一个.rlib 文件。
  • 现在我想让我的 rust_test 项目将其构建为依赖项,我发现我只需要在我的 rust_test .toml 文件中添加:utils = { path = "utils" }
  • 现在我可以使用cargo build 在 rust_test 文件夹中构建我的 rust_test 可执行文件和我的 utils lib

再一次,到目前为止一切都很好。对我来说,难题的最后一部分是在我的 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" }

ma​​in.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 编译器/链接器在其他地方寻找 test123() 函数。

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

所以问题是我现在需要做什么才能将我的库链接到我的应用程序,以便我可以使用 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`.

【问题讨论】:

    标签: rust subdirectory


    【解决方案1】:

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

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

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


    编辑

    您还应该在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 指的是 这个箱子的内部模块。

    【讨论】:

    • 我添加了mod utils;,因为我已经遇到了错误。我在最后更新了问题,以显示删除mod utils; 时出现的错误。所以我想我仍然缺少一些东西
    • 感谢您的更新。我已经删除了 lib.rs 中我的 mod utils 中/周围的两条 #[] 行(并删除了 mod utils;)现在我收到错误:cannot find function 'test123' in crate 'utils'。这更近了一步 - 至少它现在正在尝试查看 crate utils...但它没有找到该功能...
    • 啊哈! - 明白了,它让我遇到了下一个错误,即function test123 is private,但在快速谷歌之后,我只是把pub放在它前面,然后我的编译工作。非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 2012-10-09
    • 2015-09-10
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多