Embedding Julia code in C

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(){


int index_vec[5] = {1,2,3,4,5};
int i;
for(i = 0; i<5; i++){
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:

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

Thanks in advance,

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_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).


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

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):

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


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:

1 Like

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.

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

1 Like