Guidelines for wrapping a simple C library in Julia

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?

1 Like

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 . 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.

1 Like

BinaryBuilder.jl 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), 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.


The very first thing I would do is to figure out how ccall or @ccall works along Libdl.dlopen.

Then also read this section of the manual:

Once you have done that, can help you with packaging. Other packages such as CBinding.jl or Clang.jl can help you automate the bindings.


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.

1 Like

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.


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:

For example, here is the build_tarballs.jl script for cpuinfo:

1 Like