C++ static linking breaks in multi-crate workspace

⚓ Rust    📅 2025-05-15    👤 surdeus    👁️ 5      

surdeus

Warning

This post was published 97 days ago. The information described in this article may have changed.

I have a workspace of two crates, with the folder structure:

example-binding
-- Cargo.toml
-- build.rs
-- cpp
   -- stuff.h
   -- stuff.cpp
-- src
   -- lib.rs

example-lib
-- Cargo.toml
-- src
   -- lib.rs

Full example repo: GitHub - hoxxep/rust-cpp-dep: An issue with C++ dependency linking

The example-lib depends on a method in example-binding which depends on the C++ function example_cpp.

The following confuses me:

  • cargo test -p example-binding works and correctly links.
  • cargo test -p example-lib fails to link, with an error for "undefined reference to example_cpp" (the C++ function).

This behaviour happens on an x86 Ubuntu Github Actions runner and an Arm Debian based Pi; but weirdly both commands run without issue on MacOS. Using cargo -vv, the rustc command args don't miss anything obvious like env variables, or -L and --extern flags.

Would anyone have any hints on how to debug this further, or know something obvious that I've missed? I'd be interested in being able to log the linker commands during cargo build/test to see what differs in case there's a difference in cargo commands.

Thank you!

3 posts - 2 participants

Read full topic

🏷️ rust_feed