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

Using many cxx libraries at the same time #252

Open
TheZoq2 opened this issue Aug 11, 2020 · 3 comments
Open

Using many cxx libraries at the same time #252

TheZoq2 opened this issue Aug 11, 2020 · 3 comments
Labels
integration How cxx fits into the big picture of an organization's codebase and builds linking Issues that manifest as link failures

Comments

@TheZoq2
Copy link

TheZoq2 commented Aug 11, 2020

Since I liked the first time I used this crate, I figured I could start RIIRing another part of my project. This part is a standalone library, so I don't want to merge it with my first converted part, but unfortunately that means I run into some linker errors because I now link against the cxx library twice.

  = note: /usr/bin/ld: /home/frans/Documents/phd/cinnabar/mercurous/target/debug/deps/libcxx-3ab177cc3e5e5c69.rlib(cxx-3ab177cc3e5e5c69.cxx.ed64qtld-cgu.13.rcgu.o): in function `cxxbridge03$str$valid':
          /home/frans/.cargo/registry/src/github.com-1ecc6299db9ec823/cxx-0.3.4/src/symbols/rust_str.rs:5: multiple definition of `cxxbridge03$str$valid'; ../affine-arith/build/libaffine_arith_rust.a(cxx-34c4476bdb46593d.cxx.c3ul3jrz-cgu.13.rcgu.o):cxx.c3ul3jrz-cgu.13:(.text.cxxbridge03$str$valid+0x0): first defined here
          /usr/bin/ld: /home/frans/Documents/phd/cinnabar/mercurous/target/debug/deps/libcxx-3ab177cc3e5e5c69.rlib(cxx-3ab177cc3e5e5c69.cxx.ed64qtld-cgu.5.rcgu.o): in function `cxxbridge03$exception':
          /home/frans/.cargo/registry/src/github.com-1ecc6299db9ec823/cxx-0.3.4/src/symbols/exception.rs:4: multiple definition of `cxxbridge03$exception'; ../affine-arith/build/libaffine_arith_rust.a(cxx-34c4476bdb46593d.cxx.c3ul3jrz-cgu.10.rcgu.o):cxx.c3ul3jrz-cgu.10:(.text.cxxbridge03$exception+0x0): first defined here
          /usr/bin/ld: /home/frans/Documents/phd/cinnabar/mercurous/target/debug/deps/libcxx-3ab177cc3e5e5c69.rlib(cxx-3ab177cc3e5e5c69.cxx.ed64qtld-cgu.8.rcgu.o): in function `cxxbridge03$string$clone':
          /home/frans/.cargo/registry/src/github.com-1ecc6299db9ec823/cxx-0.3.4/src/symbols/rust_string.rs:12: multiple definition of `cxxbridge03$string$clone'; ../affine-arith/build/libaffine_arith_rust.a(cxx-34c4476bdb46593d.cxx.c3ul3jrz-cgu.7.rcgu.o):cxx.c3ul3jrz-cgu.7:(.text.cxxbridge03$string$clone+0x0): first defined here
          /usr/bin/ld: /home/frans/Documents/phd/cinnabar/mercurous/target/debug/deps/libcxx-3ab177cc3e5e5c69.rlib(cxx-3ab177cc3e5e5c69.cxx.ed64qtld-cgu.8.rcgu.o): in function `cxxbridge03$string$drop':
          /home/frans/.cargo/registry/src/github.com-1ecc6299db9ec823/cxx-0.3.4/src/symbols/rust_string.rs:33: multiple definition of `cxxbridge03$string$drop'; ../affine-arith/build/libaffine_arith_rust.a(cxx-34c4476bdb46593d.cxx.c3ul3jrz-cgu.7.rcgu.o):cxx.c3ul3jrz-cgu.7:(.text.cxxbridge03$string$drop+0x0): first defined here
          /usr/bin/ld: /home/frans/Documents/phd/cinnabar/mercurous/target/debug/deps/libcxx-3ab177cc3e5e5c69.rlib(cxx-3ab177cc3e5e5c69.cxx.ed64qtld-cgu.8.rcgu.o): in function `cxxbridge03$string$from':
          /home/frans/.cargo/registry/src/github.com-1ecc6299db9ec823/cxx-0.3.4/src/symbols/rust_string.rs:17: multiple definition of `cxxbridge03$string$from'; ../affine-arith/build/libaffine_arith_rust.a(cxx-34c4476bdb46593d.cxx.c3ul3jrz-cgu.7.rcgu.o):cxx.c3ul3jrz-cgu.7:(.text.cxxbridge03$string$from+0x0): first defined here
          /usr/bin/ld: /home/frans/Documents/phd/cinnabar/mercurous/target/debug/deps/libcxx-3ab177cc3e5e5c69.rlib(cxx-3ab177cc3e5e5c69.cxx.ed64qtld-cgu.8.rcgu.o): in function `cxxbridge03$string$len':
          /home/frans/.cargo/registry/src/github.com-1ecc6299db9ec823/cxx-0.3.4/src/symbols/rust_string.rs:43: multiple definition of `cxxbridge03$string$len'; ../affine-arith/build/libaffine_arith_rust.a(cxx-34c4476bdb46593d.cxx.c3ul3jrz-cgu.7.rcgu.o):cxx.c3ul3jrz-cgu.7:(.text.cxxbridge03$string$len+0x0): first defined here
          /usr/bin/ld: /home/frans/Documents/phd/cinnabar/mercurous/target/debug/deps/libcxx-3ab177cc3e5e5c69.rlib(cxx-3ab177cc3e5e5c69.cxx.ed64qtld-cgu.8.rcgu.o): in function `cxxbridge03$string$new':
          /home/frans/.cargo/registry/src/github.com-1ecc6299db9ec823/cxx-0.3.4/src/symbols/rust_string.rs:7: multiple definition of `cxxbridge03$string$new'; ../affine-arith/build/libaffine_arith_rust.a(cxx-34c4476bdb46593d.cxx.c3ul3jrz-cgu.7.rcgu.o):cxx.c3ul3jrz-cgu.7:(.text.cxxbridge03$string$new+0x0): first defined here
          /usr/bin/ld: /home/frans/Documents/phd/cinnabar/mercurous/target/debug/deps/libcxx-3ab177cc3e5e5c69.rlib(cxx-3ab177cc3e5e5c69.cxx.ed64qtld-cgu.8.rcgu.o): in function `cxxbridge03$string$ptr':
          /home/frans/.cargo/registry/src/github.com-1ecc6299db9ec823/cxx-0.3.4/src/symbols/rust_string.rs:38: multiple definition of `cxxbridge03$string$ptr'; ../affine-arith/build/libaffine_arith_rust.a(cxx-34c4476bdb46593d.cxx.c3ul3jrz-cgu.7.rcgu.o):cxx.c3ul3jrz-cgu.7:(.text.cxxbridge03$string$ptr+0x0): first defined here
          collect2: error: ld returned 1 exit status

Is there a way to work around this, something like optionally not building against the cxx cpp file?

@TheZoq2
Copy link
Author

TheZoq2 commented Aug 11, 2020

I thought a bit more about this, and to me, it looks like these are symbols defined in rust, not in the cpp file, so just not building against the cxx cpp file probably won't help in this case

Edit: I was going to try to add a fix for this, so I added cxx as a submodule and now things compile as expected. I pressume that's not a solution though

@antonok-edm
Copy link

Mozilla's approach to the duplicate symbols problem is to compile all libraries into rlibs, and then create a single wrapper crate around all of them that compiles into a staticlib for linking purposes. I'm curious to see if that could be applicable for cxx as well; if so it would be nice to have a guide somewhere.

@dtolnay dtolnay added integration How cxx fits into the big picture of an organization's codebase and builds linking Issues that manifest as link failures labels Aug 30, 2020
@adetaylor
Copy link
Collaborator

I wrote some stuff here which may be relevant:
https://github.com/adetaylor/cxx/blob/book/book/src/building.md#linking-the-c-and-rust-together

but the bottom line is, @antonok-edm is right - the only realistic solution is to pull all your Rust code together into a single staticlib target :( There's a bit more discussion here: rust-lang/rust#73632

vlovich added a commit to vlovich/lol-html that referenced this issue Apr 26, 2021
When you have multiple disparate Rust libraries that you link into a
larger C/C++ executable, you need to create a super crate that links
all dependencies to avoid linkage issues (e.g. so that Cargo & Rust can
see semver incompatabilities & build them correctly among other things).
Add `rlib` to the `crate-type` list for C bindings to allow building
such a "super" crate.

The following links:

* https://github.com/adetaylor/cxx/blob/book/book/src/building.md#linking-the-c-and-rust-together
* dtolnay/cxx#252

have more context.
vlovich added a commit to vlovich/lol-html that referenced this issue Apr 26, 2021
When you have multiple disparate Rust libraries that you link into a
larger C/C++ executable, you need to create a super crate that links
all dependencies to avoid linkage issues (e.g. so that Cargo & Rust can
see semver incompatabilities & build them correctly among other things).
Add `rlib` to the `crate-type` list for C bindings to allow building
such a "super" crate.

The following links:

* https://github.com/adetaylor/cxx/blob/book/book/src/building.md#linking-the-c-and-rust-together
* dtolnay/cxx#252

have more context.
kornelski pushed a commit to cloudflare/lol-html that referenced this issue Apr 27, 2021
When you have multiple disparate Rust libraries that you link into a
larger C/C++ executable, you need to create a super crate that links
all dependencies to avoid linkage issues (e.g. so that Cargo & Rust can
see semver incompatabilities & build them correctly among other things).
Add `rlib` to the `crate-type` list for C bindings to allow building
such a "super" crate.

The following links:

* https://github.com/adetaylor/cxx/blob/book/book/src/building.md#linking-the-c-and-rust-together
* dtolnay/cxx#252

have more context.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integration How cxx fits into the big picture of an organization's codebase and builds linking Issues that manifest as link failures
Projects
None yet
Development

No branches or pull requests

4 participants