I am curious about rust executable. I know when I run cargo run command with debug or release mode, under target folder there is an executable. However, when I run the ldd command on that executable it gives the following output;
Which makes me think is it acting like a static library ? I know if I give this executable to someone else, they can run it even without having Rust installed (at least book says this way). Therefore, it makes sense if it is acting as static.
Is there anyway to make it dynamic. I mean I want to make a common folder of my crates (dependencies) for all my projects and give this path when compiling my project (such as L flag to gcc/g++). Afterwards, without that folder executable should not work? What my goal is reducing the size of executable and make it more environment dependent.
There's -C prefer-dynamic, but it's a bad idea to use it, because it does make executables depend on the Rust compiler, and not just any Rust compiler, but your exact version on your computer. Rust's ABI for Rust-only dynamic libraries is not guaranteed to remain the same between Rust versions.
Rust can dynamically link C libraries that have a stable ABI, and Rust libraries that explicitly export a C ABI, but majority of crates needs to be linked statically, and many (crates that use generics or proc macros) are even logically impossible to ever implement as a dynamic library.
I have 2 questions related with this. I couldn't figure it out how to use -C prefer-dynamic flag while creating binary.
When I run my project with cargo run --release, is the executable under ./target/release/rust_project static? If it is working in different environments which don't even have rust downloaded it should be static right?
If it is static, is it possible to make it dynamic? What I mean, I have crates downloaded on my machine and I want to give the path of each crate while running my project with cargo run --release or something like rustc -L /path/of/crates src/main.rs. Ofcourse, with this option, the executable will be dependent on the rust compiler and all of the crates.