# AOT compiling using PackageCompiler

**URL:** https://discourse.julialang.org/t/aot-compiling-using-packagecompiler/16911
**Category:** New to Julia
**Created:** [October 29, 2018, 5:24am UTC](https://discourse.julialang.org/t/aot-compiling-using-packagecompiler/16911 "2018-10-29T05:24:23Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![atombear](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atombear/32/8488_2.png) [@atombear](https://discourse.julialang.org/u/atombear)
#### Post date: [October 29, 2018, 5:24am UTC](https://discourse.julialang.org/t/aot-compiling-using-packagecompiler/16911/1 "2018-10-29T05:24:23Z")

</div>

Hello, I’m enchanted with the idea of bringing performant julia code into a python script through a shared library, especially if it as easy as PackageCompiler seems to imply. I am almost entirely unfamiliar with compiled languages, so much of this feels new to me.

I got a simple script to work, but now the problem I am having is with the following seemingly simple script:

```julia
test.jl

Base.@ccallable function func0()::Cdouble
    return sum([1.34, .7])
end

Base.@ccallable function func1()::Cdouble
    return (1.2 + .1) / 2.0
end

```

I compile the file to a shared library with `julia juliac.jl -vas test.jl`

Calling `func1` from python works fine, but `func0` causes a segfault. I have this same problem calling any imported function, and I don’t understand why. I imagine I am misunderstanding something fundamental.

Thank you in advance!

---

<div class="post-metadata">

### Author: ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)
#### Post date: [October 29, 2018, 10:19am UTC](https://discourse.julialang.org/t/aot-compiling-using-packagecompiler/16911/2 "2018-10-29T10:19:36Z")

</div>

I think `@ccallable` functions might need to use valid C, so both the square brackets and sum() could be trouble

From the compiling an executable section

> ```julia
> Base.@ccallable function julia_main(ARGS::Vector{String})::Cint
> hello_main(ARGS) # call your program's logic. ( using julia code )
> return 0
> end
> 
> ```

```julia

```

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [October 29, 2018, 11:22am UTC](https://discourse.julialang.org/t/aot-compiling-using-packagecompiler/16911/3 "2018-10-29T11:22:22Z")

</div>

> [@y4lu](#):
>
> I think `@ccallable` functions might need to use valid C,

I’m not sure what’s causing the segfault, but this is certainly not the case. There’s no restriction to the type of Julia code you can use (at least if you don’t explicitly disable the JIT compiler when you call `julia_init` in your driver program).

Have you tried `build_executable` with `func0` as the `julia_main` function? That should definitely work, and the C program it produces should give you something to pattern match if you’re trying to build a shared library with multiple `ccallable` functions.

---

<div class="post-metadata">

### Author: ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)
#### Post date: [October 29, 2018, 12:05pm UTC](https://discourse.julialang.org/t/aot-compiling-using-packagecompiler/16911/4 "2018-10-29T12:05:25Z")

</div>

Did you ever initialize the Julia runtime (IIRC via `julia_init)`? If not, it may be possible that func1 only runs because it doesn’t need to invoke the compiler, but func0 does, which will fail because Julia wasn’t initialized.

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [October 29, 2018, 1:08pm UTC](https://discourse.julialang.org/t/aot-compiling-using-packagecompiler/16911/5 "2018-10-29T13:08:10Z")

</div>

I find correctly initializing it is a bit tricky. But it works for me:

```python
from pathlib import Path
import ctypes

libpath = Path( __file__ ).resolve().parent.joinpath("builddir", "test.so")
dll = ctypes.CDLL(libpath, ctypes.RTLD_GLOBAL)
dll.func0.restype = ctypes.c_double
dll.func1.restype = ctypes.c_double

try:
    jl_init_with_image = dll.jl_init_with_image
except AttributeError:
    jl_init_with_image = dll.jl_init_with_image__threading
jl_init_with_image.argtypes = [ctypes.c_char_p, ctypes.c_char_p]

if __name__ == " __main__":
    jl_init_with_image(None, str(libpath).encode("utf-8"))
    print("func1 =", dll.func1())
    print("func0 =", dll.func0())

```

---

<div class="post-metadata">

### Author: ![atombear](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atombear/32/8488_2.png) [@atombear](https://discourse.julialang.org/u/atombear)
#### Post date: [October 29, 2018, 3:07pm UTC](https://discourse.julialang.org/t/aot-compiling-using-packagecompiler/16911/6 "2018-10-29T15:07:54Z")

</div>

this works, thank you! i will now have to understand exactly what you did!

---

<div class="post-metadata">

### Author: ![jocklawrie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jocklawrie/32/33616_2.png) [@jocklawrie](https://discourse.julialang.org/u/jocklawrie)
#### Post date: [October 30, 2018, 12:51am UTC](https://discourse.julialang.org/t/aot-compiling-using-packagecompiler/16911/7 "2018-10-30T00:51:49Z")

</div>

Are you able to post a complete example (Julia and Python code)?  
I’m having a lot of trouble getting this to work on Linux.

E.g., the first error I get says that juliac.jl needs to `Pkg.add("ArgParse")`, which suggests we are starting from different states.

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [October 30, 2018, 1:14am UTC](https://discourse.julialang.org/t/aot-compiling-using-packagecompiler/16911/8 "2018-10-30T01:14:25Z")

</div>

Here is a (hopefully) reproducible example:

[https://gist.github.com/7a1b24ae13d01f417d616293db164b31](https://gist.github.com/7a1b24ae13d01f417d616293db164b31)

Check out the repository and run `make`.

But you guys should be using [PyJulia](https://github.com/JuliaPy/pyjulia) 😉

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [October 30, 2018, 1:24am UTC](https://discourse.julialang.org/t/aot-compiling-using-packagecompiler/16911/9 "2018-10-30T01:24:54Z")

</div>

Oops I forgot to add the Makefile (it’s added now).

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [October 30, 2018, 2:35am UTC](https://discourse.julialang.org/t/aot-compiling-using-packagecompiler/16911/10 "2018-10-30T02:35:56Z")

</div>

Note by the way that without a snoopfile (see `build_executable` call in the [usage example](https://github.com/JuliaLang/PackageCompiler.jl#usage-example)), the functions are still compiled the first time they’re called from C or Python with given input types, as opposed to having their native code (for a finite set of input type tuples) incorporated into the system image.

---

<div class="post-metadata">

### Author: ![jocklawrie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jocklawrie/32/33616_2.png) [@jocklawrie](https://discourse.julialang.org/u/jocklawrie)
#### Post date: [October 30, 2018, 9:05pm UTC](https://discourse.julialang.org/t/aot-compiling-using-packagecompiler/16911/11 "2018-10-30T21:05:47Z")

</div>

Thanks @tkf that’s most helpful. I have this working.

My next challenge is to mimic `test.py` in Julia (call the library functions from Julia).

The use case is a set of users who each request a batch computation of about 30 mins running time. Each user has some idiosyncratic code that lives in its own library, so I only have to load the library for the requesting user rather than load the code for all users.

Here’s what I have so far (posted [here](https://github.com/JuliaLang/PackageCompiler.jl/issues/141), apologies for the double post):

```julia
using Libdl
test = Libdl.dlopen("test.so")
func0 = Libdl.dlsym(test, :func0) # Returns a Ptr(Nothing)
func0() # segfault

```

I’m not initializing the runtime at all, and `test` doesn’t have a `jl_init_with_image` handle exposed. Any idea how to get this going?

---

<div class="post-metadata">

### Author: ![jocklawrie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jocklawrie/32/33616_2.png) [@jocklawrie](https://discourse.julialang.org/u/jocklawrie)
#### Post date: [October 30, 2018, 9:08pm UTC](https://discourse.julialang.org/t/aot-compiling-using-packagecompiler/16911/12 "2018-10-30T21:08:56Z")

</div>

Ah ok.

For my own inner peace, does this mean that the compiler will compile all functions that are called from a given snoopfile (for the arg signature actually called), including those called indirectly as dependencies?

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [October 30, 2018, 10:14pm UTC](https://discourse.julialang.org/t/aot-compiling-using-packagecompiler/16911/13 "2018-10-30T22:14:46Z")

</div>

The translation of the Python script (without `jl_init_with_image`) would be something like

```julia
test = Libdl.dlopen("test.so", RTLD_GLOBAL)
func0 = Libdl.dlsym(test, :func0)
ccall(func0, Cdouble, ())

```

but it segfaults too.

The shared library `test.so` links to libjulia and (I think) full Julia runtime is available. So why don’t you load the Julia code directly?

---

<div class="post-metadata">

### Author: ![jocklawrie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jocklawrie/32/33616_2.png) [@jocklawrie](https://discourse.julialang.org/u/jocklawrie)
#### Post date: [October 30, 2018, 11:47pm UTC](https://discourse.julialang.org/t/aot-compiling-using-packagecompiler/16911/14 "2018-10-30T23:47:53Z")

</div>

The aim is to load a module conditional on the script input. E.g., if `ARGS[1] == x` load module A, else load module B.

Currently I have to load all modules. A shared library would enable conitional loading.

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [October 31, 2018, 12:05am UTC](https://discourse.julialang.org/t/aot-compiling-using-packagecompiler/16911/15 "2018-10-31T00:05:34Z")

</div>

Why not something like `@eval using $(Symbol(ARGS[1]))`?
