Michael Pittino
Michael Pittino

Reputation: 1636

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?

Upvotes: 3

Views: 2079

Answers (2)

Daniel Fath
Daniel Fath

Reputation: 18069

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.

Upvotes: 4

Michael Pittino
Michael Pittino

Reputation: 1636

Just figured it: Rename the src/main.rs to src/lib.rs and it only builds the lib target!

Upvotes: 0

Related Questions