Cargo - generating --extern flags for rustc

⚓ Rust    📅 2026-01-10    👤 surdeus    👁️ 3      

surdeus

Hi there,

I have been doing some research on how better to integrate cargo projects with beardbolt - a godbolt/compiler-explorer like tool for Emacs.

The default example that comes with beardbolt (and its predecessor RMSBolt) is only really suitable for rust code that doesn't include any dependencies. I however would like to work with cargo dependencies.

I've been able to get it working with a dependency by manually generating a rustc command for beardbolt to use (edited for legibility):

rustc --crate-name example --edition=2024 --crate-type bin -C opt-level=0 \
  -L dependency=/home/john/.local/state/emacs/beardbolt-sandbox/target/debug/deps \
  --extern ndarray=/home/john/.local/state/emacs/beardbolt-sandbox/target/debug/deps/libndarray-dd3e9807fe477367.rlib

beardbolt will then generate the following command (edited for legibility):

rustc --crate-name example --edition=2024 --crate-type bin -C opt-level=0 \
  -L dependency=/home/john/.local/state/emacs/beardbolt-sandbox/target/debug/deps \
  --extern ndarray=/home/john/.local/state/emacs/beardbolt-sandbox/target/debug/deps/libndarray-dd3e9807fe477367.rlib \
  -C debuginfo=1 --emit link /tmp/beardbolt-dump-ErjehI.rs \
  -o /home/john/.local/state/emacs/beardbolt-sandbox/beardbolt.o \
&& objdump -d /home/john/.local/state/emacs/beardbolt-sandbox/beardbolt.o \
   --insn-width=16 -l -M att \
> /home/john/.local/state/emacs/beardbolt-sandbox/beardbolt.o.disass

screenshot-20260110-03:12:43

I want to be able to generate the --extern flag dynamically - but I can't see a way of doing this.

  • I checked the cargo metadata command, but it doesn't seem to contain these output directories
  • I don't seem to be able to do a dry run of cargo build
  • The checksum in Cargo.lock doesn't match the hash used in the rlib path
  • When multiple builds happen, you can have multiple rlibs, and I don't know how to figure out which one is the correct one

I'd really appreciate it if someone could point me in the right direction, thank you!

JH

9 posts - 3 participants

Read full topic

🏷️ Rust_feed