What are rustc -l and -L actually for?

I am compiling a crate with rustc only, purely out of curiosity, and I noticed something strange.

When linking a crate to a file, in extern crate mycrate and mycrate::myitem in the source, the only way to do it, seemingly, is by using rustc [OPTIONS] --extern mycrate=libmycrate.rlib, which works great, but -l never seems to work (rustc [OPTIONS] -l libmycrate.rlib), and I'm not even sure what -l even does (I know it links something, but what? It can't be external crates.) And what does -L do? It's something about the library search path, but what libraries is it searching for? It can't be external crates.

Nothing in the documentation (Rust reference, rustc book) really clarifies this for me.

An explanation on what these options do and how to use them would be excellent.

Thank you!

I'm not at a computer to check the usage info, but my guess is that -l links against a precompiled static library (like with C compilers), and -L determines the search path for -l.

-L [<KIND>=]<PATH>  Add a directory to the library search path. The
                        optional KIND can be one of
                        <dependency|crate|native|framework|all> (default: all).

-l [<KIND>[:<MODIFIERS>]=]<NAME>[:<RENAME>]
                        Link the generated crate(s) to the specified native
                        library NAME. The optional KIND can be one of
                        <static|framework|dylib> (default: dylib).
                        Optional comma separated MODIFIERS
                        <bundle|verbatim|whole-archive|as-needed>
                        may be specified each with a prefix of either '+' to
                        enable or '-' to disable.

IIRC ".rlib" is basically a static lib (".a" or ".lib") that includes the Rust API type metadata needed for type checking, so if you use them as a static library only you're satisfying the linker with the symbols it would have wanted, but not the Rust frontend when it's trying to resolve the names.

I'm not sure if there's a valid use case for passing an .rlib to -l, perhaps it should warn?

It took me a couple years to figure out. When you store all crates in one place, then -L is helpful. -l used to specify a crate individually.