Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linking against multiple static libraries that use cxx #883

Closed
jbruechert opened this issue Jun 9, 2021 · 2 comments
Closed

Linking against multiple static libraries that use cxx #883

jbruechert opened this issue Jun 9, 2021 · 2 comments
Labels
linking Issues that manifest as link failures

Comments

@jbruechert
Copy link

In my C++ project I have two rust crates that export a C++ API using cxx.

That setup ends in a bunch of linking errors like this:

/usr/bin/ld: libangelfish_readability.a(cxx-546393ec83734d88.cxx.77528ncj-cgu.12.rcgu.o): in function `cxxbridge1$exception':
/home/jbb/.cargo/registry/src/gh.neting.cc-1ecc6299db9ec823/cxx-1.0.49/src/symbols/exception.rs:6: multiple definition of `cxxbridge1$exception'; libangelfish_adblock.a(cxx-506fb85a29953a06.cxx.6ziszbha-cgu.12.rcgu.o):/home/jbb/.cargo/registry/src/gh.neting.cc-1ecc6299db9ec823/cxx-1.0.49/src/symbols/exception.rs:6: first defined here
/usr/bin/ld: libangelfish_readability.a(cxx-546393ec83734d88.cxx.77528ncj-cgu.12.rcgu.o): in function `cxxbridge1$str$from':
/home/jbb/.cargo/registry/src/gh.neting.cc-1ecc6299db9ec823/cxx-1.0.49/src/symbols/rust_str.rs:18: multiple definition of `cxxbridge1$str$from'; libangelfish_adblock.a(cxx-506fb85a29953a06.cxx.6ziszbha-cgu.12.rcgu.o):/home/jbb/.cargo/registry/src/gh.neting.cc-1ecc6299db9ec823/cxx-1.0.49/src/symbols/rust_str.rs:18: first defined here

Can I do anything about it apart from moving everything into one crate, or is this a general limitation?

@Geobert
Copy link

Geobert commented Jul 15, 2021

What's the status on this? Any clue if it's a limitation or if some method is available to make that work?

@dtolnay dtolnay added the linking Issues that manifest as link failures label Sep 5, 2021
@dtolnay
Copy link
Owner

dtolnay commented Sep 5, 2021

According to https://cxx.rs/build/other.html (source: @adetaylor in #345):

you first need to use rustc and/or cargo to generate a single Rust staticlib target and pass that into your foreign linker invocation.

  • If you need to link multiple Rust subsystems, you will need to generate a single staticlib perhaps using lots of extern crate statements to include multiple Rust rlibs. Multiple Rust staticlib files are likely to conflict.

That all seems consistent with what you've described. Rust does not expose any way for a C++ linker to link multiple Rust staticlibs. That part always has to be done by Rust's linker.

rust-lang/rust#73632 has more extensive discussion of this situation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
linking Issues that manifest as link failures
Projects
None yet
Development

No branches or pull requests

3 participants