Reputation: 16331
This is my first attempt at rust, I am from a c++ background and trying to get going. So I started to create my project in a folder called .../rust/
Note: I used this link to get started with the tools: https://medium.com/@wizofe/cross-compiling-rust-for-arm-e-g-raspberry-pi-using-any-os-11711ebfc52b
cargo new --bin rust_test
. This creates .../rust/rust_test
.cargo build
or cargo build --target=armv7-unknown-linux-gnueabihf
(for my BeagleBB)So far so good. Now I want to create a library that I can share with other projects. But I will create it inside the rust_test folder as .../rust/rust_test/utils
:
cargo new --lib utils
utils
dir with: cargo build
, this generates a .rlib file.utils = { path = "utils" }
to my rust_test .toml file.cargo build
Again, all good so far. The final part of the puzzle for me is to use the function within my utils library. There are two functions in there. One called adder(a,b)
- an attempt at a template function, and a basic function called test123()
. This is where I have got stuck. I can't seem to formulate the correct syntax to call either of these functions.
Here are my main files:
location: .../rust/rust_test/
Cargo.toml
[package]
name = "rust_test"
version = "0.1.0"
authors = ["[email protected] <[email protected]>"]
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
}
location: .../rust/rust_test/utils/
Cargo.toml
[package]
name = "utils"
version = "0.1.0"
authors = ["[email protected] <[email protected]>"]
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`.
I have to admit I don't really understand what the #[cfg...]
and #[...]
lines do. But from what I have read I thought the mod utils;
in main.rs tells the rust compiler/linker to look else where for the test123() function.
Maybe I have not even linked the files at all yet - I have only built them?
So the question is what do I need to do now to link my library to my application so I can use the lib function test123()
?
update
if I remove mod utils;
I get the error:
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`.
Upvotes: 3
Views: 1364
Reputation: 16805
I think that getting rid of mod utils;
in main.rs
should
solve your problem.
mod utils;
in main.rs
tells the compiler that
utils
is an inner module of your application (but it
does not exist so does not contain the functions you are
looking for), although it is actually a crate (external
to your application).
The module system helps organise the details inside a crate while a crate is seen as a library.
edit
You should also get rid of #[cfg(utils)]
in lib.rs
because
it means that the following item exists only if the utils
feature is set in your Cargo.toml
file (which is not
the case).
The cfg()
directives are intended for conditional compilation.
( https://doc.rust-lang.org/reference/conditional-compilation.html )
And sorry, I forgot, mod utils {}
in lib.rs
may not be necessary,
or you will need to refer to the function as utils::utils::test123()
from your application.
The first utils
refers to the crate, the second utils
refers to
an inner module of this crate.
Upvotes: 1