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

Make cpp functions visible to other packages #407

Closed
NicChr opened this issue Nov 27, 2024 · 1 comment
Closed

Make cpp functions visible to other packages #407

NicChr opened this issue Nov 27, 2024 · 1 comment

Comments

@NicChr
Copy link

NicChr commented Nov 27, 2024

Hello, is there a way to make my C++ functions accessible through cpp11 (or other means) to other R packages that use C/C++?

The goal is to be able to call package A's C++ functions directly in package B's C++ code though I'm not sure how to accomplish this given that much of the registration is automatically generated.

Any help would be appreciated, thanks!

@DavisVaughan
Copy link
Member

DavisVaughan commented Dec 3, 2024

It is possible, though it is not at all easy, but systemfonts does this

It utilizes [[cpp11::init]] to register functions as "callable" at the C level
https://github.com/r-lib/systemfonts/blob/02b567086379edaca1a9b3620ad6776e6bb876a7/src/font_fallback.cpp#L78
https://github.com/r-lib/systemfonts/blob/02b567086379edaca1a9b3620ad6776e6bb876a7/src/font_fallback.h#L15

Then exposes a header file that is used by other packages
https://github.com/r-lib/systemfonts/blob/02b567086379edaca1a9b3620ad6776e6bb876a7/inst/include/systemfonts.h

For example, ragg uses it by LinkingTo systemfonts
https://github.com/r-lib/ragg/blob/6e8bfd1264dfaa36aa6f92592e13a1169986e7b9/DESCRIPTION#L35

Note that you have to be very careful about the types you expose. This basically goes through a "pure C" api path, so you can't really expose C++ types that easily, and you have to "rebuild" any custom types like this one
https://github.com/r-lib/systemfonts/blob/02b567086379edaca1a9b3620ad6776e6bb876a7/inst/include/systemfonts.h#L18

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