Best way to create a julia function wrapper of a cpp function

Hi,

what is the best way to create a julia function wrapper of a C++ function. I started with the Cxx package and just put the C++ code in cxx"“” but it is quite slow. Now I´m considering to use the CxxWrap package. Is it the currently method, which leads to the best performance of embedding C++ code to julia? Or is there something else what is better?

1 Like

Is there a difference between calling a function written in C++ or in C?
If not somebody posted this yesterday:

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:
    1. relatively mature and full-featured
    2. under active development
    3. easy to wrap C++ template function/class
  • cons:
    1. no wrapper generator
    2. need to learn and use a “glue” language/API
    3. 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:
    1. has a native and easy to use C++ REPL
    2. Julia and C++ code are fully integrated
    3. easy to wrap C++ template function/class
  • cons:
    1. “magic”(hard to understand what happens under the hood)
    2. the package is not under active development(because of 1.)
    3. no Windows support(at least for now)

Clang.jl

Use this package if the cpp library already has a C binding/wrapper.

  • pros:
    1. provides an easy to use C-binding generator
    2. provides libclang-c utilities(it’s essentially a wrapper for libclang-c)
  • cons:
    1. no cpp to c wrapper generator which means one still needs to manually write C wrappers
    2. the C-binding generator has some limitations
    3. wrapping C++ template function/class is a nightmare
8 Likes

Thank you very much for this overview. In my case I have to wrap just 3 functions, which are in total approx. 200 lines of code. So

will be my preference.

Hi again,

I tried to follow your advise (explained in the quote). I found this little example Julia calling C: A minimal example | Perfectionatic's blog and followed the instructions. I can also build the library but when I use the command

using Libdl
push!(Libdl.DL_LOAD_PATH,"C:/Users/xxx/Downloads/lib_mean/src/libmean.so")

x=ccall((:mean,"libmean"),Float64,(Float64,Float64),2.0,5.0)
println(x)

I got an error "error compiling top-level scope: could not load library “libmean” "

Do you know what I have done wrong? I´m working on Windows 10, Julia 1.1.0

you need to recompile that lib using MSVC or cross-compile it using mingw.

Thanks for your quick answer. I used mingw calling mingw32-make. The problem was that I had to call the libary with the extension .so

x=ccall((:mean,"libmean"),Float64,(Float64,Float64),2.0,5.0)

not sure what you were asking here. it looks like you’re trying to open an ELF file on Windows?

file libmean.so

My problem is solved due to calling

x=ccall((:mean,"libmean.so"),Float64,(Float64,Float64),2.0,5.0)

instead of

x=ccall((:mean,"libmean"),Float64,(Float64,Float64),2.0,5.0)

That is what I tried to explain with the following sentence

1 Like

sorry for the misunderstanding :slight_smile: