Dark hole of linking: duplicate symbols

I has workspace: foo-sys (lib crate, wrapper around C++ library), foo (lib crate wrapper for foo-sys),
demo (bin crate to demonstrate work of foo).

The problem that C++ library consists from two static libraries, with one
common common object file - boo.o, C++ linker handle this without problems,
but cargo/rustc works with that in very strange manner.

cd crate && cargo build
cd crate && cargo test

works for all three: foo, foo-sys, demo.

But cargo test in the root of workspace gives error about duplication symbols from boo.o.

What is goging on, any idea?

I run cargo test -vv and cd demo && cargo test -vv,
and only difference between linking of foo-sys is

good:
--json=diagnostic-rendered-ansi,artifacts
--crate-type
lib
--emit=dep-info,metadata,link
bad:
--json=diagnostic-rendered-ansi
--emit=dep-info,link
-C
debuginfo=2
--test

So I completly at lost I though that cargo test for workspace is just run cargo test for all crates in workspace, but looks like it is not?

What is the full rustc invocation for foo-sys when using cargo test? Also during compilation of which crate are the linker errors generated?

Actually I run cargo test in both cases, but in the good one I run it inside crate's directories,
and in the bad one I run in the top directory where:

Cargo.toml
[workspace]
members = ["foo-sys", "foo", "demo"]

In the good case rustc invocation:

rustc --crate-name foo_sys --edition=2018 foo-sys/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -C debuginfo=2 -C metadata=852d2a1fcf1f062d -C extra-filename=-852d2a1fcf1f062d --out-dir workspace/target/debug/deps -C incremental=workspace/target/debug/incremental -L dependency=workspace/target/debug/deps -Clink-arg=-fuse-ld=gold -L native=workspace/target/debug/build/foo-sys-87bf250393e539bc/out/build -L native=workspace/target/debug/build/foo-sys-87bf250393e539bc/out/build/vendor/lib2 -l static=CppLib2 -l static=CppLib1 -l stdc++

in the bad one:

rustc --crate-name foo_sys --edition=2018 foo-sys/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --emit=dep-info,link -C debuginfo=2 --test -C metadata=76e75dee7179080d -C extra-filename=-76e75dee7179080d --out-dir workspace/target/debug/deps -C incremental=workspace/target/debug/incremental -L dependency=workspace/target/debug/deps -Clink-arg=-fuse-ld=gold -L native=workspace/target/debug/build/foo-sys-87bf250393e539bc/out/build -L native=workspace/target/debug/build/foo-sys-87bf250393e539bc/out/build/vendor/lib2 -L native=workspace/target/debug/build/foo-sys-87bf250393e539bc/out -l static=CppLib2 -l static=CppLib1 -l stdc++

Also during compilation of which crate are the linker errors generated?

foo-sys, the same problem happens if I run cargo test -p foo-sys, instead of cargo test in the parent directory of all crates.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.