Quick build of single file that uses lib(s)

Hello all,

coming from C-lang universe...

i wonder whether it is possible to easily build single .rs file that uses external "libraries", like structopt from Rust By Example - Is it possible to use some C language equivalent of:

gcc -L path/to/something -l libsomething mysource.c

(no C integration to be used in Rust, just as a comparison of lib being used in C)

without need to have all the project/crate/module files/structure?

For quick introductory experiments and single file tiny apps, it seems over-complicated to create project structure all the time, with all the toml, main.rs, etc.
I'd prefer to use rustc directly, with ability to tell it where/how to find "used" Rust libraries...

Trying to google stuff up (and not knowing correct terms most probably yet), i end in various cargo tutorials and all the file creation manuals, which i hope to be able to skip/not use...

The reference you’re looking for is The rustc book. If I recall correctly you want

rustc main.rs -L /path/to/libs --extern crate1 --extern crate2

and it will look for /path/to/libs/libcrate1.rlib and /path/to/libs/libcrate2.rlib.

1 Like

thank you for direction, i need to go through rustc book i guess, though i hoped that it would be mentioned as one of common items for newcomers to be able to run simple apps with common "imports"...

i still get the same error when i try to link to a directory that contains structopt-0.3.17.crate downloaded "somehow" when i built the file via full project structure...

>echo %RUSTLIBS%
>rustc -L %RUSTLIBS% --extern structopt src\fretboard.rs
error[E0432]: unresolved import `structopt`
 --> src\fretboard.rs:1:5
1 | use structopt::StructOpt;
  |     ^^^^^^^^^ maybe a missing crate `structopt`?

I'll dive into referenced rustc book :slight_smile:

Edit: You probably want --edition=2018 on the command line as well. Otherwise it may be expecting extern crate declarations in your source code.

I’ve never tried to link a cargo-downloaded crate into one of my non-cargo projects, so I unfortunately can’t give you any further advice. Good luck.

*.crate files are just a gzip-ed bundle of the source code. It needs to be unpacked, then build, and finally linked into your application.
That's what cargo does for you.
Of course you could do that manually, but it involves passing a lot of flags to several invocations of rustc

thanks for pointers, ill grok through the book and post here later, if i get my wanted solution for the curious... :slight_smile: maybe i jumped into build customizations too soon on my Rust path...

Learning rust first without cargo is a reasonable approach, as long as you don’t mind sticking to the faciities that std provides or that you write yourself. It’s what I did and it’s served me well, but it’s certainly not for everyone.

If you want to know which rustc invocation cargo uses you can run cargo build --verbose on such a project.
I did that, build a small tool and blogged about it.


I'd say "quick" is quite colored by the C expectations, and doesn't match what's actually quick in the Rust world. In Rust running rustc directly is the slow, tedious, error-prone way. It's an equivalent of running the C preprocessor, compiler, assembler, archiver, and linker all separately, because the 5-in-1 combined "gcc" command is doing too much.

It's quicker to write cargo new than to write equivalent rustc commands. Even in terms of compile time, running bare rustc it's not saving you time. Cargo is quite smart in the way it uses rustc, and enables parallelized, incremental builds.

1 Like

The flip side of this is that cargo makes it so easy to add dependencies that you can easily end up building packages you don’t really need to support features you don’t use. Build times for my copy-and-paste prototypes are significantly faster than the equivalent version that uses popular crates.

Of course, this advantage goes away as soon as I need something really complex.

I remember reading about a tool made for something like this, I think it was https://crates.io/crates/cargo-single

1 Like

I'd mark both current and @Heliozoa's answers as solutions to my problem, i gotta let old/foreign language habits go... :slight_smile:

Thanks also to all others for tips and clarifications on some points!

1 Like