Rustc add extern crate by path

I cannot use cargo for this, so no cargo.toml or anything like that.

I have a file and I want it to use an extern crate. The crate is not available anywhere online, only on my PC. How can I use that crate from the file ?

I tried doing (like I would with a module)

extern crate crate_name;

I also tried

extern crate "/path/to/the/crate" as crate_name

Without any success. Do I need to pass some parameter to rustc to get it working ?

Build a crate from scratch using cargo build --verbose to see the rustc commands it uses. And yes, you need to pass a parameter to rustc.

I found the --extern option, which does not seem to do what I want. I also found the-L crate=... but that also doesn't work. Any advice ?

EDIT : I also tried replacing crate by the crate name, without success

I am not familiar with using rustc directly, but if you post the actual commands you tried, along with some cargo build --verbose commands I can compare with, I can try to guess what you did wrong?

1 Like

For comparison, here's part of the build script from one of my non-cargo projects:


# Abort on first error
set -e

mkdir -p bin
pushd bin

# Compile and run proc macro tests
rustc --edition=2018 --test ../src/ \
      -o ./procmacro-test -A unused_imports

# Compile proc macro crate
rustc --edition=2018 --crate-type=proc-macro \

# Compile and run library test
rustc --edition=2018 --crate-name=memquery --test \
      ../src/ -o ./memquery-test -L . -A unused_imports

# Compile library crate
rustc ../src/ --edition=2018 --crate-type=rlib \
      --crate-name=memquery -L .

# Compile and run example binary tests
rustc ../src/ --edition=2018 --test -o ./kwic-test -L . \
      --extern memquery --extern memquery_procmacro

# Compile example binary
rustc ../src/ --edition=2018 -o ./kwic -L . --extern memquery \
      --extern memquery_procmacro -C opt-level=3 -C lto

Well, I see the -L . that probably tells rustc to 'include' the files in the current directory, but how would I specify a crate with -L and a full path ?

As I recall, rustc really wants compiled crate filenames to follow a specific pattern, and it's the one that is produced by default when you pass --crate-name as a parameter. It will then use the crate name from the extern declaration (whether it's in the source code or on the command line) to look for files of that particular name in the directories specified by the -L flags.

(I think it's libmycrate.rlib for --crate-name=mycrate, but I'm not sure)

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.