代码之家  ›  专栏  ›  技术社区  ›  code_fodder

将basic rust程序链接到子文件夹中的rlib

  •  2
  • code_fodder  · 技术社区  · 5 年前

    .../rust/

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

    • cargo new --bin rust_test . 这就产生了 .../rust/rust_test .
    • cargo build cargo build --target=armv7-unknown-linux-gnueabihf (给我的小猎犬)

    到现在为止,一直都还不错。现在我想创建一个可以与其他项目共享的库。但我会在rust_test文件夹中创建它 .../rust/rust_test/utils :

    • 创建库时使用: cargo new --lib utils
    • 我可以在 utils 货物建造
    • 现在我想让我的rust_测试项目作为一个依赖项来构建它,我发现我只需要添加: utils = { path = "utils" } 我的rust_test.toml文件。
    • 货物建造

    再说一遍,到目前为止一切都很好。对我来说,难题的最后一部分是在utils库中使用函数。里面有两个功能。一个叫 adder(a,b) test123() . 这就是我被困的地方。公式化这些函数的语法似乎都不正确。

    以下是我的主要文件:

    锈蚀试验

    .../rust/rust_test/

    货物.toml

    [package]
    name = "rust_test"
    version = "0.1.0"
    authors = ["adadachanji@gmail.com <adadachanji@gmail.com>"]
    edition = "2018"
    
    # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
    
    [dependencies]
    utils = { path = "utils" }
    

    mod utils;
    
    fn main() {
        println!("Hello, world!");
    
        utils::test123();    // ??? - does not work
    }
    

    实用工具

    .../rust/rust_test/utils/

    货物.toml

    [package]
    name = "utils"
    version = "0.1.0"
    authors = ["adadachanji@gmail.com <adadachanji@gmail.com>"]
    edition = "2018"
    
    # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
    
    [dependencies]
    

    图书馆

    #[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...] #[...] 行不行。但从我所读到的我认为 mod utils;

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

    所以问题是我现在需要做什么来将我的库链接到我的应用程序,这样我就可以使用lib函数了 ?

    更新

    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`.
    
    1 回复  |  直到 5 年前
        1
  •  1
  •   prog-fh    5 年前

    我认为摆脱 mod utils; main.rs 应该 解决你的问题。

    在里面 主要单位:卢比 告诉编译器 utils 不存在,因此不包含您的函数 寻找),尽管它实际上是一个板条箱(外部 应用程序)。

    模块系统有助于整理板条箱中的细节 而板条箱被视为图书馆。


    编辑

    你也应该摆脱 #[cfg(utils)] lib.rs 因为 实用工具 在您的 Cargo.toml 案例)。 cfg() 指令用于条件编译。 ( https://doc.rust-lang.org/reference/conditional-compilation.html

    我忘了,对不起, mod utils {} 在里面 图书馆 可能没有必要, utils::utils::test123() 从你的申请。 第一个 指的是板条箱,第二个 指 这个板条箱的内部模块。

    推荐文章