r/learnrust • • 13d ago

No `main` function found in crate for library crate

I use RustRover IDEA. And the IDEA prompt me an error as No 'main' function found in crate

rust compiler seems treat the library crate as binary crate. And when I add

[lib]
path = "lib.rs"
crate-type = ["cdylib"]

I have try both path = "lib.rs" and path = "src/lib.rs". path = "lib.rs" works but cannot load other module like mod1.rs and module/mod.rs in the same directory as lib.rs. And after I remove the [lib]. No 'main'function found error return.

I try `cargo test' and the error message shows

error[E0583]: file not found for module `mod1`
 --> src/lib.rs:5:1
  |
5 | mod mod1;
  | ^^^^^^^^^^^^^
  |
  = help: to create the module `mod1`, create file "src/lib/mod1.rs" or "src/lib/mod1/mod.rs"
  = note: if there is a `mod mod1` elsewhere in the crate already, import it with `use crate::...` instead

error[E0583]: file not found for module `mod2`
 --> src/lib.rs:6:1
  |
6 | mod mod2;
  | ^^^^^^^^^^^
  |
  = help: to create the module `mod2`, create file "src/lib/mod2.rs" or "src/lib/mod2/mod.rs"
  = note: if there is a `mod mod2` elsewhere in the crate already, import it with `use crate::...` instead

error[E0583]: file not found for module `module`
 --> src/lib.rs:7:1
  |
7 | mod module;
  | ^^^^^^^^^^
  |
  = help: to create the module `module`, create file "src/lib/module.rs" or "src/lib/module/mod.rs"
  = note: if there is a `mod module` elsewhere in the crate already, import it with `use crate::...` instead

error[E0425]: cannot find function `my_func` in the crate root
   --> src/lib.rs:137:16
    |
137 |         crate::my_func(
    |                ^^^^^^^^^^^^^^^^^^^ not found in the crate root
    |
help: consider importing this function
    |
134 +     use crate::lib::my_func;
    |
help: if you import `my_func`, refer to it directly
    |
137 -         crate::my_func(
137 +         my_func(
    |

error: could not compile `<crate-name>` (bin "crate-name>" test) due to 4 previous errors; 5 warnings emitted
2 Upvotes

7 comments sorted by

6

u/kmdreko 13d ago

This suspiciously looks like you have mod lib; somewhere

1

u/Ayano-Keiko 13d ago

Thanks. Previous I cannot load function in library crate and I add mod lib in "main.rs". But now I cannot load crate_name::function with error cannot find module or crate<create-name>in this scope [E0433] when I add

#[lib]
#crate-type = ['cdylib']

6

u/kmdreko 13d ago • Bravo

Do not use mod lib; from main.rs. Instead, use your_crate_name::...; from main.rs to use the library.

Edit: oh, you also need to allow normal Rust libraries to be compiled in order to use the library from main.rs

crate-type = ['rlib', 'cdylib']

2

u/Ayano-Keiko 13d ago

Thanks very much. fixed

1

u/MultipleAnimals 13d ago

sounds like your folder structure is messed up? It should look something like this:

mycrate/  
    Cargo.toml  
    src/  
        lib.rs // mod mod1; here  
        mod1.rs

1

u/alietors 13d ago

Do you have a main function? Do you have those modules the compiler is flagging?

1

u/Technical_Radish7139 13d ago

set crate-type to rlib or leave it out. cdylib is for dynamic libraries and messes up module resolution in tests. Use the default 2018+ path rules where mod1.rs sits next to lib.rs inside src, do not put anything in Cargo.toml unless you have a weird non-standard layout