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

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:

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

Thanks in advance,
Miguel.

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

https://docs.julialang.org/en/v1/manual/embedding/

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

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

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

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