Unable to access lib.rs file from binary - rust

I am unable to access a library from my binary.
Here is what my cargo.toml looks like
[package]
name = "app"
version = "0.1.0"
edition = "2021"
[dependencies]
<--snip-->
[lib]
path = "src/lib.rs"
[[bin]]
path = "src/main.rs"
name = "realio"
the application root
.
├── Cargo.toml
├── src
│ ├── lib.rs
│ └── main.rs
└── test
└── integration.rs`
and my main.rs
#![crate_name = "realio"]
use env_logger::Env;
use realio::run;
use std::net::TcpListener;
#[tokio::main]
async fn main() -> std::io::Result<()> {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let listener = TcpListener::bind("127.0.0.1:8000").expect("failed to bind port");
run(listener)?.await
}
However , I get the following error
error[E0432]: unresolved import `realio`
--> app/src/main.rs:4:5
|
4 | use realio::run;
| ^^^^^^^^^^^^^^^^^^^^^ use of undeclared crate or module `realio`
I would appreciate pointers on this

You're missing the name for the lib.
This would be right:
[dependencies]
[lib]
name = "realio"
path = "src/lib.rs"
[[bin]]
name = "realio"
path = "src/main.rs"
But you don't need to manually declare it there if you stick with the main.rs and lib.rs naming convention. Also keep in mind to change the Package name (line 2 in your Cargo.toml) to "realio", so your code still works.
You can find more infos for that in the Cargo Book: https://doc.rust-lang.org/cargo/guide/project-layout.html

Related

Use of undeclared crate or module with workspaces

I have the following structure:
-- project/
|
|-- Cargo.toml
|-- Cargo.lock
|-- src/
|
|-- main.rs
|-- crate1/
|-- lib.rs
|-- Cargo.toml
|-- tests
|-- Cargo.toml
|-- test.rs
and this are the content of the Cargo.toml
# Cargo.toml
...
[workspace]
members = [
"crate1",
"tests"
]
...
# crate1/Cargo.toml
[package]
name = "crate1"
...
[lib]
path = "lib.rs"
...
here I'm using another lib for my tests, I don't think the problem is here, because I already used this way to do my tests several times, and it worked perfectly, but for some reason, now this is happening to me, I don't know if everything is a typo error of my self or not, but I already checked it a lot of times
# tests/Cargo.tom
[package]
name = "tests"
version = "0.1.0"
edition = "2021"
publish = false
[dev-dependencies]
crate1 = { path = "../crate1" }
[[test]]
name = "crate1_test"
path = "crate1_test.rs"
[[test]]
name = "other_crate1_test"
path = "other_crate1_test.rs"
this is how one of the tests looks like
// tests/crate1_test.rs
use crate1::random_func;
[test]
fn random_func_test() {
assert!(random_func());
}
And for some reason cargo don't recognize the "crate1" crate and throws me this error each time I import the crate:
error[E0433]: failed to resolve: use of undeclared crate or module `crate1`
--> tests/crate1_test.rs:1:5
|
1 | use crate1::random_func;
| ^^^^^^ use of undeclared crate or module `crate1`
For more information about this error, try `rustc --explain E0433`.
error: could not compile `project-manager` due to previous error
I found the problem, it was that I didn't make the crate1 as a dependency of the main project, this is how my root Cargo.toml looks now:
[package]
name = "project"
version = "0.1.0"
edition = "2021"
[workspace]
members = [
"crate1",
"tests"
]
[dependencies]
crate1 = { path = "crate1" }
reqwest = "0.11.13"
tokio = { version = "1", features = ["full"] }
and now I can build my tests correctly
In src/main.rs you have to add mod crate1; and potentially mod tests;

Bindgen failing because of relative paths in header file

I'm trying to build out some bindings for vmaf but i've been running into some issues. The header files and the .c files in the vmaf repository live in seperate folders. I'm having an issue where the main vmaf.h file references other .h files in the same directory:
#ifndef __VMAF_H__
#define __VMAF_H__
#include <stdint.h>
#include <stdio.h>
#include "libvmaf/compute_vmaf.h"
#include "libvmaf/model.h"
#include "libvmaf/picture.h"
#include "libvmaf/feature.h"
...
this results in me getting the following build error:
vmaf/libvmaf/include/libvmaf/libvmaf.h:25:10: fatal error: 'libvmaf/compute_vmaf.h' file not found. It looks to me that rust-bindgen is looking in the current working directory for the next header file when in reality it lives in a subdirectory in my project as a git submodule pointing to the vmaf git repository
Here's the folder structure
.
├── src # lib.rs lives here
├── target
│ └── debug
└── vmaf
├── libvmaf
│ ├── doc
│ ├── include
│ │ └── libvmaf # vmaf.h lives here along with other .h files
│ ├── src # various other .h files live here
│ │ ├── arm
│ │ ├── compat
│ │ ├── ext
│ │ ├── feature
│ │ └── x86
│ ├── test
│ └── tools
and here's my build.rs
extern crate meson;
use std::env;
use std::path::PathBuf;
fn main() {
//env::set_var("RUST_BACKTRACE", "1");
let build_path = PathBuf::from(env::var("OUT_DIR").unwrap());
_ = build_path.join("build");
let build_path = build_path.to_str().unwrap();
println!("cargo:rustc-link-lib=libvmaf");
println!("cargo:rustc-link-search=native={build_path}");
meson::build("vmaf/libvmaf", build_path);
let bindings = bindgen::Builder::default()
.header("vmaf/libvmaf/include/libvmaf/libvmaf.h")
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.generate()
.expect("Unable to generate bindings");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings.write_to_file(out_path).expect("Couldn't write bindings!")
}
How can I get rust-bindgen to look in that directory rather than from my current working directory?
Should i use cargo:rustc-link-search and point it to the correct directory? Will that mess up linking to the library itself since i've already used that statement earlier to compile the meson project?
The answer ended up being the -I flag to clang arg
// Path to vendor header files
let headers_dir = PathBuf::from("vmaf/libvmaf/include");
let headers_dir_canonical = canonicalize(headers_dir).unwrap();
let include_path = headers_dir_canonical.to_str().unwrap();
// Generate bindings to libvmaf using rust-bindgen
let bindings = bindgen::Builder::default()
.header("vmaf/libvmaf/include/libvmaf/libvmaf.h")
.clang_arg(format!("-I{include_path}")) // New Stuff!
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.generate()
.expect("Unable to generate bindings");

rust how to define an internal dependency?

I am trying to achieve the following, I have this project structure:
.
├── Cargo.lock
├── Cargo.toml
└── src
├── lib.rs
├── mesh
│ ├── gltf_loader
│ │ ├── Cargo.toml
│ │ ├── pluralize
│ │ └── src
│ └── mesh.rs
└── vulkan
├── core.rs
├── hardware_interface.rs
├── image.rs
├── memory.rs
├── mod.rs
├── pipeline.rs
├── renderer.rs
├── shader_program.rs
├── swapchain.rs
└── tools.rs
in here pluralize defines a procedural macro that is needed to compile gltf_loader but that needs to be kept hidden from all other dependencies. So I want to compile gltf loader as a "subcrate" then use that crate as a dependency for mesh.rs. Ideally without moving the directory structure.
To that effect I added this to the root toml:
[dependencies]
gltf_loader = { path = "src/mesh/gltf_loader" }
However if I do this and try to run an example, I get this error:
error: failed to get `gltf_loader` as a dependency of package `vulkan_bindings v0.1.0 (/home/makogan/rust_never_engine)`
Caused by:
failed to load source for dependency `gltf_loader`
Caused by:
Unable to update /home/makogan/rust_never_engine/src/mesh/gltf_loader
Caused by:
failed to parse manifest at `/home/makogan/rust_never_engine/src/mesh/gltf_loader/Cargo.toml`
Caused by:
can't find `proc-macros` bin at `src/bin/proc-macros.rs` or `src/bin/proc-macros/main.rs`. Please specify bin.path if you want to use a non-default path.
Why does this happen?
This id the full .toml:
[package]
name = "vulkan_bindings"
version = "0.1.0"
edition = "2021"
[lib]
name = "vulkan_bindings"
path = "src/lib.rs"
[dependencies]
# Rendering
ash = { version = "0.37.0" }
glfw = { version = "0.45.0", features = ["vulkan"] }
gpu-allocator = "0.18.0"
spirv_cross = { version = "0.23.1", features = ["glsl"] }
shaderc = "0.8.0"
# Math
nalgebra = "*"
nalgebra-glm = "0.3"
nalgebra-sparse = "0.1"
# json
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
data-url = "0.1.1"
# misc
paste = "1.0.8"
termcolor = "1.1.3"
regex = "1.6.0"
add_getters_setters = "1.1.2"
# internal
gltf_loader = { path = "src/mesh/gltf_loader" }

Resolving imports in Rust

I'm having trouble with importing rand crate from crates.io. After adding the line rand="0.8.3" and then running command cargo build for the project, it keeps displaying the same errors:
error[E0432]: unresolved import `rand`
--> main.rs:1:5
|
1 | use rand::Rng;
| ^^^^ maybe a missing crate `rand`?
error[E0433]: failed to resolve: use of undeclared crate or module `rand`
--> main.rs:4:25
|
4 | let secret_number = rand::thread_rng().gen_range(1..=11);
| ^^^^ use of undeclared crate or module `rand`
error: aborting due to 2 previous errors
the cargo.toml file
[package]
name = "roller"
version = "0.1.0"
authors = ["User"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.8.3"
Basically the simplest reproducible example is this single line of code:
use rand::Rng;
fn main(){
let secret_number = rand::thread_rng().gen_range(1..=11);
print!("{}",secret_number);
}
What's wrong with it?
Just in case:
The **cargo.lock**file:
# This file is automatically #generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "getrandom"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c"
[[package]]
name = "ppv-lite86"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
[[package]]
name = "rand"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
name = "rand_chacha"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73"
dependencies = [
"rand_core",
]
[[package]]
name = "roller"
version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
In file Cargo.toml add lines:
[dependencies]
rand="0.3.14"
And rebuild project!
It worked with these versions:
rustc 1.53.0
cargo 1.53.0
Took answer from here https://github.com/rust-lang/rust/issues/34616
got the same error when I run it with vs code(rust-analyzer).
When you press the run button
rustic main.rs this command is called in terminal, it will cause error.
Type this in terminal,
cargo build
cargo run
It works well
If you think that you did everything right and get a strange unable to import error like this then you could try cargo clean.
That will allow you to fully re-build your binary/library after that.

Re-export optional cargo feature that can be turned off

With the following directory structure:
tree hello_dep
.
├── Cargo.lock
├── Cargo.toml
├── dep_a
│ ├── Cargo.toml
│ └── src
│ └── main.rs
├── dep_b
│ ├── Cargo.toml
│ └── src
│ └── main.rs
└── src
└── main.rs
And the following dependency chain: hello_dep -> dep_a -> dep_b -> (optional feature) rustc-serialize,
I would like to create a feature in dep_a that re-exports the optional rustc-serialize feature in dep_b.
At the bottom, I have dep_b, which has rustc-serialize as an optional default feature:
# dep_b/Cargo.toml
[package]
name = "dep_b"
version = "0.1.0"
[dependencies]
rustc-serialize = { version = "0.3.19", optional = true }
[features]
default = ["rustc-serialize"]
I would like to create a feature in dep_a to optionally reexport "rustc-serialize". Here is the attempt:
# dep_a/Cargo.toml
[package]
name = "dep_a"
version = "0.1.0"
[dependencies]
dep_b = { version = "0.1.0", path = "../dep_b" }
[features]
rustc-serialize = ["dep_b/rustc-serialize"]
default = ["rustc-serialize"]
However, when I try to add this as a dependency with the default off using the following Cargo.toml:
# hello_dep/Cargo.toml
[package]
name = "hello_dep"
version = "0.1.0"
[dependencies]
dep_a = { version = "0.1.0", path = "dep_a", default-features = false, optional = true }
cargo build still yields rustc-serialize in Cargo.lock. But directly depending on dep_b correctly avoids pulling in rustc-serialize with the following line
dep_b = { version = "0.1.0", path = "dep_b", default-features = false }
Is this a bug in Cargo, or am I doing something wrong? Here is a related question
In dep_a/Cargo.toml, you did not specify default-features = false on the dep_b dependency. Therefore, the rustc-serialize feature in dep_b is enabled by default. The fact that you included a feature in dep_a to enable dep_b's rustc-serialize doesn't change the fact that it's still enabled when dep_a's feature is not enabled.
Thus, in dep_a/Cargo.toml, you should have:
[dependencies]
dep_b = { version = "0.1.0", path = "../dep_b", default-features = false }

Resources