Trying PackageCompiler.jl lib example on Python

Hey there,

I am testing PackageCompiler with the examples in the repo. I am able to compile the library correctly following the documentation but then when I try to use it in Python with the following code I just get

segmentation fault  python test_lib.py
import ctypes
import os

print(os.getcwd())
mylib = ctypes.CDLL('MyLibCompiled/lib/libinc.so')
mylib.increment32.argtypes = [ctypes.c_int]
mylib.increment32.restype = ctypes.c_int
mylib.increment64.argtypes = [ctypes.c_long]
mylib.increment64.restype = ctypes.c_long
count32 = 10
result32 = mylib.increment32(count32)
print(f"Result of increment32: {result32}")

Debugging it, basically it crashes when calling the increment method.
Wondering if anyone could point me to debug what is going on.
Many thanks,
Eduard

You need to initilaize Julia:
https://docs.julialang.org/en/v1/manual/embedding/

Good point!
I am trying to initialize it with

import ctypes
import os
from ctypes import c_void_p, RTLD_GLOBAL

libjulia = ctypes.CDLL('MyLibCompiled/lib/libjulia.so', RTLD_GLOBAL)
libjulia.jl_init()

however when doing jl_init() I get

ERROR: could not load library "MyLibCompiled/lib/../bin/../lib/julia/sys.so"
MyLibCompiled/lib/../bin/../lib/julia/sys.so: cannot open shared object file: No such file or directory

As the path is trimmed I cant really know what is happening but it seems is trying to find this file but it didnt get ocmpiled?

So you need to set jl_options with the location of the system image you just compiled.

Frankly, you should probably use the Python package juliacall to at least initialize Julia, then try to call the C function pointer.

Thanks again Mark! I would prefer to avoid using juliacall as I plan to call from other languages such as java. I found a libcg repo which successfully build and calls a librar From c and rust however i read that there are some libraries that don’t work well and that everything needs to be statically typed which limits the usage quite a bit :upside_down_face:

This is slightly easier to describe from either C or Zig than Python.

You can see the julia_init function the libcg function that libcg uses works:

Basically, we need to communicate to Julia the location of the system image. You can either populate the jl_options struct directly or use the command line option processor. The -J or --sysimage command line option indicates the system image location.

As I say this though, I suspect that the juliac tool is about to be reincarnated, which might provide a whole new pathway to do this.

The bottom line is that you must initialize the Julia runtime before invoking that C pointer.