# How to compile the julia code into c language?

**URL:** <https://discourse.julialang.org/t/how-to-compile-the-julia-code-into-c-language/76573>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 16, 2022, 5:19pm UTC](https://discourse.julialang.org/t/how-to-compile-the-julia-code-into-c-language/76573 "2022-02-16T17:19:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [February 16, 2022, 5:19pm UTC](https://discourse.julialang.org/t/how-to-compile-the-julia-code-into-c-language/76573/1 "2022-02-16T17:19:30Z")

</div>

I have my code in julia language, can I generated a C code for it?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 16, 2022, 5:21pm UTC](https://discourse.julialang.org/t/how-to-compile-the-julia-code-into-c-language/76573/2 "2022-02-16T17:21:15Z")

</div>

no, but keep an eye on:  
[https://github.com/tshort/StaticCompiler.jl](https://github.com/tshort/StaticCompiler.jl)

---

<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:** [February 16, 2022, 5:21pm UTC](https://discourse.julialang.org/t/how-to-compile-the-julia-code-into-c-language/76573/3 "2022-02-16T17:21:53Z")

</div>

No basically. There was at one point Intel’s Julia2C, but it’s outdated. You can however call Julia code from C.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 16, 2022, 5:27pm UTC](https://discourse.julialang.org/t/how-to-compile-the-julia-code-into-c-language/76573/6 "2022-02-16T17:27:23Z")

</div>

no, checkout:

> **[GitHub - JuliaLang/PackageCompiler.jl: Compile your Julia Package](https://github.com/JuliaLang/PackageCompiler.jl)**
>
> Compile your Julia Package. Contribute to JuliaLang/PackageCompiler.jl development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 16, 2022, 5:27pm UTC](https://discourse.julialang.org/t/how-to-compile-the-julia-code-into-c-language/76573/7 "2022-02-16T17:27:47Z")

</div>

don’t use Modelica or don’t use Julia

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 16, 2022, 5:29pm UTC](https://discourse.julialang.org/t/how-to-compile-the-julia-code-into-c-language/76573/9 "2022-02-16T17:29:29Z")

</div>

does you Julia code use Julia packages? you need to manually translate most likely

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [February 16, 2022, 5:34pm UTC](https://discourse.julialang.org/t/how-to-compile-the-julia-code-into-c-language/76573/10 "2022-02-16T17:34:24Z")

</div>

Yes, I am using several Julia packages (plus user-defined ones) like `SparseArrays, LinearAlgebra, StaticArrays, PlotlyJS, PyPlot, BenchmarkTools, NativeFileDialog, JLD2, CSV, DataFrames, Gtk` translating them can be done by this [https://github.com/JuliaLang/PackageCompiler.jl](https://github.com/JuliaLang/PackageCompiler.jl)?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 16, 2022, 5:39pm UTC](https://discourse.julialang.org/t/how-to-compile-the-julia-code-into-c-language/76573/11 "2022-02-16T17:39:18Z")

</div>

no, that compiles to executable.

there’s no way to translate, not at this scale (i.e. so many julia packages involved)

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [February 16, 2022, 5:43pm UTC](https://discourse.julialang.org/t/how-to-compile-the-julia-code-into-c-language/76573/12 "2022-02-16T17:43:37Z")

</div>

Aha, how can I translate my embedded julia packages manually?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 16, 2022, 5:52pm UTC](https://discourse.julialang.org/t/how-to-compile-the-julia-code-into-c-language/76573/13 "2022-02-16T17:52:17Z")

</div>

Re-implement the functions you used in whatever language supported by the other tool you needed?

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [February 16, 2022, 5:59pm UTC](https://discourse.julialang.org/t/how-to-compile-the-julia-code-into-c-language/76573/14 "2022-02-16T17:59:17Z")

</div>

I think it will be a pain.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 16, 2022, 5:59pm UTC](https://discourse.julialang.org/t/how-to-compile-the-julia-code-into-c-language/76573/15 "2022-02-16T17:59:33Z")

</div>

> [@Amro](#):
>
> I have my code in julia language, can I generated a C code for it?

You could write a thin wrapper in C that links `libjulia` and calls your Julia code.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [February 16, 2022, 6:02pm UTC](https://discourse.julialang.org/t/how-to-compile-the-julia-code-into-c-language/76573/16 "2022-02-16T18:02:11Z")

</div>

Can you give me an example or link for doing this?

---

<div class="post-metadata">

**Author:** ![rashid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rashid/32/9398_2.png) [@rashid](https://discourse.julialang.org/u/rashid)\
**Post date:** [February 16, 2022, 6:36pm UTC](https://discourse.julialang.org/t/how-to-compile-the-julia-code-into-c-language/76573/17 "2022-02-16T18:36:08Z")

</div>

A quick search led me to this: [Calling local Julia package from C - Stack Overflow](https://stackoverflow.com/questions/55970064/calling-local-julia-package-from-c)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 16, 2022, 7:28pm UTC](https://discourse.julialang.org/t/how-to-compile-the-julia-code-into-c-language/76573/18 "2022-02-16T19:28:56Z")

</div>

I think it’s easier to compile a C-callable interface on the Julia side than to mess around with lots of `jl_*` functions, for example: [I am searching for a C++ code implementing the complex polygamma function - Computational Science Stack Exchange](https://scicomp.stackexchange.com/questions/23194/i-am-searching-for-a-c-code-implementing-the-complex-polygamma-function)

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 16, 2022, 8:37pm UTC](https://discourse.julialang.org/t/how-to-compile-the-julia-code-into-c-language/76573/19 "2022-02-16T20:37:32Z")

</div>

You said they support Fortran or C, do they mean they support shared library? Or they take Fortran or C code and compile together into their stuff

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 16, 2022, 9:02pm UTC](https://discourse.julialang.org/t/how-to-compile-the-julia-code-into-c-language/76573/21 "2022-02-16T21:02:37Z")

</div>

One doesn’t simply “call a c function”, if by calling you mean like ccall() in Julia, then it’s Fortran or C compiled to a .so shared library, which means you can do the same for Julia, by writing a wrapper as suggested above

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [February 17, 2022, 10:35pm UTC](https://discourse.julialang.org/t/how-to-compile-the-julia-code-into-c-language/76573/22 "2022-02-17T22:35:36Z")

</div>

I have worked in this today, I found that modelica includes the C function directly into its model, in which the C function is compiled by gcc (embedded in modelica). I am not sure if I can do this for julia also as I could not find that `gcc` support julia.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 17, 2022, 10:48pm UTC](https://discourse.julialang.org/t/how-to-compile-the-julia-code-into-c-language/76573/23 "2022-02-17T22:48:50Z")

</div>

> [@Amro](#):
>
> in which the C function is compiled by gcc (embedded in modelica).

Seems pretty limiting if it doesn’t let you link to libraries. That would only let you use C code that is completely self contained, which is not true of most interesting code these days.

Are you sure they don’t allow you to compile things yourself and just provide a shared library to load?

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [February 17, 2022, 10:58pm UTC](https://discourse.julialang.org/t/how-to-compile-the-julia-code-into-c-language/76573/24 "2022-02-17T22:58:38Z")

</div>

They provide to include a shared library as well. So, I am wondering if I can compile a Julia code to a shared library, so I can include this gendered library such as `dll` but I am not sure if it will be compatible or not.

[Next page](https://discourse.julialang.org/t/how-to-compile-the-julia-code-into-c-language/76573.md?page=2)
