How to expose a C++ library to julia

question

#1

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").


#2

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

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


#3

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


#4

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.


#5

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


#6

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"

#7

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.


#8

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.


#9

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:


#10

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