# Compiling Julia to C/C++ or small executable file

**URL:** <https://discourse.julialang.org/t/compiling-julia-to-c-c-or-small-executable-file/86949>\
**Category:** General Usage\
**Tags:** package-compiler\
**Created:** [September 8, 2022, 3:46pm UTC](https://discourse.julialang.org/t/compiling-julia-to-c-c-or-small-executable-file/86949 "2022-09-08T15:46:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [September 8, 2022, 3:46pm UTC](https://discourse.julialang.org/t/compiling-julia-to-c-c-or-small-executable-file/86949/1 "2022-09-08T15:46:29Z")

</div>

My programs often use complex intermediate data structures and algorithms (databases, rule-based transforms, symbolic math, simplifications, …). Their results are rather simple, to be included or embedded in other C/C++ code or run outside of Julia.

From what I understood, there are possibilities to generate a rather large executable, with some hope that Julia will improve on that, but not being high on the list.

Would it then make sense (if possible at all) to develop a Julia-to-C/C++ converter, or small executable compiler, step-by-step, starting with a limited feature set:

- Minimum C/Julia subset: no multiple dispatch, no packages …
- multiple dispatch
- including eval(), needed for packages (?)

Readability of the generated C-code would be definitively a plus.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 8, 2022, 3:52pm UTC](https://discourse.julialang.org/t/compiling-julia-to-c-c-or-small-executable-file/86949/2 "2022-09-08T15:52:26Z")

</div>

transpiling to C/C++ is strictly harder than generating a small exe.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 8, 2022, 4:20pm UTC](https://discourse.julialang.org/t/compiling-julia-to-c-c-or-small-executable-file/86949/3 "2022-09-08T16:20:45Z")

</div>

You can make small binaries (limited subset of Julia):

> [@Successful Static Compilation of Julia Code for use in Production](https://discourse.julialang.org/t/successful-static-compilation-of-julia-code-for-use-in-production/79318):
>
> The [StaticCompiler](https://github.com/tshort/StaticCompiler.jl) package was recently registered. This post records a successful experiment to statically compile a piece of Julia code into a small .so library on Linux, which is then loaded from Python and used in training of a deep learning model. TLDR Static compilation to a stand-alone library does work on Linux but has rather significant restrictions on the functionality available. It is mostly useful for core computational routines. Roughly speaking, if it could be implemented in plai…

> ### Calling the Library from Python

That’s just an example, you can call from C or C++ too.

There’s already the outdated/unmaintained Intel’s Julia2C (for a limited subset of Julia; to C++ possible too):

> [@Status of and need for ParallelAccelerator](https://discourse.julialang.org/t/status-of-and-need-for-parallelaccelerator/19277):
>
> I just notice it’s one of the packages that hasn’t been upgraded, still stuck on 0.6. I’m just thinking is this more of a testament of there no longer being a need with Julia 1.0+ much faster with or without say thread handing? Other abandoned IntelLabs projects are Julia2C and HPAT.jl. HPAT was one of my favorites to point to. It just seemed like an awesome Julia-only package, 100x faster than mainstream competitors. It still may be that, while it didn’t require being made in Julia with Inte…

If you want to call Julia from C++, with all Julia features possible including eval, then there’s the seemingly awesome (well documented) Jluna project for that. Similar project to call from Rust. And from C, just the embedding API, these build on.

> **[GitHub - Clemapfel/jluna: Julia Wrapper for C++ with Focus on Safety,...](https://github.com/Clemapfel/jluna)**
>
> Julia Wrapper for C++ with Focus on Safety, Elegance, and Ease of Use

You can’t have small binaries with all Julia features, at least if you include eval (that you may want to avoid anyway), since with it Julia is strictly more powerful than C or C++; then you need the backend of the compiler, LLVM dependency. But you can drop that (and some other dependencies, for smaller binaries if not needed.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 9, 2022, 5:59am UTC](https://discourse.julialang.org/t/compiling-julia-to-c-c-or-small-executable-file/86949/4 "2022-09-09T05:59:46Z")

</div>

There is already a package that generates C code for LTI systems: [GitHub - JuliaControl/SymbolicControlSystems.jl: C-code generation and an interface between ControlSystems.jl and SymPy.jl](https://github.com/JuliaControl/SymbolicControlSystems.jl)

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [September 11, 2022, 10:16am UTC](https://discourse.julialang.org/t/compiling-julia-to-c-c-or-small-executable-file/86949/5 "2022-09-11T10:16:41Z")

</div>

Thanks for all comments and ideas!

My actual work requires translation of (Matlab) functions to C, C++, and event-based SystemVerilog.  
After type inference, I describe the function in a small intermediate format, which then can be easily parsed into the target language.

After completion, I found the following sources along similar lines:

[kiselgra/c-mera: Next-level syntax for C-like languages 🙂 (github.com)](https://github.com/kiselgra/c-mera)

[LingDong-/wax: A tiny programming language that transpiles to C, C++, Java, TypeScript, Python, C#, Swift, Lua and WebAssembly 🚀 (github.com)](https://github.com/LingDong-/wax)
