dtolnay / cxx

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

Building two CXX C++ bindings with the same symbols but in different namespaces. #1346

Closed SDAChess closed 4 months ago

SDAChess commented 4 months ago

Hello,

I am trying to build C++ bindings for my Rust code and I'm hitting the following problem. I have two libraries (libA and libB) that both defines the same symbols, under a different namespace (libA::test and libB::test) for instance.

When creating bindings for both these functions, I get a linking error from Cargo saying that symbols are defined mutiple times.

I assume this is because CXX generates the bridge code in the top level namespace.

Do you have any ideas/solutions.

I'm guessing generating the bridge code in separate namespaces (for instance the one given in the compile function for the bridge) would fix this issue but maybe this is not possible for FFI?

Thank you very much.

SDAChess commented 4 months ago

When trying to reproduce this in a simpler example I am not running into the same issue. I will keep you updated on this but it might be an issue on my side.

SDAChess commented 4 months ago

Okay, turns out that if you forget to annotate a member function with the #[namespace] directive, it will still work because cxx will find the member function, but the generated code won't be put in the correct namespace. I don't think this is a bug but a bit troubling at first. Closing this.