# Clang vs CBindingGen

**URL:** https://discourse.julialang.org/t/clang-vs-cbindinggen/56693
**Category:** General Usage
**Tags:** c, clang
**Created:** [March 7, 2021, 7:06pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693 "2021-03-07T19:06:48Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![mhinsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mhinsch/32/26676_2.png) [@mhinsch](https://discourse.julialang.org/u/mhinsch)
#### Post date: [March 7, 2021, 7:06pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/1 "2021-03-07T19:06:48Z")

</div>

I’m thinking about generating a wrapper for a pure C library ([raylib](https://www.raylib.com/)). So far I have found two options for doing that (semi-) automatically: [Clang](https://github.com/JuliaInterop/Clang.jl) and [CBindingGen](https://github.com/analytech-solutions/CBindingGen.jl).  
Clang seems to be the default option, but while documentation for both is on the sparse side, CBindingGen appears to be slightly more accessible (I managed to generate and load bindings in about 30min).  
Does anybody have any experience with either of them and can give me some pros and cons to help me with the decision which one to use?

Edit: added links

---

<div class="post-metadata">

### Author: ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)
#### Post date: [March 7, 2021, 7:16pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/2 "2021-03-07T19:16:04Z")

</div>

If it helps at all, this is the first time I’ve heard of CBindingGen and it’s unclear from their docs why it’s being developed instead of contributing to Clang.jl (I’m sure there’s a reason, but I couldn’t find one written down)

---

<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 7, 2021, 7:36pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/3 "2021-03-07T19:36:22Z")

</div>

`CBindingGen.jl` is based on [`CBinding.jl`](https://github.com/analytech-solutions/CBinding.jl), which overcomes some limitations of Julia’s standard C interface, `Clang.jl` uses Julia’s standard C interface. @krrutkow can perhaps offer some more insights (albeit biased 😉)

---

<div class="post-metadata">

### Author: ![mhinsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mhinsch/32/26676_2.png) [@mhinsch](https://discourse.julialang.org/u/mhinsch)
#### Post date: [March 7, 2021, 9:42pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/4 "2021-03-07T21:42:07Z")

</div>

Yes, same for me, therefore my question. I had assumed Clang was the only option and found CBindingGen only by accident.

---

<div class="post-metadata">

### Author: ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)
#### Post date: [March 8, 2021, 1:59am UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/5 "2021-03-08T01:59:03Z")

</div>

In short, the goal of Clang.jl is to generate self-contained Julia wrapper code for the C API. The generated code is in pure Julia style.

The goal of CBindingGen.jl is to generate Julia wrapper code using CBinding.jl’s macros. The generated code is like C code.

> [@mhinsch](#):
>
> Clang seems to be the default option, but while documentation for both is on the sparse side,

I’m reworking the package to make it easy to use, see [Rework the package -- Take 1 by Gnimuc · Pull Request #278 · JuliaInterop/Clang.jl · GitHub](https://github.com/JuliaInterop/Clang.jl/pull/278).

---

<div class="post-metadata">

### Author: ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)
#### Post date: [March 8, 2021, 2:22am UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/6 "2021-03-08T02:22:32Z")

</div>

This is how I generate the bindings with [#278](https://github.com/JuliaInterop/Clang.jl/pull/278),

```nohighlight
➜ git clone https://github.com/raysan5/raylib.git
Cloning into 'raylib'...
remote: Enumerating objects: 69, done.
remote: Counting objects: 100% (69/69), done.
remote: Compressing objects: 100% (48/48), done.
remote: Total 28173 (delta 37), reused 42 (delta 21), pack-reused 28104
Receiving objects: 100% (28173/28173), 352.88 MiB | 2.83 MiB/s, done.
Resolving deltas: 100% (20012/20012), done.
Updating files: 100% (885/885), done.

➜ cd raylib 

➜ mkdir gen && cd gen

➜ cat wrap.toml
[general]
library_name = "libraylib"
output_file_path = "./LibRaylib.jl"
module_name = "LibRaylib"
use_julia_native_enum_type = true

➜ cat wrap.jl
using Clang.Generators

cd(@ __DIR__ )

const RAYLIB_H = [joinpath(@ __DIR__ , "..", "src", "raylib.h")]

options = load_options(joinpath(@ __DIR__ , "wrap.toml"))

args = String[]

ctx = create_context(RAYLIB_H, args, options)

build!(ctx)

➜ julia wrap.jl
[Info: [CollectTopLevelNode]: processing header: /Users/gnimuc/Codes/raylib/gen/../src/raylib.h
[ Info: Building the DAG...
[ Info: Emit Julia expressions...
[Info: [ProloguePrinter]: print to ./LibRaylib.jl
[Info: [GeneralPrinter]: print to ./LibRaylib.jl
[Info: [EpiloguePrinter]: print to ./LibRaylib.jl
[ Info: Done!

```

---

<div class="post-metadata">

### Author: ![mhinsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mhinsch/32/26676_2.png) [@mhinsch](https://discourse.julialang.org/u/mhinsch)
#### Post date: [March 8, 2021, 9:03am UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/7 "2021-03-08T09:03:36Z")

</div>

Wow, thanks for the comprehensive walkthrough.

That looks pretty straightforward. I’ll give it a try as soon as I have time and decide based on how much I like both packages’ output 😉 .

---

<div class="post-metadata">

### Author: ![krrutkow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krrutkow/32/9138_2.png) [@krrutkow](https://discourse.julialang.org/u/krrutkow)
#### Post date: [March 8, 2021, 1:33pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/8 "2021-03-08T13:33:41Z")

</div>

CBinding was necessary to more accurately support C code, but it had its quirks. CBindingGen was an alternative to Clang (which has its own quirks) that used the CBinding syntax to less manually create the bindings.

The CBinding\* framework was an experimental and educational step though, and we are working on releasing a new related package this week that you all might find rather interesting.

---

<div class="post-metadata">

### Author: ![mhinsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mhinsch/32/26676_2.png) [@mhinsch](https://discourse.julialang.org/u/mhinsch)
#### Post date: [March 14, 2021, 3:32pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/9 "2021-03-14T15:32:58Z")

</div>

I finally got around to giving this a try and I have to say the generated code looks super-pretty! If it works as well as it looks this is definitely a winner.

Update:

Works great save for macros and varargs (not implemented yet, as mentioned in the PR @Gnimuc linked to). Also, the library name in the .toml file needs to be a symbol (so “:libraylib” in my case).

But I can definitely work with this and the generated Julia API seems to be much nicer than CBindingGen.

---

<div class="post-metadata">

### Author: ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)
#### Post date: [March 15, 2021, 1:39pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/10 "2021-03-15T13:39:34Z")

</div>

You may also be interested in @krrutkow’s new work: [GitHub - analytech-solutions/C.jl: Automatic C interfacing for Julia](https://github.com/analytech-solutions/C.jl).

The new string macro just allows users to write exactly C code, which, I think, is way better than CBinding.jl’s Julia-C-hybridized macros.

As I mentioned above, the goal of Clang.jl’s generator is to generate code that a user will write based upon Julia’s C interop docs. The goal of CBinding.jl/C.jl is to get a better user experience when working with the C interface.

---

<div class="post-metadata">

### Author: ![krrutkow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krrutkow/32/9138_2.png) [@krrutkow](https://discourse.julialang.org/u/krrutkow)
#### Post date: [March 15, 2021, 1:44pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/11 "2021-03-15T13:44:00Z")

</div>

Also, the Julia package curators are not a fan of the C.jl package name, so package functionality will probably be merged into CBinding.jl

---

<div class="post-metadata">

### Author: ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)
#### Post date: [March 15, 2021, 1:52pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/12 "2021-03-15T13:52:59Z")

</div>

And for historical reasons, the name of Clang.jl is also to some extent misleading. Clang.jl should only host a thin wrapper over libclang or even Clang, but I guess it’s too late to make the change. 😅

---

<div class="post-metadata">

### Author: ![krrutkow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krrutkow/32/9138_2.png) [@krrutkow](https://discourse.julialang.org/u/krrutkow)
#### Post date: [March 15, 2021, 1:55pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/13 "2021-03-15T13:55:23Z")

</div>

Hah! “Thin” is a subjective term, so no worries!

---

<div class="post-metadata">

### Author: ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)
#### Post date: [March 15, 2021, 2:21pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/14 "2021-03-15T14:21:02Z")

</div>

For someone who might be interested in the interop between Julia and C, here are some recent PRs/Issues related to the topic:

- `@ccall` macro for varargs: [https://github.com/JuliaLang/julia/pull/32748](https://github.com/JuliaLang/julia/pull/32748)

- layout: [https://github.com/JuliaLang/julia/pull/33886](https://github.com/JuliaLang/julia/pull/33886)

- layout: [https://github.com/JuliaLang/julia/issues/35795](https://github.com/JuliaLang/julia/issues/35795)

- forward decl: [https://github.com/JuliaLang/julia/pull/32658](https://github.com/JuliaLang/julia/pull/32658)

---

<div class="post-metadata">

### Author: ![mhinsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mhinsch/32/26676_2.png) [@mhinsch](https://discourse.julialang.org/u/mhinsch)
#### Post date: [March 15, 2021, 3:10pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/15 "2021-03-15T15:10:55Z")

</div>

Off-topic, but I’ve been waiting for #32658 to finally be merged for ages. It’s really slightly embarrassing that Julia still has no simple, straightforward way to declare mutually recursive types.

---

<div class="post-metadata">

### Author: ![krrutkow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krrutkow/32/9138_2.png) [@krrutkow](https://discourse.julialang.org/u/krrutkow)
#### Post date: [March 17, 2021, 1:15pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/16 "2021-03-17T13:15:26Z")

</div>

The raylib bindings using CBinding.jl v1.0:

```nohighlight
module libraylib
	using CBinding
	
	let
		CMAKE_INSTALL_PREFIX = "$(dirname(@ __DIR__ ))/deps/usr"
		c`-I$(CMAKE_INSTALL_PREFIX)/include -L$(CMAKE_INSTALL_PREFIX)/lib -lraylib`
	end
	
	const c"va_list" = Cvoid
	c"""
		#include <raylib.h>
		#include <physac.h>
		#include <raymath.h>
		#include <rlgl.h>
	"""ji
end

```

You can add ‘w’ to the string macro options to wrap the inline functions as well, but that is more experimental functionality.

---

<div class="post-metadata">

### Author: ![mhinsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mhinsch/32/26676_2.png) [@mhinsch](https://discourse.julialang.org/u/mhinsch)
#### Post date: [March 17, 2021, 2:01pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/17 "2021-03-17T14:01:15Z")

</div>

That’s also quite lean! But if I understand the documentation correctly the generated bindings have to be used via the c"" string macro, right?

---

<div class="post-metadata">

### Author: ![krrutkow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krrutkow/32/9138_2.png) [@krrutkow](https://discourse.julialang.org/u/krrutkow)
#### Post date: [March 17, 2021, 2:08pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/18 "2021-03-17T14:08:28Z")

</div>

With the ‘j’ string macro option applied, Julian names are also created. So in this example you can directly use `libraylib.Transform` for instance.

As you mention, bindings are always generated in the `c"..."` symbol “namespace” to avoid name collisions (that approach is 100% foolproof). They are optionally mapped into the Julian symbol “namespace” if the user determines no name collisions will occur and requests that functionality.

---

<div class="post-metadata">

### Author: ![freemint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freemint/32/20013_2.png) [@freemint](https://discourse.julialang.org/u/freemint)
#### Post date: [April 21, 2021, 10:37pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/19 "2021-04-21T22:37:25Z")

</div>

@mhinsch I’m also interested in an Raylib binding. If you have something working would you mind uploading it to GitHub?

---

<div class="post-metadata">

### Author: ![mhinsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mhinsch/32/26676_2.png) [@mhinsch](https://discourse.julialang.org/u/mhinsch)
#### Post date: [April 22, 2021, 1:32pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/20 "2021-04-22T13:32:49Z")

</div>

Unfortunately more urgent (as in I’m actually getting paid to do them) things got in the way and will do so for a while. I still intend to get back to the raylib wrapper, but it might be a couple of months before I have the time to do so.

[Next page](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693.md?page=2)
