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

[Example Request]: How to integrate 2+ cxx crate #448

Open
OlivierLDff opened this issue Oct 16, 2023 · 2 comments
Open

[Example Request]: How to integrate 2+ cxx crate #448

OlivierLDff opened this issue Oct 16, 2023 · 2 comments

Comments

@OlivierLDff
Copy link
Contributor

Current Behavior

There is at the moment an example on how to use 1 cxx crate from cmake in https://github.com/corrosion-rs/corrosion/tree/master/test/cxxbridge.

Expected Behavior

I've read that this is possible to have multiple cxx rlib, one rust static library that does extern crate thingy. But I haven't seen any example how to do that with cmake, and how does it integrate with corrosion_add_cxxbridge.

From the book:

We currently require the CRATE argument to be a target imported by Corrosion, however, Corrosion does not import rlib only libraries. As a workaround users can add staticlib to their list of crate kinds. In the future this may be solved more properly, by either adding an option to also import Rlib targets (without build rules) or by adding a MANIFEST_PATH argument to this function, specifying where the crate is.

Is there an example somewhere of how to do so? A minimal working example.

Steps To Reproduce

No response

Environment

- OS:
- CMake:
- CMake Generator:

CMake configure log with Debug log-level

No response

CMake Build step log

No response

@OlivierLDff OlivierLDff added the bug Something isn't working label Oct 16, 2023
@jschwe jschwe removed the bug Something isn't working label Oct 17, 2023
@jschwe jschwe removed their assignment Oct 21, 2023
@jschwe
Copy link
Collaborator

jschwe commented Oct 21, 2023

I've read that this is possible to have multiple cxx rlib, one rust static library that does extern crate thingy.

Until the rlib format is stabilized for external linker consumption, you do need to ensure that only one rust static library is linked into a C/C++ executable. Adding a dummy crate that depends on the crates you are interested in would be a solution and could be automated by generating a corresponding Cargo.toml.

As for running cxxbridge: I don't work with C++ myself, but in principle all you need to do is run the cxxbridge command once for every file that needs cxx bindings. A nice solution might be to parse the cargo metadata output of the staticlib crate, and run cxxbridge on every dependency that depends directly on cxx (My assumption is that you only depend on cxx directly when you want bindings for that crate). There is already some parsing of cargo metadata implemented, to determine the version of cxxbridge to use.

For a first test, this dependency parsing could be skipped entirely and just pass the relevant .rs file manually via the FILES parameter. The corrosion_add_cxxbridge function needs to be adapted slightly for this to allow absolute paths, and still place the generated files into sensible directories, but I think the cxxbridge itself doesn't really need to know about the crate name or anything.

Then you have a bunch of .cc and .h files, and if there are no duplicate symbol names, just linking the generated cxx static lib with the rust static lib into your C++ project should hopefully work.

@OlivierLDff
Copy link
Contributor Author

Ok I see, some hours of exploration/experiment are still needed. I'm still new to rust so I'm not that fluent in all of the ecosystem.
I think I will go the simple way of linking to dynamic libraries which should solve the issues right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants