I tried to use cargo new --lib restaurant
to create a lib
but an error was reported when compiling:
no main
function was found in a binary crate.
After checking, I found that the rust application I created was a binary application instead of a lib. But if I directly use clion to create a lib, it can compile successfully. I want to know what should I do if I want to use cargo to create a lib?
I don't know what went wrong with your library, but any project with a main.rs
file is considered a binary crate and is required to have a main function. Did you perhaps rename the lib.rs
file to main.rs
?
I did not change the name of the lib.rs file to main.rs. If I use cargo to create the lib, then lib.rs will require me to have a main function as the program entry, and prompt me that the program is a binary program, but in reality I want to create a library instead of an executable program. Using clion to create a lib will not cause this error. My lib.rs does not need to include a main function and can be compiled
The only difference between cargo new --bin
and cargo new --lib
is that the former gives you placeholder src/main.rs
and the latter gives you a placeholder src/lib.rs
. At least, with my vanilla setup.
Is there a [[bin]]
section in your Cargo.toml
?
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.