4 ms·
Can Rust compile to a shared lib the programs can use as easily as a C library? Yes it can! You use the `#[no_std]` if you wish to be rid of the runtime, and t
by ehsanu1 12y ago
Can Rust compile to a shared lib the programs can use as easily as a C library?
Yes it can! You use the `#[no_std]` if you wish to be rid of the runtime, and the `#[crate_type = "lib"]` for outputting shared libraries (or use the `--crate-type` compiler flag).
Here's an older blog post on it, though the rust code there probably won't compile as-is with current Rust: http://bluishcoder.co.nz/2013/08/08/linking_and_calling_rust_functions_from_c.html http://bluishcoder.co.nz/2013/08/08/linking_and_calling_rust...
- jnbiche 12y agoUnfortunately, this means giving up the powerful Rust standard library and threads. Fortunately, third-party devs are creating a "zero-runtime" standard library that can be used in situations like this, and in embedded programming: https://github.com/thestinger/rust-core https://github.com/thestinger/rust-core Right now, rust-core only provides basic collections and io, but more may be in the works. I suspect rust-core will eventually grow into a small but capable standard library.
- kibwen 12y agorust-core has been coasting along in maintenance mode for quite a while now, because it was only intended as a temporary stopgap. Nobody really wants to fragment the community with a third-party standard library. Instead, the Rust devs intend to restructure the standard library such that it has multiple profiles, and that you can jettison parts of it (such as the parts that require the runtime) without losing all of the other goodies. Here's the most recent RFC for doing so, filed yesterday: https://github.com/rust-lang/rfcs/pull/40/files https://github.com/rust-lang/rfcs/pull/40/files
- jnbiche 12y agoOh, very cool. I was hoping something like this would emerge.
- dbaupp 12y agoTo be clear, #[no_std] only "removes" the runtime by ensuring you don't accidentally use any functions that need the runtime. Something like #![crate_id="basic_lib"] #![crate_type="dylib"] // for a .so /* not there's no `no_std` */ #[no_mangle] pub extern "C" fn add_in_rust(x: i32, y: i32) -> i32 { x + y } Compiling that will give a `libbasic_lib....so` which can be linked against by any C program, and the `add_in_rust` function called without the Rust code needing to touch a runtime. (I imagine that one may have to add some extra linker flags.) There are many functions/types in libstd etc. that can be used without a runtime (although its still not as great as it will be in future).