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

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.

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

3 Likes

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

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):

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.

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.

1 Like

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

1 Like

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 :slight_smile: (github.com)

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

1 Like