# Calling C functions

**URL:** <https://discourse.julialang.org/t/calling-c-functions/15004>\
**Category:** New to Julia\
**Created:** [September 15, 2018, 10:07pm UTC](https://discourse.julialang.org/t/calling-c-functions/15004 "2018-09-15T22:07:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![josemaria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josemaria/32/5365_2.png) [@josemaria](https://discourse.julialang.org/u/josemaria)\
**Post date:** [September 15, 2018, 10:07pm UTC](https://discourse.julialang.org/t/calling-c-functions/15004/1 "2018-09-15T22:07:40Z")

</div>

I am trying to wrap a C library (VapourSynth). It is the first time I try to do something like this, so I’m struggling a bit.

The wrapped API has something like this:

```julia
struct VSAPI
    createCore::Ptr{Cvoid}
    freeCore::Ptr{Cvoid}
    getCoreInfo::Ptr{Cvoid}
    ...
end

```

Where _createCore_, _freeCore_, _getCoreInfo_ are functions which are defined like this in the header file:

```nohighlight
struct VSAPI {
    VSCore *(VS_CC *createCore)(int threads) VS_NOEXCEPT;
    void (VS_CC *freeCore)(VSCore *core) VS_NOEXCEPT;
    const VSCoreInfo *(VS_CC *getCoreInfo)(VSCore *core) VS_NOEXCEPT;
    ...

```

Then I try to run something like this:

```julia
julia> api = ccall((:getVapourSynthAPI, "./vapoursynth/.libs/libvapoursynth.so"), Ptr{VSAPI}, (Cint,), 3)
Ptr{VSAPI} @0x00007f45f971a780

julia> api.createCore(0)
ERROR: type Ptr has no field createCore
Stacktrace:
 [1] top-level scope at none:0

```

which fails.

How could I call those functions within VSAPI?

---

<div class="post-metadata">

**Author:** ![josemaria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josemaria/32/5365_2.png) [@josemaria](https://discourse.julialang.org/u/josemaria)\
**Post date:** [September 16, 2018, 12:19pm UTC](https://discourse.julialang.org/t/calling-c-functions/15004/2 "2018-09-16T12:19:38Z")

</div>

In accordance with the documentation, _unsafe\_load_ gets an instance from the pointer returned by _ccall_:

```julia
julia> pointer = ccall((:getVapourSynthAPI, "./vapoursynth/.libs/libvapoursynth.so"), Ptr{VSAPI}, (Cint,), 3)
Ptr{VSAPI} @0x00007f45f971a780
julia> api = unsafe_load(pointer)
VSAPI(Ptr{Nothing} @0x00007f45f96d7650, Ptr{Nothing} @0x00007f45f96d7630, Ptr{Nothing} @0x00007f45f96d7620, Ptr{Nothing} @0x00007f45f96d77d0, Ptr{Nothing} @0x00007f45f96d7740, 
...
julia> typeof(api)
VSAPI
julia> api.createCore(0)
ERROR: MethodError: objects of type Ptr{Nothing} are not callable
Stacktrace:
 [1] top-level scope at none:0

```

So now I am able to get access to the functions within the instance, but they still don’t work.

Maybe the function type is not properly defined?

I have:

```julia
struct VSAPI
    createCore::Ptr{Cvoid}

```

where the header says:

```nohighlight
struct VSAPI {
    VSCore *(VS_CC *createCore)(int threads) VS_NOEXCEPT;

```

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [September 16, 2018, 12:22pm UTC](https://discourse.julialang.org/t/calling-c-functions/15004/3 "2018-09-16T12:22:09Z")

</div>

You cannot call the pointers to functions directly as if they were Julia functions. Julia has no idea what arguments this function takes, not what it’s return type. Instead, pass `api.createCore` as the first argument to another `ccall`.

---

<div class="post-metadata">

**Author:** ![josemaria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josemaria/32/5365_2.png) [@josemaria](https://discourse.julialang.org/u/josemaria)\
**Post date:** [September 16, 2018, 3:04pm UTC](https://discourse.julialang.org/t/calling-c-functions/15004/4 "2018-09-16T15:04:33Z")

</div>

Could you draft an example, so I can follow the pattern? (I’m not a programmer)

---

<div class="post-metadata">

**Author:** ![josemaria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josemaria/32/5365_2.png) [@josemaria](https://discourse.julialang.org/u/josemaria)\
**Post date:** [September 16, 2018, 4:01pm UTC](https://discourse.julialang.org/t/calling-c-functions/15004/5 "2018-09-16T16:01:28Z")

</div>

Understood!  
I did:

```julia
julia> abstract type VSCORE
       end
julia> core = ccall(api.createCore, Ptr{VSCORE}, (Cint,), 0)
Ptr{VSCORE} @0x000055f18e0252b0

```

Thanks as lot!
