Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将basic rust程序链接到子文件夹中的rlib 这是我第一次尝试生锈,我来自C++背景并试图前进。所以我开始在一个名为../rust/_Rust_Subdirectory - Fatal编程技术网

将basic rust程序链接到子文件夹中的rlib 这是我第一次尝试生锈,我来自C++背景并试图前进。所以我开始在一个名为../rust/

将basic rust程序链接到子文件夹中的rlib 这是我第一次尝试生锈,我来自C++背景并试图前进。所以我开始在一个名为../rust/,rust,subdirectory,Rust,Subdirectory,注意:我使用此链接开始使用以下工具: 我使用:cargo new--bin rust\u test创建了默认的生锈程序。这将创建../rust/rust\u测试 我可以使用:cargo build或cargo build--target=armv7 unknown linux gnueabihf(对于我的BeagleBB) 到目前为止还不错。现在我想创建一个可以与其他项目共享的库。但是我将在rust\u test文件夹中创建它,作为../rust/rust\u test/utils: 创建

注意:我使用此链接开始使用以下工具:

  • 我使用:
    cargo new--bin rust\u test
    创建了默认的生锈程序。这将创建
    ../rust/rust\u测试
  • 我可以使用:
    cargo build
    cargo build--target=armv7 unknown linux gnueabihf
    (对于我的BeagleBB)
到目前为止还不错。现在我想创建一个可以与其他项目共享的库。但是我将在rust\u test文件夹中创建它,作为
../rust/rust\u test/utils

  • 创建库时使用:
    cargo new--lib utils
  • 我可以在
    utils
    目录中使用:
    cargo build
    构建我的utils,这将生成一个.rlib文件
  • 现在我想让我的rust_测试项目作为依赖项来构建它,我发现我只需要将:
    utils={path=“utils”}
    添加到我的rust_测试.toml文件中
  • 现在,我可以使用
    cargo build
同样,到目前为止一切都很好。对我来说,谜题的最后一部分是在我的utils库中使用该函数。这里有两个功能。一个名为
adder(a,b)
——尝试使用模板函数,一个名为
test123()
的基本函数。这就是我被卡住的地方。我似乎无法制定调用这两个函数的正确语法

以下是我的主要文件:

锈蚀试验 位置:
../rust/rust\u 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" }
[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]
乌提尔斯 位置:
../rust/rust\u test/utils/

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" }
[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]
我必须承认我真的不明白
#[cfg…]
#[…]
行的作用。但是从我读到的内容来看,我认为
modutils告诉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`.

我认为摆脱
modutilsmain.rs中的code>应该
解决你的问题

modutilsmain.rs
中的code>告诉编译器
utils
是应用程序的内部模块(但它 不存在,因此不包含您正在使用的函数 寻找),尽管它实际上是一个板条箱(外部 添加到您的应用程序中)

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


编辑

您还应该去掉
lib.rs中的
#[cfg(utils)]
,因为 这意味着只有当
utils
功能设置在
Cargo.toml
文件中(该文件不是 案件)。
cfg()
指令用于条件编译。 ()

对不起,我忘了,
lib.rs
中的
mod utils{}
可能没有必要, 或者您需要将函数称为
utils::utils::test123()
从你的申请中。 第一个
utils
指板条箱,第二个
utils
指板条箱
这个板条箱的内部模块。

我添加了
模块utils因为我已经收到一个错误。我在最后更新了问题,以显示如果我删除
mod utils,我会得到的错误。所以我想我还是缺少一些更新的工具。我已经在lib.rs中删除了mod utils中/周围的两行
[]
(并删除了
mod utils;
),现在我得到了错误:
在板条箱“utils”中找不到函数“test123”
。这是一个更近的步骤-至少它现在正试图寻找在板条箱utils。。。但是它找不到函数…啊哈这就明白了,好吧,它让我看到了下一个错误,那就是
函数test123是私有的
,但是在快速的谷歌搜索之后,我只是把
pub
放在它前面,然后我的编译工作了。非常感谢:)
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`.