Using a C++ library from Racket

I would like to use an existing C++ library from a Racket program. The library is sufficiently complicated that the authors figured they needed C++

How should I go about that?

Presumably using the ffi.
Is there existing documentation about using ffi with C instead of C++?

Or do I need to define my own set of C-interfaced functions (using something like extern "C" in new C++ code that I write) and call them from Racket's ffi?

Presumably there will be issues with system initialization. Doesn't C++ like to own the main() function?

Presumably there will be issues with C++'s storage allocation. It has its own ideas about constructors, destructors, pointer copying, etc. How to set things up so that C++ will use these properly and not get interference from the garbage collector?

-- hendrik

1 Like

Without knowing more details, yes, you will need to define a C API for the C++ library.

Native C++ libraries do name mangling. The C API will expose a non-mangled version. It will also take care of fitting the C++ object model into something that can work with raw pointers, plain-old-data and manage memory, such that Racket (or another higher language) can safely use the FFI constructs with the garbage collector.

Most C++ libraries do not need to own the main() function. If they need one time initialization, they will usually provide a function for that. You will have to make sure your Racket bindings also call that function before doing other operations.