cargo icon indicating copy to clipboard operation
cargo copied to clipboard

Allow crate-type=["staticlib","cdylib"] for [[bin]]

Open retep998 opened this issue 6 years ago • 5 comments
trafficstars

Cargo currently enforces only one library crate per package which makes total sense for Rust libraries. However it doesn't make much sense for cdylibs or staticlibs which are not going to be consumed by downstream Rust dependencies. Requiring that only the library crate can be a cdylib or staticlib, and not binary crates, sometimes forces people into mixing lib and cdylib or staticlib and Rust currently produces suboptimal results when doing so. It also means people cannot produce multiple cdylibs or staticlibs from a single package.

I'd like to propose extending [[bin]] to allowcdylib and staticlib for the crate-type.

retep998 avatar Nov 26 '18 21:11 retep998

I'd like to see this as well.

I don't know if it makes sense to do this by extending [[bin]], or by adding a new section [[clib]] or similar.

joshtriplett avatar May 25 '20 18:05 joshtriplett

See #8628.

m-ou-se avatar Aug 17 '20 17:08 m-ou-se

This is a very good proposal. And I have a use case where it would fit perfectly.

When it is necessary to use the same target to generate a desktop executable, but a cdylib for android and wasm, and a staticlib for ios.

It is related to: https://github.com/rust-lang/rfcs/pull/3180

It would not be possible to use the [lib] target to generate these libraries, as there are #[no_mangle] functions that cannot be present in an ordinary rlib, due to name collision. And that way it is also possible to have several native libraries using just one package.

guilhermewerner avatar Oct 08 '21 11:10 guilhermewerner

This could also be potentially useful for building multiple wasm modules.

crumblingstatue avatar Jun 03 '22 13:06 crumblingstatue

My current workaround is to use [[example]] section. This way I can create a cdylib binary, while keeping the core functionality in the [lib] that can be reused from other rust code. See sqlite-hashes and sqlite-compressions -- loadable SQLite extensions.

nyurik avatar Dec 23 '23 04:12 nyurik