问题描述
这是我第一次尝试防锈,我来自c ++背景并且正在尝试着。因此,我开始在名为.../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
(对于我的BeagleBB)进行构建
到目前为止一切顺利。现在,我想创建一个可以与其他项目共享的库。但我将在rust_test文件夹中将其创建为.../rust/rust_test/utils
:
- 使用以下内容创建了库:
cargo new --lib utils
- 我可以使用
utils
在cargo build
目录中构建我的工具,这将生成一个.rlib文件。 - 现在,我想获取我的rust_test项目以将其构建为依赖项,我发现只需要在我的rust_test .toml文件中添加
utils = { path = "utils" }
。 - 现在,我可以使用
cargo build
在rust_test文件夹中构建rust_test可执行文件和utils库
到目前为止,一切都很好。对我来说,难题的最后一部分是在utils库中使用该函数。那里有两个功能。一个叫做adder(a,b)
的人-尝试使用模板函数,而一个基本函数叫test123()
。这就是我卡住的地方。我似乎无法制定正确的语法来调用这两个函数。
这是我的主要文件:
rust_test
位置:.../rust/rust_test/
Cargo.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" }
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 = ["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]
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`.
解决方法
我认为在mod utils;
中摆脱main.rs
解决您的问题。
mod utils;
中的 main.rs
告诉编译器
utils
是应用程序的内部模块(但它是
不存在,因此不包含您所使用的功能
寻找),尽管它实际上是一箱(外部
到您的应用程序。)
模块系统有助于在箱子内部组织细节 而将箱子视为图书馆。
修改
您还应该摆脱#[cfg(utils)]
中的lib.rs
,因为
这意味着只有utils
功能已在您的Cargo.toml
文件中设置(不是
案子)。
cfg()
伪指令旨在用于条件编译。
(https://doc.rust-lang.org/reference/conditional-compilation.html)
抱歉,我忘了,mod utils {}
中的lib.rs
可能不是必需的,
否则您需要将该函数称为utils::utils::test123()
从您的应用程序。
第一个utils
是指板条箱,第二个utils
是指板条箱
这个板条箱的内部模块。