# Embedding Julia code in C

**URL:** <https://discourse.julialang.org/t/embedding-julia-code-in-c/98507>\
**Category:** General Usage\
**Tags:** embedding, c\
**Created:** [May 8, 2023, 9:23pm UTC](https://discourse.julialang.org/t/embedding-julia-code-in-c/98507 "2023-05-08T21:23:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mb96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mb96/32/45786_2.png) [@mb96](https://discourse.julialang.org/u/mb96)\
**Post date:** [May 8, 2023, 9:23pm UTC](https://discourse.julialang.org/t/embedding-julia-code-in-c/98507/1 "2023-05-08T21:23:23Z")

</div>

Hi there,

I am trying to compile a simple C program that calls a Julia base function. However, it is not compiling. The C code reads as

“”"  
#include \<stdio.h\>  
#include \<julia.h\>

int main(){

```
jl_init();

int index_vec[5] = {1,2,3,4,5};
int i;
for(i = 0; i<5; i++){
    jl_eval_string("print(sqrt(2.0))");
}
jl_atexit_hook(0);
return 0;

```

}  
“”"

Then to compile I run the following command on the CLI

gcc -o test -fPIC -I/Applications/Julia-1.7.app/Contents/Resources/julia/include/julia -L/Applications/Julia-1.7.app/Contents/Resources/julia/lib test\_C.c -ljulia

which is what I’ve seen reccomended in various places. This is the error I get:

 ![Captura de Pantalla 2023-05-08 a la(s) 17.22.43](https://global.discourse-cdn.com/julialang/original/3X/9/8/98207c8a9849a8bff78dcd2743b85d00b25bb62a.png)

Could I get some guidance on how to make this work please?

Thanks in advance,  
Miguel.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [May 8, 2023, 9:50pm UTC](https://discourse.julialang.org/t/embedding-julia-code-in-c/98507/2 "2023-05-08T21:50:51Z")

</div>

I could compile your program (and the slightly different from the docs, with JULIA\_DEFINE\_FAST\_TLS) with any of these on Linux, and a bit different gcc line from the docs:

```julia
$ JULIA_DIR=/home/pharaldsson/.julia/juliaup/julia-1.9.0-rc1+0.x64.linux.gnu/
$ JULIA_DIR=/home/pharaldsson/.julia/juliaup/julia-1.8.0+0.x64.linux.gnu
$ JULIA_DIR=/home/pharaldsson/.julia/juliaup/julia-1.7.3+0.x64.linux.gnu/
$ gcc -o test -fPIC -I$JULIA_DIR/include/julia -L$JULIA_DIR/lib -Wl,-rpath,$JULIA_DIR/lib test_jl2.c -ljulia

```

I’m not sure if you have a Mac problem or what, but use the latest 1.9.0 (which is out, not yet announced, or on the download page, e.g. rc3), or 1.8 (about to be decorated unsupported) or 1.6 LTS (or the unsupported 1.7, that should still work).

[https://docs.julialang.org/en/v1/manual/embedding/](https://docs.julialang.org/en/v1/manual/embedding/)

I googled your error and found (I’m not sure it’s relevant):

> <https://stackoverflow.com/questions/54517789/how-to-fix-the-ld-warning-ignoring-file-atome-error>

> If you interpret those bytes as ASCII, you get #include \<stdio.

It’s probably not relevant, but at least you don’t need `#include <stdio.h>` or at least it’s not in the example in the docs.

FYI: thee’s also his package for C++ (and another for Rust):

> **[GitHub - Clemapfel/jluna: Julia Wrapper for C++ with Focus on Safety,...](https://github.com/Clemapfel/jluna)**
>
> Julia Wrapper for C++ with Focus on Safety, Elegance, and Ease of Use - GitHub - Clemapfel/jluna: Julia Wrapper for C++ with Focus on Safety, Elegance, and Ease of Use

> jluna aims to fully wrap the official Julia C-API, replacing it in projects with C++ as the host language […]
> 
> - multidimensional, iterable array interface
> - provides a custom thread pool that, unlike the C-API, allows for concurrent interfacing with Julia

Can I ask what you want to do more, why embed, and why from C? The above package is for C++, but C++ is an (almost strict) superset of C, and it might be better to use that package from C++, even for the common C subset?

[I’ve already made a PR that was merged to doc existence of Julina in Julia’s official docs, so I’m unclear why it doesn’t show in 1.9.0 docs. I see it’s there for 1.10-DEV, and I’ve now ask for a backport of the docs.]

[https://docs.julialang.org/en/v1.10-dev/manual/embedding/#Embedding-Julia](https://docs.julialang.org/en/v1.10-dev/manual/embedding/#Embedding-Julia)

---

<div class="post-metadata">

**Author:** ![mb96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mb96/32/45786_2.png) [@mb96](https://discourse.julialang.org/u/mb96)\
**Post date:** [May 8, 2023, 10:34pm UTC](https://discourse.julialang.org/t/embedding-julia-code-in-c/98507/4 "2023-05-08T22:34:51Z")

</div>

Solved! Thanks so much! I downloaded juliaup and replicated your steps and it worked. Note that in my case I had to modify “julia-1.8.0+0.x64.linux.gnu” for example to an existing version in my “.julia/juliaup/” directory

in my case:  
julia-1.8.5+0.aarch64.apple.darwin14

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [May 8, 2023, 10:45pm UTC](https://discourse.julialang.org/t/embedding-julia-code-in-c/98507/5 "2023-05-08T22:45:15Z")

</div>

Regular julia installation should do. I just had juliaup handy. It’s excellent though, and maybe no reason to prefer not using it for embedding.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 9, 2023, 12:13am UTC](https://discourse.julialang.org/t/embedding-julia-code-in-c/98507/6 "2023-05-09T00:13:10Z")

</div>

I wonder if there is a good way to get the paths above. Maybe we need

> <https://github.com/JuliaLang/julia/issues/25326>
>
> As discussed in #20899, in addition to \`Sys.BINDIR\` (location of \`julia\` executa…ble) it would be good to have \`Sys.LIBDIR\` (installed location of \`libjulia\`), \`Sys.INCLUDEDIR\` (installed location of \`julia.h\`), and perhaps a few others (e.g. \`Sys.JULIA\` and \`Sys.LIBJULIA\` for the full paths of \`julia\` and \`libjulia\`, respectively).
