# Generating C code from JuMP.jl model

**URL:** <https://discourse.julialang.org/t/generating-c-code-from-jump-jl-model/122736>\
**Category:** Modelling & Simulations\
**Tags:** question, jump, modelling\
**Created:** [November 17, 2024, 12:14pm UTC](https://discourse.julialang.org/t/generating-c-code-from-jump-jl-model/122736 "2024-11-17T12:14:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![langestefan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/langestefan/32/207923_2.png) [@langestefan](https://discourse.julialang.org/u/langestefan)\
**Post date:** [November 17, 2024, 12:14pm UTC](https://discourse.julialang.org/t/generating-c-code-from-jump-jl-model/122736/1 "2024-11-17T12:14:17Z")

</div>

I have defined an MILP model in JuMP.jl. This model works great. Now I would like to implement it on a real time controller in C.

Is there a way to generate C code for my model so that I can just copy paste it into my project?

There are some frameworks that offer this:

- [TinyMPC](https://tinympc.org/)
- [CVXGEN](https://cvxgen.com/docs/index.html) for cvxpy

Is there any way to do this with JuMP?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [November 17, 2024, 2:16pm UTC](https://discourse.julialang.org/t/generating-c-code-from-jump-jl-model/122736/2 "2024-11-17T14:16:23Z")

</div>

What exactly is your “real-time controller”? JuMP does not have any facilities for C-code generation, but depending on what your target platform is, there may be other options than going through C code.

---

<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:** [November 17, 2024, 4:46pm UTC](https://discourse.julialang.org/t/generating-c-code-from-jump-jl-model/122736/3 "2024-11-17T16:46:32Z")

</div>

See also: [Code generation · Issue #124 · JuliaControl/ModelPredictiveControl.jl · GitHub](https://github.com/JuliaControl/ModelPredictiveControl.jl/issues/124)

Probably not possible now, but perhaps in the future.

---

<div class="post-metadata">

**Author:** ![langestefan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/langestefan/32/207923_2.png) [@langestefan](https://discourse.julialang.org/u/langestefan)\
**Post date:** [November 17, 2024, 6:27pm UTC](https://discourse.julialang.org/t/generating-c-code-from-jump-jl-model/122736/4 "2024-11-17T18:27:09Z")

</div>

I think its a cortex M33, need to double check this. Dont want to bother the hardware people during the weekend 😉

---

<div class="post-metadata">

**Author:** ![langestefan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/langestefan/32/207923_2.png) [@langestefan](https://discourse.julialang.org/u/langestefan)\
**Post date:** [November 17, 2024, 6:28pm UTC](https://discourse.julialang.org/t/generating-c-code-from-jump-jl-model/122736/5 "2024-11-17T18:28:28Z")

</div>

That’s interesting! But I’m glad I’m not the only one looking at this.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [November 17, 2024, 6:49pm UTC](https://discourse.julialang.org/t/generating-c-code-from-jump-jl-model/122736/6 "2024-11-17T18:49:41Z")

</div>

JuMP does not support transpiling to C, and it is not a feature that we have planned on our roadmap. (Just use cvxpy/cvxgen?)

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [November 17, 2024, 9:59pm UTC](https://discourse.julialang.org/t/generating-c-code-from-jump-jl-model/122736/7 "2024-11-17T21:59:57Z")

</div>

Also if I understand correctly this wouldn’t just require JuMP but also an actual solver to generate the code from?

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [November 17, 2024, 10:20pm UTC](https://discourse.julialang.org/t/generating-c-code-from-jump-jl-model/122736/8 "2024-11-17T22:20:53Z")

</div>

Yes, but most applicable solvers (e.g., SCS, ECOS) are in C and pretty trivial to compile.

In theory this could be done. JuMP ends up calling the C interfaces via `ccall`, so we “know” what C code to write. We just don’t currently trace these calls, and the various bridge reformulations mean that the user’s original JuMP model is often quite dissimilar to what actually gets solved. We also have limited support for parameters, which is a key component of how cvxpygen works.
