fltk-rs / fltk-webview

webview functionality for embedded fltk windows
MIT License
47 stars 7 forks source link

C-ABI compatibility #8

Closed zcream closed 3 years ago

zcream commented 3 years ago

There are 2 ways to use rust crates from C. My interest would be using web view from fltk in C.

  1. cbindgen
  2. cargo-c

Are any of these recommended ? The cargo-c method requires

The TL;DR:

Create a capi.rs with the C-API you want to expose and use #[cfg(cargo_c)]#[cfg(feature="capi")] to hide it when you build a normal rust library.
Make sure you have a lib target and if you are using a workspace the first member is the crate you want to export, that means that you might have to add a "." member at the start of the list.
Since Rust 1.38, also add "staticlib" to the "lib" crate-type. Do not specify the crate-type, cargo-c will add the correct library target by itself.
You may use the feature capi to add C-API-specific optional dependencies.
NOTE: It must be always present in Cargo.toml

Remember to add a cbindgen.toml and fill it with at least the include guard and probably you want to set the language to C (it defaults to C++)
Once you are happy with the result update your documentation to tell the user to install cargo-c and do cargo cinstall --prefix=/usr --destdir=/tmp/some-place or something along those lines.
MoAlyousef commented 3 years ago

You can normally use webview directly from C/C++. I have a proof of concept here using it with FLTK (C++): https://github.com/MoAlyousef/Fl_WebView

zcream commented 3 years ago

Asked a related query on the link.