# Creating fully self-contained and pre-compiled library

**URL:** https://discourse.julialang.org/t/creating-fully-self-contained-and-pre-compiled-library/131374
**Category:** General Usage
**Created:** [August 5, 2025, 1:40pm UTC](https://discourse.julialang.org/t/creating-fully-self-contained-and-pre-compiled-library/131374 "2025-08-05T13:40:46Z")
**Posts on this page:** 1
**Showing post:** 36

<div class="post-metadata">

### Author: ![asprionj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asprionj/32/6856_2.png) [@asprionj](https://discourse.julialang.org/u/asprionj)
#### Post date: [November 10, 2025, 3:46pm UTC](https://discourse.julialang.org/t/creating-fully-self-contained-and-pre-compiled-library/131374/36 "2025-11-10T15:46:46Z")

</div>

Hi everyone, we’re now using JuliaC.jl to create a shared library, with all dependencies bundled – awesome! Thanks a lot to the folks making this possible!

Now, two points are still a bit tricky, and we did run into problems with (most probably) them.

First, **thread safety of the JuliaC-generated shared library**. When embedding julia, one has to take care of only using the `jl_init`-ing or julia-spawned threads for all further `jl_*` calls ([Embedding Julia · The Julia Language](https://docs.julialang.org/en/v1/manual/embedding/#Thread-safety)). How does this transfer over to a JuliaC-generate shared lib? Shall only a single thread load the library, and then handle all the calls to the `Base.@ccallable` functions? We’re getting segfaults after around 70 calls when we call from different threads, but none when calling from a single thread.

Is this documented somewhere and we just missed it?

Second, a **low-level detail on memory handling and the julia-side GC**. I first had something like this:

```julia
buf_size = io.size # io is an IOBuffer holding an encoded protobuf-message
out_ptr = Ptr{UInt8}(Libc.malloc(buf_size + 4))
unsafe_copyto!(out_ptr, pointer(vcat(sizeinfo_bytes, e.io.data[1:buf_size])), buf_size + 4)
return out_ptr

```

Now, my AI-coding assistant mentioned (when working on the first issue) that the julia GC might collect the temporary array created via `vcat`, while the copying-process was still ongoing. The proposed solution is:

```julia
payload = take!(e.io)
buf_size = length(payload)
out_ptr = Ptr{UInt8}(Libc.malloc(buf_size + 4))
# directly write size information
unsafe_store!(out_ptr, UInt8((buf_size >> 24) & 0xff))
unsafe_store!(out_ptr + 1, UInt8((buf_size >> 16) & 0xff))
unsafe_store!(out_ptr + 2, UInt8((buf_size >> 8) & 0xff))
unsafe_store!(out_ptr + 3, UInt8(buf_size & 0xff))
# Copy payload from Julia vector
GC.@preserve payload begin
    Base.unsafe_copyto!(out_ptr + 4, pointer(payload), buf_size)
end
return out_ptr

```

Is this actually “better”? Is it actually required to avoid any unsafe behaviour?

Thanks a lot in advance!

---

_[View the full topic](https://discourse.julialang.org/t/creating-fully-self-contained-and-pre-compiled-library/131374)._
