It depends on your specific use case. If you’re wrapping a small cpp library which only exports a few APIs or a large one in which you only need to call a small subset API, then the “best way” would be to manually 1. write a C89 wrapper over the library and 2. embed it in Julia by directly using ccall
. No package, no magic, and no performance overhead. However, if you’re gonna wrap everything in a large cpp library, it is just not feasible to manually do the above two steps.
Currently, there are three packages that try to alleviate the pain point of manually doing these boring jobs:
CxxWrap.jl
Use this package if you wanna write wrapper code on the C++ side.
- pros:
- relatively mature and full-featured
- under active development
- easy to wrap C++ template function/class
- cons:
- no wrapper generator
- need to learn and use a “glue” language/API
- IIRC, to compile the C++ wrapper code, one needs a compiler that supports C++14
Cxx.jl
:
Use this package if you wanna write wrapper code on the Julia side.
- pros:
- has a native and easy to use C++ REPL
- Julia and C++ code are fully integrated
- easy to wrap C++ template function/class
- cons:
- “magic”(hard to understand what happens under the hood)
- the package is not under active development(because of 1.)
no Windows support(at least for now)
Clang.jl
Use this package if the cpp library already has a C binding/wrapper.
- pros:
- provides an easy to use C-binding generator
- provides libclang-c utilities(it’s essentially a wrapper for libclang-c)
- cons:
- no cpp to c wrapper generator which means one still needs to manually write C wrappers
- the C-binding generator has some limitations
- wrapping C++ template function/class is a nightmare