Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build only `lib` target

I want to build a dynamic link library (dll).
My Cargo.toml currently looks like this:

[package]
name = "sample"
version = "0.1.0"
authors = ["author"]

[lib]
name = "main"
crate-type = ["dylib"]

[dependencies]

I use VS Code with the RustyCode plugin as my IDE on windows. When I run the build command this builds into a sample.exe and main.dll.
I know I can run cargo build --lib to only build my lib target but I dont have access to this command inside VS Code (afaik). Is there anyway to specify that I only want to build the lib target in my Cargo.toml file so I can use the VS Code build command which runs cargo build/cargo run?

like image 291
Michael Pittino Avatar asked Jan 21 '26 10:01

Michael Pittino


1 Answers

Cargo builds files using convention over configuration approach. When it finds a main.rs it builds an executable, and when it encounters lib.rs it expects to build a library.

Calling your lib main managed to confuse Cargo. The only solution I managed to find is to either change name of your crate from name = "main" to name = "foo" (and then rename your main.rs into foo.rs) or to change its name to lib.rs, as you did.

like image 135
Daniel Fath Avatar answered Jan 23 '26 06:01

Daniel Fath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!