#How does rustc (not cargo) finds the linking library file's name?

6 messages · Page 1 of 1 (latest)

jovial bridge
#

When we use rustc (not cargo) to compile a rust file like this ```rust
extern crate my_crate
fn main() {
my_crate::run();
}

lets say we invoke rustc using `rustc --out-dir ./build main.rs -Ldeps`, and the `my_crate` could be anything (rlib/dylib/cdylib), how would rust know the exact filename of the correct crate?
jovial bridge
#

I just tested it with rlib and here are my observations:

Code:

// lib.rs
#![crate_type = "rlib"]
#![crate_name = "my_lib"]

pub fn run() {
    println!("Working!")
}
// main.rs
extern crate my_lib;

fn main() {
    my_lib::run();
}

Commands I used to compile (on windows):

C:/Users/Me/Desktop/Rust/Test> rustc lib.rs
C:/Users/Me/Desktop/Rust/Test> rustc main.rs -L.

Observations:

  1. Compiling the lib.rs file gave me a new file named libmy_lib.rlib
  2. If I rename this file to libsomething.rlib, and in main.rs, I use extern crate something, the compilation fails.
  3. If I rename this file to libsomething.rlib, and leave everything else the same, the compilation still fails.

Basically if I keep the compiler generated name, and I have used the crate_name attribute while compiling the library, the compilation is successful, otherwise it would give compilation error.

warm lava
warm lava
warm lava
jovial bridge
#

but if I change libmy_lib.rlib to libsomething.rlib, and extern my_lib to extern something, the compilation breaks.