# Extending julia with C or C++ like python?

**URL:** <https://discourse.julialang.org/t/extending-julia-with-c-or-c-like-python/92406>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 2, 2023, 6:44am UTC](https://discourse.julialang.org/t/extending-julia-with-c-or-c-like-python/92406 "2023-01-02T06:44:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yexiangyan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yexiangyan/32/46008_2.png) [@yexiangyan](https://discourse.julialang.org/u/yexiangyan)\
**Post date:** [January 2, 2023, 6:44am UTC](https://discourse.julialang.org/t/extending-julia-with-c-or-c-like-python/92406/1 "2023-01-02T06:44:22Z")

</div>

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:

```julia
#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!

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [January 2, 2023, 8:25am UTC](https://discourse.julialang.org/t/extending-julia-with-c-or-c-like-python/92406/2 "2023-01-02T08:25:24Z")

</div>

Well, you can call C functions directly from Julia. Have a look at the manual: [Calling C and Fortran Code · The Julia Language](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/)

C++ is more tricky. You could try to use [GitHub - JuliaInterop/CxxWrap.jl: Package to make C++ libraries available in Julia](https://github.com/JuliaInterop/CxxWrap.jl) .

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

---

<div class="post-metadata">

**Author:** ![yexiangyan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yexiangyan/32/46008_2.png) [@yexiangyan](https://discourse.julialang.org/u/yexiangyan)\
**Post date:** [January 2, 2023, 3:05pm UTC](https://discourse.julialang.org/t/extending-julia-with-c-or-c-like-python/92406/3 "2023-01-02T15:05:29Z")

</div>

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++.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 2, 2023, 3:14pm UTC](https://discourse.julialang.org/t/extending-julia-with-c-or-c-like-python/92406/4 "2023-01-02T15:14:07Z")

</div>

> [@yexiangyan](#):
>
> 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, basically, the library re-implemented [SWIG](https://swig.org/)? 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](https://github.com/JuliaInterop/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](https://github.com/JuliaPackaging/Yggdrasil) have been interfaced to Julia, and typically the interfaces are written as much as possible on the Julia side.
