# Jl\_init() causing segmentation fault

**URL:** <https://discourse.julialang.org/t/jl-init-causing-segmentation-fault/35940>\
**Category:** New to Julia\
**Created:** [March 13, 2020, 5:36pm UTC](https://discourse.julialang.org/t/jl-init-causing-segmentation-fault/35940 "2020-03-13T17:36:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndresGomez97](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@AndresGomez97](https://discourse.julialang.org/u/AndresGomez97)\
**Post date:** [March 13, 2020, 5:36pm UTC](https://discourse.julialang.org/t/jl-init-causing-segmentation-fault/35940/1 "2020-03-13T17:36:23Z")

</div>

Hi, I am new in Julia and I am trying to run the following code that embeds julia into c.

```julia
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <julia.h>

int main(){
  void *handle;
  void (*jl_init_with_image)(const char*, const char*);
  int (*jl_atexit_hook)(int);
  void (*jl_eval_string)(const char*);
  handle = dlopen("$JULIA_DIR/usr/lib/libjulia.so", RTLD_LAZY | RTLD_GLOBAL);
  if (!handle) {
      fprintf(stderr, "%s\n", dlerror());
      exit(EXIT_FAILURE);
  }

  *(void**)(&jl_init_with_image) = dlsym(handle, "jl_init_with_image");
  *(int**)(&jl_atexit_hook)= dlsym(handle, "jl_atexit_hook");
  *(void**)(&hl_eval_string) = dlsym(handle, "jl_eval_string");

  dlerror();

  jl_init_with_image("$JULIA_DIR/usr/bin","$JULIA_DIR/usr/lib/julia/sys.ji");
  jl_eval_string("print('foo')");
  jl_atexit_hook(0);

  return 0;
}

```

As you can see I am trying to embed it using dlopen to link libjulia shared object instead of doing it with gcc -L option because as I need to make it work with ROOT this is necessary (ROOT and Julia don’t like each other because of LLVM). When I run this program Segmentation fault occurs.

I am using Julia 1.3.0 from git and running this in amd64\_linux26 system.

Also using “gcc -Wall file.c -ldl -I’$JULIA\_DIR/usr/include/julia’ -o file -JULIA\_NUMBER\_THREADS=1 -fPIC” for compiling it.

I’ve tried to use jl\_init() but it also causes segmentation fault.

Maybe I am not understanding the parameters I need to pass to jl\_init() or jl\_init\_with\_image(), but in both cases I am getting a segmentation fault and I don’t know why so some help would be great.

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [March 13, 2020, 6:30pm UTC](https://discourse.julialang.org/t/jl-init-causing-segmentation-fault/35940/2 "2020-03-13T18:30:42Z")

</div>

Can you check the `jl_init_with_image` pointer? On my system that is coming back null…however I **can** get a pointer to `jl_get_default_sysimg_path` can call that…

```julia
  printf("jl_init_with_image: %p\n", (void*)jl_init_with_image);
  printf("jl_atexit_hook: %p\n", (void*)jl_atexit_hook);
  printf("jl_eval_string: %p\n", (void*)jl_eval_string);

```

For which I get:

```julia
jl_init_with_image: (nil)
jl_atexit_hook: 0x7fe7dada8370
jl_eval_string: 0x7fe7daddfd80

```

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [March 13, 2020, 6:34pm UTC](https://discourse.julialang.org/t/jl-init-causing-segmentation-fault/35940/3 "2020-03-13T18:34:08Z")

</div>

Use `jl_init_with_image__threading` instead. That function exists…At least in Julia 1.3.1.

Here this might be easier, this is the file that **I** got working…I put julia in /tmp/…

```julia
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <julia.h>

int main(){
  void *handle;
  void (*jl_init_with_image)(const char*, const char*);
  int (*jl_atexit_hook)(int);
  void (*jl_eval_string)(const char*);
  char *(*jl_get_default_sysimg_path)();
  void (*jl_init)();

  handle = dlopen("/tmp/julia-1.3.1/lib/libjulia.so", RTLD_LAZY | RTLD_GLOBAL);
  if (!handle) {
      fprintf(stderr, "%s\n", dlerror());
      exit(EXIT_FAILURE);
  }

  *(&jl_init_with_image) = dlsym(handle, "jl_init_with_image__threading");
  *(&jl_atexit_hook)= dlsym(handle, "jl_atexit_hook");
  *(&jl_eval_string) = dlsym(handle, "jl_eval_string");
  *(&jl_get_default_sysimg_path) = dlsym(handle, "jl_get_default_sysimg_path");
  *(&jl_init) = dlsym(handle, "jl_init");

  dlerror();
  printf("jl_init_with_image: %p\n", (void*)jl_init_with_image);
  printf("jl_atexit_hook: %p\n", (void*)jl_atexit_hook);
  printf("jl_eval_string: %p\n", (void*)jl_eval_string);
  printf("default sysimg path: %s\n", jl_get_default_sysimg_path());

  jl_init_with_image("/tmp/julia-1.3.1/bin", jl_get_default_sysimg_path());
  jl_eval_string("print('foo')");
  jl_atexit_hook(0);
  return 0;
}

```

---

<div class="post-metadata">

**Author:** ![AndresGomez97](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@AndresGomez97](https://discourse.julialang.org/u/AndresGomez97)\
**Post date:** [March 13, 2020, 6:37pm UTC](https://discourse.julialang.org/t/jl-init-causing-segmentation-fault/35940/4 "2020-03-13T18:37:14Z")

</div>

It seems like that was the problem. Neither jl\_init() or jl\_init\_wiht\_image() methods exist.

But jl\_init\_threading() and jl\_init\_with\_image\_\_threading() both exist.

Thanks a lot pixel 🙂

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [March 13, 2020, 6:39pm UTC](https://discourse.julialang.org/t/jl-init-causing-segmentation-fault/35940/5 "2020-03-13T18:39:10Z")

</div>

I noticed the:

```julia
#define jl_init jl_init__threading
#define jl_init_with_image jl_init_with_image__threading

```

lines in the julia.h header when I was searching for the jl\_init functions. So dumb luck 🙂

---

<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:** [March 13, 2020, 7:02pm UTC](https://discourse.julialang.org/t/jl-init-causing-segmentation-fault/35940/6 "2020-03-13T19:02:14Z")

</div>

> [@AndresGomez97](#):
>
> Hi, I am new in Julia and I am trying to run the following code that embeds julia into c.

Be aware that embedding Julia is considered an advanced topic. This may change in the future but for now I wouldn’t really recommend it unless you are experienced in at least one of Julia and C, preferably both. Not saying that it’s impossible otherwise but expect it to be very challenging.

> [@AndresGomez97](#):
>
> As you can see I am trying to embed it using dlopen to link libjulia shared object

This is definitely possible but not very well documented. The best information on the subject can be found here: [https://github.com/JuliaLang/julia/pull/28886/files](https://github.com/JuliaLang/julia/pull/28886/files).

---

<div class="post-metadata">

**Author:** ![AndresGomez97](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@AndresGomez97](https://discourse.julialang.org/u/AndresGomez97)\
**Post date:** [March 13, 2020, 7:16pm UTC](https://discourse.julialang.org/t/jl-init-causing-segmentation-fault/35940/7 "2020-03-13T19:16:12Z")

</div>

Yes I know, I am not that skilled in neither Julia or C and I can tell that it is challenging but thanks to Julia community and forums, besides documentation might not be that well done for this kind of things, helped me a lot to progress.
