dtolnay / cxx

Safe interop between Rust and C++
https://cxx.rs
Apache License 2.0
5.93k stars 337 forks source link

Using many cxx libraries at the same time #252

Open TheZoq2 opened 4 years ago

TheZoq2 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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.

adetaylor commented 4 years ago

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: https://github.com/rust-lang/rust/issues/73632