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

C Bindings #569

Open
scolsen opened this issue Feb 13, 2023 · 6 comments
Open

C Bindings #569

scolsen opened this issue Feb 13, 2023 · 6 comments

Comments

@scolsen
Copy link
Contributor

scolsen commented Feb 13, 2023

A good portion of libDaisy is currently only defined as CPP classes and bindings. Providing bindings in plain C would not only allow users to leverage libDaisy in plain C, it would also burst open the door for adding bindings to other programming languages, many of which have foreign function interfaces that anticipate C linkage/ABI. For example, if C bindings were provided one could use libDaisy from zig.

I'm going to write such bindings for myself regardless, simply because I much prefer C to CPP and I may explore using other languages as well. Before I do that as a separate project, I figured I'd file an issue here in case it's something others want too and is worth upstreaming into the library. In theory it will only require adding a few extern "C" calls to the CPP sources.

@stephenhensley
Copy link
Collaborator

Hm! I just saw zig for the first time the other day.

I'd be curious to see what the footprint looks like once you get started! 😄

@chaosprint
Copy link

I would second this.

@scolsen
Copy link
Contributor Author

scolsen commented Apr 12, 2023

I'll set aside some time this week to start exploring this. I poked around a bit yesterday, and in most cases we'll likely need to add static factories for constructing and destructing class instances; other than that, all the C definitions can just take an additional instance parameter and wrap calls to the underlying methods.

@scolsen
Copy link
Contributor Author

scolsen commented Apr 29, 2023

Ok, #580 has a small proof-of-concept of how we might do this, starting with the Analog Control API. I've confirmed it at least builds, but we'll likely want to add unit tests for the C bindings.

The general process is as follows:

  1. Define an opaque pointer that represents instances of the class in C.
  2. Define a create method that wraps invocation of the constructor, static_casting it to a void pointer, then C casting the void pointer to the opaque C type.
  3. Define a destroy method that wraps the invocation of the destructor by casting back to the CPP type.
  4. Wrap any other class methods.

@znmeb
Copy link

znmeb commented Apr 29, 2023

+1 for C bindings! But I'm not buying the zig hype just yet. The effort-to-payoff ratio for learning a new syntax and semantics when there are a dozen major Turing-complete multi-paradigm languages, three of which I know well, is probably too high. :-)

@scolsen
Copy link
Contributor Author

scolsen commented Apr 29, 2023

+1 for C bindings! But I'm not buying the zig hype just yet. The effort-to-payoff ratio for learning a new syntax and semantics when there are a dozen major Turing-complete multi-paradigm languages, three of which I know well, is probably too high. :-)

totally, no plans to add explicit zig support, at least not from me. I just mentioned it as one example of a language that could leverage a C FFI. This effort is just limited to providing some C bindings.

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

4 participants