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

Can v2 be used in c++? #91

Closed
Ben-Mack opened this issue Jan 7, 2021 · 4 comments
Closed

Can v2 be used in c++? #91

Ben-Mack opened this issue Jan 7, 2021 · 4 comments

Comments

@Ben-Mack
Copy link

Ben-Mack commented Jan 7, 2021

I have no experience with Rust so it would be great if there's some guidance on how to intergrate dssim v2 with c++ codebase.

@kornelski
Copy link
Owner

Theoretically, yes: https://doc.rust-lang.org/nomicon/ffi.html#calling-rust-code-from-c

Rust can make C-compatible static libraries and export C ABI structs and functions, which you can then pick up on the C++ side. There's even tooling like https://lib.rs/cxx that automates a lot of this.

However, I haven't prepared an interface for it, so it needs a bit of DIY. If you'd like to do it, then:

  1. dssim-core is the library interface with minimal dependencies (no image I/O) so it's the best one to expose. https://github.com/kornelski/dssim/tree/main/dssim-core
  2. Create C-compatible functions that have #[no_mangle] pub extern "C" fn type for the functionality you need.
  3. Write their equivalent as a C header (or use https://lib.rs/cbindgen to automate this)
  4. Add [lib]\ncrate-type = ["lib", "staticlib"] to https://github.com/kornelski/dssim/blob/main/dssim-core/Cargo.toml
  5. cargo build --release will then make target/release/libdssim-core.a that you can link with a C or C++ project

@Ben-Mack
Copy link
Author

Ben-Mack commented Jan 7, 2021

Thank you for the quick and detailed response!

Please consider adding such interface to the library as I think there'll be more demand to use this lib with C/C++.

@kornelski
Copy link
Owner

Done in b5c4195

@Ben-Mack
Copy link
Author

Ben-Mack commented Jan 8, 2021

Wow, that's really quick, these code comments are so detailed too, thanks a lot!

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