# Guidelines for wrapping a simple C library in Julia

**URL:** https://discourse.julialang.org/t/guidelines-for-wrapping-a-simple-c-library-in-julia/112335
**Category:** General Usage
**Tags:** ccall, binarybuilder, c, clang
**Created:** [March 30, 2024, 8:13pm UTC](https://discourse.julialang.org/t/guidelines-for-wrapping-a-simple-c-library-in-julia/112335 "2024-03-30T20:13:19Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![pjssilva](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pjssilva/32/4238_2.png) [@pjssilva](https://discourse.julialang.org/u/pjssilva)
#### Post date: [March 30, 2024, 8:13pm UTC](https://discourse.julialang.org/t/guidelines-for-wrapping-a-simple-c-library-in-julia/112335/1 "2024-03-30T20:13:19Z")

</div>

I am scratching my own itch here and decided to write a wrapper to a small C library **I wrote** to solve the problem of projecting a point onto the intersection of a box and a hyperplane. Is there a good “best practices” description on how to achieve this?

Looking around, I found mentions to BinaryBuilder.jl and CBinding.jl. Is that a good direction?

---

<div class="post-metadata">

### Author: ![j-fu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j-fu/32/11373_2.png) [@j-fu](https://discourse.julialang.org/u/j-fu)
#### Post date: [March 30, 2024, 8:33pm UTC](https://discourse.julialang.org/t/guidelines-for-wrapping-a-simple-c-library-in-julia/112335/2 "2024-03-30T20:33:11Z")

</div>

May be you first start with learning how to call C code - you can call it directly from julia via `ccall`, and you can load your locally built library into Julia via `dlopen`, see [Calling C and Fortran Code · The Julia Language](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/) . If your API is small, you can easily write Julia wrappers invoking `ccall` by hand. CBinding.jl tries to automate this process.

The BinaryBuilder allows to maintain precompiled libraries and wrap them into jll packages. You would use this to distribute the precompiled C code to users of your Julia library.

That said, did you consider to implement your algorithm directly in Julia ? It could be more flexible with respect to data types, and a nice learning exercise.

---

<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: [March 30, 2024, 8:35pm UTC](https://discourse.julialang.org/t/guidelines-for-wrapping-a-simple-c-library-in-julia/112335/3 "2024-03-30T20:35:28Z")

</div>

[BinaryBuilder.jl](https://binarybuilder.org/) is a tool for _building_ libraries written in C/C++, Fortran, Rust and Go, and automatically ship them to users. Then you want to write the bindings: you can either do that manually (read the [relevant section of the manual](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/)), or use some tools to automatically generate them from the header files of a package. CBinding.jl is an option, but Clang.jl is also very frequently employed for this task.

---

<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: [March 30, 2024, 8:51pm UTC](https://discourse.julialang.org/t/guidelines-for-wrapping-a-simple-c-library-in-julia/112335/4 "2024-03-30T20:51:46Z")

</div>

The very first thing I would do is to figure out how [`ccall`](https://docs.julialang.org/en/v1/base/c/#ccall) or [`@ccall`](https://docs.julialang.org/en/v1/base/c/#Base.@ccall) works along [`Libdl.dlopen`](https://docs.julialang.org/en/v1/stdlib/Libdl/#Base.Libc.Libdl.dlopen).

Then also read this section of the manual:  
[https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/)

Once you have done that, [BinaryBuilder.org](http://BinaryBuilder.org) can help you with packaging. Other packages such as CBinding.jl or Clang.jl can help you automate the bindings.

---

<div class="post-metadata">

### Author: ![pjssilva](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pjssilva/32/4238_2.png) [@pjssilva](https://discourse.julialang.org/u/pjssilva)
#### Post date: [March 30, 2024, 8:54pm UTC](https://discourse.julialang.org/t/guidelines-for-wrapping-a-simple-c-library-in-julia/112335/5 "2024-03-30T20:54:08Z")

</div>

Yes, I plan to re-write the code in Julia in the future (and also make it multithreaded/parallel). But for start, I just want to write the wrapper.

Since I want to publish the package, as it might be useful for others, I believe I will need to use BInaryBuilder.jl at some point so I can distribute the binaries (instead of forcing the users to compile themselves, which might be a nightmare for support).

I will start with ccall as you say with a locally compiled version of the code. Then, I will see how to use BinaryBuilder.jl to make this locally compiled library a library. Thanks for the pointers.

---

<div class="post-metadata">

### Author: ![pjssilva](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pjssilva/32/4238_2.png) [@pjssilva](https://discourse.julialang.org/u/pjssilva)
#### Post date: [March 31, 2024, 10:58pm UTC](https://discourse.julialang.org/t/guidelines-for-wrapping-a-simple-c-library-in-julia/112335/6 "2024-03-31T22:58:06Z")

</div>

Quick follow up. I already have the simple binding done by hand from reading the manual, up and running. I am doing some few tests and then I will learn how to use BinaryBuilder.jl to make the package available.

---

<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: [March 31, 2024, 11:57pm UTC](https://discourse.julialang.org/t/guidelines-for-wrapping-a-simple-c-library-in-julia/112335/7 "2024-03-31T23:57:53Z")

</div>

BinaryBuilder does have a wizard that helps you assemble a build\_tarballs.jl script. However, I often find it easier to just manually write a build\_tarballs.jl script by hand when I know what I am doing.

The location of the build\_tarballs.jl scripts are located here:

> **[GitHub - JuliaPackaging/Yggdrasil: Collection of builder repositories for...](https://github.com/JuliaPackaging/Yggdrasil)**
>
> Collection of builder repositories for BinaryBuilder.jl - JuliaPackaging/Yggdrasil

For example, here is the build\_tarballs.jl script for `cpuinfo`:

> <https://github.com/JuliaPackaging/Yggdrasil/blob/master/C/CPUInfo/build_tarballs.jl>
