# API reference for Julia Embedding in C

**URL:** https://discourse.julialang.org/t/api-reference-for-julia-embedding-in-c/3963
**Category:** General Usage
**Tags:** question, embedding
**Created:** [May 28, 2017, 1:50pm UTC](https://discourse.julialang.org/t/api-reference-for-julia-embedding-in-c/3963 "2017-05-28T13:50:46Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![tastelessjolt](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@tastelessjolt](https://discourse.julialang.org/u/tastelessjolt)
#### Post date: [May 28, 2017, 1:50pm UTC](https://discourse.julialang.org/t/api-reference-for-julia-embedding-in-c/3963/1 "2017-05-28T13:50:46Z")

</div>

Is there any official API reference for Julia Embedding in C, or should we be understanding by going through the code or asking here?  
And specifically,

- How to create and access the data in the tuple?
- How to convert a pointer to an array(not 1D) without copying i.e. how to use ‘jl\_ptr\_to\_array’
- What should I pass the ‘dims’ argument? Should it be a tuple or svec or 1D array?

Thank you.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [May 28, 2017, 2:52pm UTC](https://discourse.julialang.org/t/api-reference-for-julia-embedding-in-c/3963/2 "2017-05-28T14:52:07Z")

</div>

> [@tastelessjolt](#):
>
> How to create and access the data in the tuple?

Same as other struct. Declare the right C struct and cast the pointer, or compute the offset manually, or use the getfield function.

> [@tastelessjolt](#):
>
> What should I pass the ‘dims’ argument? Should it be a tuple or svec or 1D array?

Tuple.

---

<div class="post-metadata">

### Author: ![tastelessjolt](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@tastelessjolt](https://discourse.julialang.org/u/tastelessjolt)
#### Post date: [May 29, 2017, 7:14am UTC](https://discourse.julialang.org/t/api-reference-for-julia-embedding-in-c/3963/3 "2017-05-29T07:14:48Z")

</div>

> [@yuyichao](#):
>
> Same as other struct

Like? I need an example. Suppose you need to create (3, 2) Integer Tuple for the ‘dims’ argument of ‘jl\_ptr\_to\_array’. I’m very new here.  
Thank you.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [May 29, 2017, 12:47pm UTC](https://discourse.julialang.org/t/api-reference-for-julia-embedding-in-c/3963/4 "2017-05-29T12:47:53Z")

</div>

```julia
jl_value_t *types[] = {(jl_value_t*)jl_long_type, (jl_value_t*)jl_long_type};
jl_tupletype_t *tt = jl_apply_tuple_type_v(types, 2);
typedef struct {
    ssize_t a;
    ssize_t b;
} ntuple2int;
ntuple2int *tuple = (ntuple2int*)jl_new_struct_uninit(tt);
JL_GC_PUSH1(&tuple);
tuple->a = 3;
tuple->b = 2;
jl_array_t *ary = jl_ptr_to_array(atype, data, (jl_value_t*)tuple, own_buffer);
JL_GC_POP();
return ary;

```

---

<div class="post-metadata">

### Author: ![tastelessjolt](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@tastelessjolt](https://discourse.julialang.org/u/tastelessjolt)
#### Post date: [June 1, 2017, 10:31am UTC](https://discourse.julialang.org/t/api-reference-for-julia-embedding-in-c/3963/5 "2017-06-01T10:31:53Z")

</div>

Thank you so much.

---

<div class="post-metadata">

### Author: ![tastelessjolt](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@tastelessjolt](https://discourse.julialang.org/u/tastelessjolt)
#### Post date: [June 3, 2017, 1:40pm UTC](https://discourse.julialang.org/t/api-reference-for-julia-embedding-in-c/3963/6 "2017-06-03T13:40:57Z")

</div>

Btw, what about the other question that,  
Documentation about Embedding Julia more than what’s there at [Embedding Julia](https://docs.julialang.org/en/release-0.5/manual/embedding/)?

Thank you.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [June 3, 2017, 5:08pm UTC](https://discourse.julialang.org/t/api-reference-for-julia-embedding-in-c/3963/7 "2017-06-03T17:08:48Z")

</div>

In general, the recommended approach is to use the ones described in the doc to evaluate julia code and define your own API. There are other direct runtime interfaces available although those breaks much more often and are not really well documented other than the header itself.

---

<div class="post-metadata">

### Author: ![tastelessjolt](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@tastelessjolt](https://discourse.julialang.org/u/tastelessjolt)
#### Post date: [June 12, 2017, 12:16am UTC](https://discourse.julialang.org/t/api-reference-for-julia-embedding-in-c/3963/8 "2017-06-12T00:16:35Z")

</div>

I’m now unable to create n length tuple.

`typedef struct { ssize_t a[2]; } ntuple2int;`  
where 2 can be any number.  
I tried this but this didn’t work.

How to create a ‘n’ length tuple then?

Thank you

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [June 12, 2017, 1:26am UTC](https://discourse.julialang.org/t/api-reference-for-julia-embedding-in-c/3963/9 "2017-06-12T01:26:11Z")

</div>

So assume you don’t have any problem creating the type (unless you have a too big type you can just create a big enough buffer to pass in the tuple element types). With that you shouldn’t have any problem allocating the object either so I assume you only have problem with accessing elements.

C does not allow unknown type during compile time so you cannot declare the corresponding C type unless you know it at C code compile time in general. In this particular case though, the three methods I mentioned in the first post still all apply.

1. You can use the getfield function. It’s accessible in C as `jl_get_field`. It should be pretty obvious to use. This does not need to rely on any C functions in your code, all offset calculation is down in julia runtime, it’s also the slowest.
2. You can do offset computation yourself. You can easily predict the layout of the object with `n` fields. You can just do the offset computation and loads yourself. For `NTuple` it’s simple enough that you can just cast the pointer to `ssize_t*` and index it.
3. It turns out that this is a very special case where it is actually possible to declare a compatible C struct. This require C99 and you can do it as `typedef struct { ssize_t a[]; } ntupleint;`

---

<div class="post-metadata">

### Author: ![tastelessjolt](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@tastelessjolt](https://discourse.julialang.org/u/tastelessjolt)
#### Post date: [June 20, 2017, 12:10am UTC](https://discourse.julialang.org/t/api-reference-for-julia-embedding-in-c/3963/10 "2017-06-20T00:10:47Z")

</div>

Thanks for the answer. I was able to create an NTuple and populate it.

---

<div class="post-metadata">

### Author: ![dmitry-kabanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmitry-kabanov/32/52639_2.png) [@dmitry-kabanov](https://discourse.julialang.org/u/dmitry-kabanov)
#### Post date: [April 16, 2024, 3:41pm UTC](https://discourse.julialang.org/t/api-reference-for-julia-embedding-in-c/3963/11 "2024-04-16T15:41:47Z")

</div>

It’s April 2024, and this information is still not on the Embedding Julia page 🙂 It is pretty exciting wasting working hours to learn how to use the Julia C API by looking at the source code.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [April 16, 2024, 5:04pm UTC](https://discourse.julialang.org/t/api-reference-for-julia-embedding-in-c/3963/12 "2024-04-16T17:04:42Z")

</div>

My advice is to keep the C API exposure to the minimum you need to set up `@cfunction` pointers and use those to interface between C and Julia. As a bonus this allows you to do things like [dynamically loading libjulia](https://github.com/GunnarFarneback/DynamicallyLoadedEmbedding.jl).

---

<div class="post-metadata">

### Author: ![dmitry-kabanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmitry-kabanov/32/52639_2.png) [@dmitry-kabanov](https://discourse.julialang.org/u/dmitry-kabanov)
#### Post date: [April 16, 2024, 7:55pm UTC](https://discourse.julialang.org/t/api-reference-for-julia-embedding-in-c/3963/13 "2024-04-16T19:55:45Z")

</div>

Thanks, Gunnar, yes, I’ve tried `@cfunction` last week. Indeed, it is easier to use. However, my use case is that the called functions are determined in runtime, and then I need to write a lot of code to generate `@cfunction` signature as strings and then use `libffi` to invoke the resultant C functions.

This is much more complicated in this particular case, then to use `jl_call` directly. And this approach is much closer to the Python C API, that I have already used. The problem is that the documentation for Julia C API is quite minimalistic, while the code is pretty advanced and lacks docstrings.

For me, `libjulia` is already linked dynamically in some sense, as the C component in my code that embeds Julia is loaded dynamically via `dlopen`, so it is also not a problem.

Thank you also for the linke to the repo, I’ll have a look and hopefully understand how to use Julia C API better!

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [April 16, 2024, 8:05pm UTC](https://discourse.julialang.org/t/api-reference-for-julia-embedding-in-c/3963/14 "2024-04-16T20:05:21Z")

</div>

I think you can mostly find how to avoid using the Julia C API there. 🙂

I don’t know your constraints but I would, if at all possible, generate the `@cfunction` signatures with Julia code and in general do as much as possible of the work on the Julia side.
