Extending julia with C or C++ like python?

Hello everyone.

Since I have a C++ library that already has a python interface, it works by the mechanism of getting the arguments passed from a python function and then doing a series of parsing jobs. Something like this for python:

#define PY_SSIZE_T_CLEAN
#include <Python.h>

static PyObject *
spam_system(PyObject *self, PyObject *args)
{
    const char *command;
    int sts;

    if (!PyArg_ParseTuple(args, "s", &command))
        return NULL;
    sts = system(command);
    return PyLong_FromLong(sts);
}

The args argument will be a pointer to a Python tuple object containing the arguments. Each item of the tuple corresponds to an argument in the call’s argument list.

Can I do the same in julia to get the arguments passed from the julia function?

Thanks!

Well, you can call C functions directly from Julia. Have a look at the manual: Calling C and Fortran Code · The Julia Language

C++ is more tricky. You could try to use GitHub - JuliaInterop/CxxWrap.jl: Package to make C++ libraries available in Julia .

I don’t know if an existing Python interface can help you if you use Julia.

3 Likes

Thanks, ufechner7,

I plan to add a julia wrapper to this C++ library. But all the functions of this C++ library have no specific arguments, it works by capturing the arguments passed from other languages and parsing them one by one to determine the other classes or functions to call. So, I have to get the arguments passed from julia’s functions and then parse them one by one in C++.

So, basically, the library re-implemented SWIG? You can certainly do this kind of introspection in Julia (take a tuple of arguments and then query one-by-one the types of each), but it’s probably a lot easier to use CxxWrap.jl to directly interface the C++ functions you want to call, and leave the dynamic dispatch to the Julia side.

A lot of C and C++ libraries have been interfaced to Julia, and typically the interfaces are written as much as possible on the Julia side.

1 Like