Skip to content

Getting cxxbridge working in a cmake codebase #229

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

Closed
Nehliin opened this issue Jul 21, 2020 · 7 comments
Closed

Getting cxxbridge working in a cmake codebase #229

Nehliin opened this issue Jul 21, 2020 · 7 comments
Labels
integration How cxx fits into the big picture of an organization's codebase and builds

Comments

@Nehliin
Copy link
Contributor

Nehliin commented Jul 21, 2020

Hello,
I'm a bit confused about the usage of the cxxbridge tool and I'm sure I've just misunderstood how to use it. I've tried to emulate the hello world example in this repo but my company is using cmake and this would be part of a much larger codebase so I tried to avoid using build.rs scripts. Our use case would be that we want to gradually replace an existing C++ code base with Rust so I want to link a Rust lib to the C++ executable.

First off both the header and cc file generated from my lib.rs file contains a definition of the shared struct which of course causes compile errors if I were to include the header in the cc file without adding ifdefs. The shared struct I've defined contains a Vec<u8> but none of the generated files created or the cxx.h file in this repo contains a definition of the private methods such as drop() in the generated C++ code. Where is that definition expected to come from?

@dtolnay
Copy link
Owner

dtolnay commented Jul 21, 2020

There has been some experimentation with CMake in #156; perhaps it would be useful as a starting point.


both the header and cc file generated from my lib.rs file contains a definition of the shared struct which of course causes compile errors if I were to include the header in the cc file without adding ifdefs

The generated cc file is not intended to include the generated header file. Is it necessary in your use case to be able to include it?

I filed #230 to follow up with ifdefs.


The shared struct I've defined contains a Vec<u8> but none of the generated files created or the cxx.h file in this repo contains a definition of the private methods such as drop() in the generated C++ code

The generated files and cxx.h are all in C++, whereas the definition for how to drop a Vec<u8> is something that can only be provided by Rust. The cxx Rust crate provides those definitions (concretely in this file). You would need to make sure that linking includes the Rust code as well as the C++ code.

@Nehliin
Copy link
Contributor Author

Nehliin commented Jul 21, 2020

Thanks for the quick response @dtolnay I'll take a look at the cmake pr! I don't think it's necessary in my use case I'm mainly just experimenting at the moment.

@Nehliin
Copy link
Contributor Author

Nehliin commented Aug 4, 2020

Hello again @dtolnay, I have now managed to get everything setup in my company's cmake set up and got it compiling and working with calling Rust functions in C++ and sharing a struct containing a simple primitive type. I now tried to add Vec<u8> to the shared struct but the generated C++ files lib.rs.h and lib.rs.cc both contain a forward declaration of struct unsafe_bitcopy_t but not any actual implementation of it. I see that it can be found in the cxx.h in this repo. Is there any reason to why that couldn't be part of the auto generated code that cxxbridge creates?

@dtolnay dtolnay added the integration How cxx fits into the big picture of an organization's codebase and builds label Aug 30, 2020
@dtolnay dtolnay changed the title Questions about cxxbridge Getting cxxbridge working in a cmake codebase Aug 30, 2020
@dtolnay
Copy link
Owner

dtolnay commented Sep 2, 2020

The missing definitions you hit was definitely a bug -- #277. I released cxx 0.4.2 with the fix.

@XiangpengHao
Copy link
Contributor

I created an example setup to use cmake and cxx here
Hope it can help!

Please let me know any issues and thanks for the great project! ❤️

@dtolnay
Copy link
Owner

dtolnay commented Dec 22, 2020

I'll close out this issue as https://cxx.rs/build/cmake.html now provides two useful starter examples of CMake setups.

@Be-ing
Copy link

Be-ing commented Aug 27, 2022

@Nehliin I have a proposal for resolving this which I have described in #462. Let's continue the discussion there.

Repository owner locked and limited conversation to collaborators Oct 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
integration How cxx fits into the big picture of an organization's codebase and builds
Projects
None yet
Development

No branches or pull requests

4 participants