# Compile C code with Julia package

**URL:** https://discourse.julialang.org/t/compile-c-code-with-julia-package/91305
**Category:** General Usage
**Tags:** package, compilation, c
**Created:** [December 6, 2022, 2:45pm UTC](https://discourse.julialang.org/t/compile-c-code-with-julia-package/91305 "2022-12-06T14:45:21Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![PaterPen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paterpen/32/27985_2.png) [@PaterPen](https://discourse.julialang.org/u/PaterPen)
#### Post date: [December 6, 2022, 2:45pm UTC](https://discourse.julialang.org/t/compile-c-code-with-julia-package/91305/1 "2022-12-06T14:45:21Z")

</div>

Hi,

I am currently thinking about migrating an R package with underlying C/C++ code to a Julia package. However, in the short term the large C/C++ codebase should be reused and therefore is intended to be called from within Julia instead of R.

My problem is now, that I neither want to ship binaries nor do I want the user having to install make, gcc etc. and compile the files. Instead I would like to have a singular Julia package which does this process for the user (similar to `R CMD INSTALL`). I have not found much beside [BinaryBuilder.jl](https://github.com/JuliaPackaging/BinaryBuilder.jl) and [CMake.jl](https://github.com/JuliaPackaging/CMake.jl) so far. How can I achieve this and what is best practise?

---

<div class="post-metadata">

### Author: ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)
#### Post date: [December 6, 2022, 3:11pm UTC](https://discourse.julialang.org/t/compile-c-code-with-julia-package/91305/2 "2022-12-06T15:11:50Z")

</div>

I guess that by “I neither want to ship binaries […]” you mean that you don’t want to do this manually? In this case, you could contribute your C code as a recipe to [GitHub - JuliaPackaging/Yggdrasil: Collection of builder repositories for BinaryBuilder.jl](https://github.com/JuliaPackaging/Yggdrasil/), which creates binaries which can be installed like a normal julia package and which would serve as a dependency for your pure julia package.

---

<div class="post-metadata">

### Author: ![PaterPen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paterpen/32/27985_2.png) [@PaterPen](https://discourse.julialang.org/u/PaterPen)
#### Post date: [January 24, 2023, 1:14am UTC](https://discourse.julialang.org/t/compile-c-code-with-julia-package/91305/3 "2023-01-24T01:14:43Z")

</div>

Thanks for your reply! It took me some time to read into the topic, please excuse my ghosting.

What I tried to express was: I was wondering whether there is a proper way to work _without_ cross-compilation. My understanding is that BinaryBuilder + Yggdrasil is used to cross-compile reliably and distribute the binaries. Another approach would be to have the user compile the binaries on his own machine.

The question arises since in the R ecosystem this is possible and sometimes even necessary because the CRAN requirements are quite strict.

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [January 24, 2023, 1:24am UTC](https://discourse.julialang.org/t/compile-c-code-with-julia-package/91305/4 "2023-01-24T01:24:28Z")

</div>

You could add a `deps/build.jl` to your package:  
[https://pkgdocs.julialang.org/v1/creating-packages/#Adding-a-build-step-to-the-package](https://pkgdocs.julialang.org/v1/creating-packages/#Adding-a-build-step-to-the-package)

More packages used to do this, but this created a rather terrible user experience. Most now use BinaryBuilder via Yggdrasil because this works quite reliably and does not impose many requirements upon the user. This will produce a JLL package.

If the user really wanted to build the binaries on their machine, they can use the JLL override mechanism via a Override.toml in the Julia depot or a LocalPreferences.toml with the Julia project.  
[https://docs.binarybuilder.org/stable/jll/#Overriding-specific-products](https://docs.binarybuilder.org/stable/jll/#Overriding-specific-products)

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [January 24, 2023, 1:47am UTC](https://discourse.julialang.org/t/compile-c-code-with-julia-package/91305/5 "2023-01-24T01:47:03Z")

</div>

> [@PaterPen](#):
>
> What I tried to express was: I was wondering whether there is a proper way to work _without_ cross-compilation.

Any specific reason why you want to avoid that?

---

<div class="post-metadata">

### Author: ![PaterPen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paterpen/32/27985_2.png) [@PaterPen](https://discourse.julialang.org/u/PaterPen)
#### Post date: [February 13, 2023, 12:21am UTC](https://discourse.julialang.org/t/compile-c-code-with-julia-package/91305/6 "2023-02-13T00:21:10Z")

</div>

Tbh the more I think about it, the less sense it makes.

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [February 13, 2023, 12:30am UTC](https://discourse.julialang.org/t/compile-c-code-with-julia-package/91305/7 "2023-02-13T00:30:40Z")

</div>

I strongly encourage the BinaryBuilder approach.

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [February 13, 2023, 12:46am UTC](https://discourse.julialang.org/t/compile-c-code-with-julia-package/91305/8 "2023-02-13T00:46:42Z")

</div>

That doesn’t answer my question, your comments are vague and unspecific.

---

<div class="post-metadata">

### Author: ![PaterPen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paterpen/32/27985_2.png) [@PaterPen](https://discourse.julialang.org/u/PaterPen)
#### Post date: [February 13, 2023, 1:02am UTC](https://discourse.julialang.org/t/compile-c-code-with-julia-package/91305/9 "2023-02-13T01:02:32Z")

</div>

Ok. Maybe this does: I had some potential issues in mind which I thought could arise with the cross-compilation approach. However in the meantime I have come to the conclusion that these potential issues are actually nonsense. Being more specific would mean presenting you some flawed argument which probably doesn’t benefit anyone.

In other words: No, I don’t have a specific reason 😃
