How to expose a C++ library to julia

I would like to use this C++ library from julia: https://github.com/ORNL/Tasmanian
there is a Python interface which I tried to use with PyCall but without success. I’m now wondering how hard it would be to wrap this as a julia package. I have seen both Cxx.jl and CxxWrap.jl, but somewhere someone mentioned that probably the easiest approach would be to add some EXTERN "C { } calls in the library. I don’t know that means. any hints and ideas much appreciated.

The build creates a shared library object. Is it possible to just link to that somehow? I’m thinking of how easy it is to do a ccall(:funcname,"library_name").

The “extern C” approach may already be done for that library:

https://github.com/ORNL/TASMANIAN/blob/ebaa87fe64b415b5d974eeafad962d43c998b7a3/InterfaceFortran/tsgC2Fortran.cpp#L48

That creates a C api for the C++ code. You should be able to use that with ccall.

1 Like

oh, great! so you would suggest to just use the methods from the fortran interface? thanks.

The easiest approach really depends on the library and the use case. If a C interface already exists, directly using ccall is probably the simplest approach. Glancing at the documentation, it looks like the Fortran interface is still experimental, but the Python interface (which looks more complete, also at first glance) also uses a set of C functions, so it may be worth looking into copying that approach. First thing to check is if the required functionality is available through one of the C interfaces, though. If you still need to write a lot of C wrapper functions yourself, then using CxxWrap.jl or Cxx.jl is probably faster once you get over the initial learning curve.

1 Like

It looks like there are two C interfaces. One for fortran and one for python:

https://github.com/ORNL/TASMANIAN/blob/ebaa87fe64b415b5d974eeafad962d43c998b7a3/SparseGrids/TasmanianSparseGrid.cpp#L1481

1 Like

awesome! thanks so much guys. seems to work!

julia> s=ccall((:tsgGetVersion, "/Applications/TSG/lib/libtasmaniansparsegrid.dylib"),Cstring,())
Cstring(0x000000012b831cb4)

julia> unsafe_string(s)
"6.0"
1 Like

just checking in to say thanks again: That was quite a breeze! I ended up copying the Python approach. I’ve got the basic functionality down I think.

https://github.com/floswald/Tasmanian.jl

2 Likes

Cool. You might also consider generating binaries for the Tasmanian libraries. BinaryBuilder is pretty new, but it works great. It looks like this is a CMake build, so it might be pretty easy. With pre-built libraries, it’ll be easier for users to install the package.

2 Likes

ha, that was a timely comment! I was just trying to understand how to do this with BinDeps. I’ll skip this then :slight_smile:

If you run into problems, your best bet is the bindeps2 channel on Slack. Or, you can ask here.

2 Likes