# Compiling and building binaries from your julia code

**URL:** <https://discourse.julialang.org/t/compiling-and-building-binaries-from-your-julia-code/5687>\
**Category:** General Usage\
**Tags:** compilation\
**Created:** [September 3, 2017, 12:47am UTC](https://discourse.julialang.org/t/compiling-and-building-binaries-from-your-julia-code/5687 "2017-09-03T00:47:53Z")\
**Posts on this page:** 10\
**Page:** 3

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [November 29, 2017, 3:24pm UTC](https://discourse.julialang.org/t/compiling-and-building-binaries-from-your-julia-code/5687/41 "2017-11-29T15:24:47Z")

</div>

I’m also interested in real-time environments (for live audio processing), but it’s a tough nut to crack.

Even if your code is compiled statically it doesn’t change the memory allocation/GC model, so the code will still not be real-time safe. If you can get away with “mostly safe” then you can get pretty far by avoiding as much heap allocation as possible so the GC doesn’t have much to do.

In ages past there was some exciting work towards making the GC incremental with a time-bounded pause times, but I think it was never ready for prime time and didn’t get merged.

---

<div class="post-metadata">

**Author:** ![airpmb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/airpmb/32/7826_2.png) [@airpmb](https://discourse.julialang.org/u/airpmb)\
**Post date:** [November 29, 2017, 3:47pm UTC](https://discourse.julialang.org/t/compiling-and-building-binaries-from-your-julia-code/5687/42 "2017-11-29T15:47:35Z")

</div>

I think I could get away with “mostly safe”, certainly if there was a path towards stronger safety guarantees down the road. I haven’t read anything about Julia yet that indicates that it shouldn’t be possible in principle to have some subset of Julia programs that could be real time safe, in some future hypothetical version of Julia.

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [November 29, 2017, 3:48pm UTC](https://discourse.julialang.org/t/compiling-and-building-binaries-from-your-julia-code/5687/43 "2017-11-29T15:48:52Z")

</div>

I also noticed that it fails when using some packages (e.g. Gtk compiles but segfault, Cairo doesn’t compile, …) do we know the potentials reasons why some packages fail ? I guess calling external libraries in particular can go wrong in several ways. Or in other words if I want to make Cairo compile-compatible, what should I look for ?

---

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [November 29, 2017, 4:01pm UTC](https://discourse.julialang.org/t/compiling-and-building-binaries-from-your-julia-code/5687/44 "2017-11-29T16:01:30Z")

</div>

> [@airpmb](#):
>
> I haven’t read anything about Julia yet that indicates that it shouldn’t be possible in principle to have some subset of Julia programs that could be real time safe, in some future hypothetical version of Julia.

I agree, and there have even been recent changes that allow more things to be stack-allocated, further reducing the GC pressure.

---

<div class="post-metadata">

**Author:** ![viralbshah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viralbshah/32/54_2.png) [@viralbshah](https://discourse.julialang.org/u/viralbshah)\
**Post date:** [November 29, 2017, 11:10pm UTC](https://discourse.julialang.org/t/compiling-and-building-binaries-from-your-julia-code/5687/45 "2017-11-29T23:10:55Z")

</div>

It will have lesser startup overhead, but for eliminating most of the startup overhead, we will need something like Tim Holy’s SnoopCompile. Jameson recently fixed it to work on 0.6/0.7 - so that would be the thing to further investigate. Completely dynamic things will still invoke the jit, but the goal post 1.0 should be to have tools that help understand these things and simplify them.

GC will be present. The much longer term plan should be to have either a DSL or a restricted subset of Julia for real-time controllers. I think it would be a great research project at a university, or for someone with the right skill and enough time to put into something like this. There have been many discussions (offline), but nothing that has been written up.

Threading in Julia is still early, but it should work in these binaries as expected. Same for distributed computing stuff. It will be a bit more work to build further tooling to make all this easy - but if it doesn’t work, it would be good to get issues on the static-julia repo.

-viral

---

<div class="post-metadata">

**Author:** ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)\
**Post date:** [November 30, 2017, 8:11am UTC](https://discourse.julialang.org/t/compiling-and-building-binaries-from-your-julia-code/5687/46 "2017-11-30T08:11:40Z")

</div>

> [@viralbshah](#):
>
> Completely dynamic things will still invoke the jit, but the goal post 1.0 should be to have tools that help understand these things and simplify them.

Ability to deploy a package binary at some point is very important, as well as an ability to write Julia in such a way that all inferences about the code structure/types can be made at compile time. The latter will also enable the same or better experience that users of “Intellisense” and C# enjoy. Please please make sure that everything needed will be baked in 1.0 🙂

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [December 1, 2017, 11:55am UTC](https://discourse.julialang.org/t/compiling-and-building-binaries-from-your-julia-code/5687/47 "2017-12-01T11:55:17Z")

</div>

I just recently put snoopcompile and the static-compilation script into one package: [https://github.com/SimonDanisch/PackageCompiler.jl](https://github.com/SimonDanisch/PackageCompiler.jl) - so that should make things already a bit easier.

@jonathanBieler

After making a fairly large package (Makie) and it’s dependencies precompilable, I came up with the following list:

- non const globals are problematic, or globals defined in functions - removing those got me to 90% of making the package save for static compilation

- type unstable code had some inference issues (around 2 occurrence, where I’m still not sure what was happening) - both cases happened with dictionaries… Only way to find those was investigating the segfaults with gdb, but then it was relatively easy to just juggle around the code a bit. - The non const globals might be related since they introduce type instabilities, not sure though…

- some generated functions needed reordering of the functions they call ( actually, even for normal code all functions that get called in a generated function should be defined before it)

- I uncovered one out of bounds issue, that somehow was not coming up without static-compilation

- I used julia-debug to uncover those bugs, but actually, the last errors I was trying to uncover where due to using julia-debug - so Makie just works correctly statically compiled with a release build right now!

- you’re pretty much on your own and need to use gdb to find the issues and I still don’t know what the underlying julia issues are and when they will get fixed 😉 See: [https://github.com/JuliaLang/julia/issues/24533](https://github.com/JuliaLang/julia/issues/24533)

---

<div class="post-metadata">

**Author:** ![Nitin\_Arora](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nitin_arora/32/1174_2.png) [@Nitin\_Arora](https://discourse.julialang.org/u/Nitin_Arora)\
**Post date:** [December 2, 2017, 12:46am UTC](https://discourse.julialang.org/t/compiling-and-building-binaries-from-your-julia-code/5687/48 "2017-12-02T00:46:48Z")

</div>

I am so excited to try this over the weekend. Thank you very much for this effort!

---

<div class="post-metadata">

**Author:** ![igor.cerovsky](https://avatars.discourse-cdn.com/v4/letter/i/c37758/32.png) [@igor.cerovsky](https://discourse.julialang.org/u/igor.cerovsky)\
**Post date:** [December 4, 2017, 2:34pm UTC](https://discourse.julialang.org/t/compiling-and-building-binaries-from-your-julia-code/5687/49 "2017-12-04T14:34:32Z")

</div>

The solution:

- use the mingw-w64 compiler from [http://mingw-w64.org/doku.php/download/mingw-builds](http://mingw-w64.org/doku.php/download/mingw-builds) with default settings
- set the PATH to `cc = is_windows() ? raw"c:\Program Files\mingw-w64\x86_64-7.2.0-win32-seh-rt_v5-rev1\mingw64\bin\x86_64-w64-mingw32-gcc.exe" : "gcc"` (I’ve used the absolute path purposely); or set system PATH
- copy all required `*.dll` files to the `hello.exe` folder from `c:\Program Files\mingw-w64\x86_64-7.2.0-win32-seh-rt_v5-rev1\mingw64\bin\` and `YOUR_JULIA_INSTALL\Julia-0.6.0\bin\`
- run the `hello.exe` in Unicode friendly console; I use Visual Studio Code

```julia
shell> d:/work/J-CPP/static-julia-master/builddir/hello.exe
hello, world
f() = 0.369760359689711
       ┌────────────────────────────────────────┐
   100 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠎│
       │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡰⠁⠀│
       │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠊⠀⠀⠀│
       │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠔⠁⠀⠀⠀⠀│
       │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠔⠁⠀⠀⠀⠀⠀⠀│
       │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⠊⠀⠀⠀⠀⠀⠀⠀⠀│
       │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
       │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
       │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠤⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
       │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠔⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
       │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠤⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
       │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠤⠒⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
       │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡠⠔⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
       │⠀⠀⠀⠀⠀⠀⢀⣀⠤⠔⠊⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
     0 │⣀⠤⠤⠔⠒⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
       └────────────────────────────────────────┘
       1 10

```

---

<div class="post-metadata">

**Author:** ![lucatrv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucatrv/32/5650_2.png) [@lucatrv](https://discourse.julialang.org/u/lucatrv)\
**Post date:** [February 16, 2018, 11:01pm UTC](https://discourse.julialang.org/t/compiling-and-building-binaries-from-your-julia-code/5687/50 "2018-02-16T23:01:38Z")

</div>

Please notice that [static-julia](https://github.com/JuliaComputing/static-julia) was merged with [PackageCompiler.jl](https://github.com/JuliaLang/PackageCompiler.jl), any further development will happen there.

[Previous page](https://discourse.julialang.org/t/compiling-and-building-binaries-from-your-julia-code/5687.md?page=2)
