# 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:** 10\
**Page:** 2

<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 22, 2021, 7:12pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/21 "2021-04-22T19:12:05Z")

</div>

Do you have any unfinished code you could upload?

I might want to take part in the Game Makers Toolkit game jam next month and if i don’t have to start from scratch that might motivate me to get something runable by then.

---

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

</div>

Not really anything beyond a basic proof of concept. I basically used Gnumic’s example:

wrap.toml

```nohighlight
[general]
library_name = ":libraylib"
output_file_path = "./LibRaylib.jl"
module_name = "LibRaylib"
use_julia_native_enum_type = true

```

wrap.jl

```julia
using Clang.Generators

cd(@ __DIR__ )

const RAYLIB_H = ["/usr/local/include/raylib.h"]

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

args = String[]

ctx = create_context(RAYLIB_H, args, options)

build!(ctx)

```

Running `wrap.jl` produces `LibRayLib.jl` which is 99% fine, but needs a tiny bit of editing to account for issues with vararg functions and some macros (no idea if that has been fixed in the meantime). Then code like this works:

run\_rl.jl

```julia

include("LibRaylib.jl")

const RL = LibRaylib

const screen_width = 1600
const screen_height = 900

function run()
	RL.InitWindow(screen_width, screen_height, "run")

	camera = RL.Camera2D(RL.Vector2(500, 500), 
		RL.Vector2(screen_width/2, screen_height/2),
		0.0,
		1.0)

	x = floor(Int, rand() * 1000) + 200
	y = floor(Int, rand() * 700) + 100

	while RL.WindowShouldClose() == 0
		RL.BeginDrawing()
		
		RL.BeginMode2D(camera)
		RL.ClearBackground(RL.Color(0, 0, 0, 255))

		x += floor(Int, (rand()-0.49)*10)
		y += floor(Int, (rand()-0.49)*10)

		RL.DrawText("Dies ist ein Test!", 200, 200, 20, RL.Color(250, 0, 0, 255))
		RL.DrawCircle(x, y, 100, RL.Color(0, 255, 0, 150))

		RL.EndMode2D()

		RL.EndDrawing()
	end

	RL.CloseWindow()
end

run()

```

---

<div class="post-metadata">

**Author:** ![HandyKurniawan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/handykurniawan/32/42607_2.png) [@HandyKurniawan](https://discourse.julialang.org/u/HandyKurniawan)\
**Post date:** [November 24, 2022, 9:07am UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/23 "2022-11-24T09:07:15Z")

</div>

Hi, thank you for the thorough examples of using clang. However, I have an issue running the same code as the above.

The `LibRaylib.jl` is created successfully, but while running the `run_rl.jl`, I got this error `ERROR: could not load library "libraylib"`.

Any tips to solve this issue?

---

<div class="post-metadata">

**Author:** ![serenity4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/serenity4/32/44707_2.png) [@serenity4](https://discourse.julialang.org/u/serenity4)\
**Post date:** [November 24, 2022, 4:32pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/24 "2022-11-24T16:32:36Z")

</div>

It seems like you are trying to use a wrapper for a library, but without the library. Are you sure that `libraylib` is installed on your system?

---

<div class="post-metadata">

**Author:** ![HandyKurniawan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/handykurniawan/32/42607_2.png) [@HandyKurniawan](https://discourse.julialang.org/u/HandyKurniawan)\
**Post date:** [November 28, 2022, 12:11pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/25 "2022-11-28T12:11:54Z")

</div>

Hi @serenity4, thank you for responding.

I have installed the raylib (following through this guide [Working on GNU Linux · raysan5/raylib Wiki · GitHub](https://github.com/raysan5/raylib/wiki/Working-on-GNU-Linux)), it still gave me the same error.

```julia
git clone https://github.com/raysan5/raylib.git raylib
cd raylib
mkdir build && cd build
cmake -DBUILD_SHARED_LIBS=ON ..
make
sudo make install

```

I am not sure why it doesn’t detect the installation. I am a new Linux user, so maybe I didn’t install it correctly.

---

<div class="post-metadata">

**Author:** ![serenity4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/serenity4/32/44707_2.png) [@serenity4](https://discourse.julialang.org/u/serenity4)\
**Post date:** [November 28, 2022, 12:35pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/26 "2022-11-28T12:35:12Z")

</div>

You should then make sure that the libraries you built are discoverable within the library search path of your system. You can look into `LD_LIBRARY_PATH` for example.

Since `Raylib` is in Yggdrasil (see [here](https://github.com/JuliaPackaging/Yggdrasil/tree/master/R/Raylib)), an even better solution would be to use the Julia package `Raylib_jll`. As long as the library name of the JLL package matches the library name you use in your wrapper, the issue might be solved by just doing `import Raylib_jll` (at package ` __init__ ()` time, `Raylib_jll` will `dlopen` the shared library and therefore the library will be callable by name directly, i.e. no need to mess with system paths).

---

<div class="post-metadata">

**Author:** ![HandyKurniawan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/handykurniawan/32/42607_2.png) [@HandyKurniawan](https://discourse.julialang.org/u/HandyKurniawan)\
**Post date:** [November 28, 2022, 12:50pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/27 "2022-11-28T12:50:22Z")

</div>

Thanks, @serenity4!

I will figure out how to continue with this one. However, I tried with a different approach using the JLL package.

```julia
using Clang.Generators
using Raylib_jll

cd(@ __DIR__ )

include_dir = normpath(Raylib_jll.artifact_dir, "include")

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

# add compiler flags, e.g. "-DXXXXXXXXX"
args = get_default_args()
push!(args, "-I$include_dir")

header_dir = include_dir
headers = [joinpath(header_dir, header) for header in readdir(header_dir) if endswith(header, ".h")]

# create context
ctx = create_context(headers, args, options)

# run generator
build!(ctx)

```

```julia
[general]
library_name = "libraylib"
output_file_path = "./LibRayLib.jl"
module_name = "LibRaylib"
jll_pkg_name = "Raylib_jll"

```

However, I got this error message

```julia
[ Info: Processing header: /home/vscode/.julia/artifacts/43bf856bd341c937a4ef62a0db273d61985df75e/include/rlgl.h
ERROR: AssertionError: duplicated definitions should be exactly the same!
Stacktrace:
 [1] (::IndexDefinition)(dag::ExprDAG, options::Dict{String, Any})
   @ Clang.Generators ~/.julia/packages/Clang/i9s1u/src/generator/passes.jl:127
 [2] build!(ctx::Context, stage::Clang.Generators.BuildStage)
   @ Clang.Generators ~/.julia/packages/Clang/i9s1u/src/generator/context.jl:169
 [3] build!(ctx::Context)
   @ Clang.Generators ~/.julia/packages/Clang/i9s1u/src/generator/context.jl:160
 [4] top-level scope
   @ /workspaces/Docker/Julia/Raylib/jll/wrap.jl:22

```

Do you have any idea why this is happening?

---

<div class="post-metadata">

**Author:** ![dylanxyz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dylanxyz/32/36646_2.png) [@dylanxyz](https://discourse.julialang.org/u/dylanxyz)\
**Post date:** [November 28, 2022, 1:19pm UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/28 "2022-11-28T13:19:10Z")

</div>

If you want to use raylib with Julia, someone already made a Julia wrapper for it:

[chengchingwen/Raylib.jl: Julia wrapper for the raylib videogames programming library (github.com)](https://github.com/chengchingwen/Raylib.jl)

It works pretty well. See this example:

[NanoVG.jl/raylib.jl at main · dylanxyz/NanoVG.jl (github.com)](https://github.com/dylanxyz/NanoVG.jl/blob/main/examples/raylib/raylib.jl)

---

<div class="post-metadata">

**Author:** ![HandyKurniawan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/handykurniawan/32/42607_2.png) [@HandyKurniawan](https://discourse.julialang.org/u/HandyKurniawan)\
**Post date:** [November 29, 2022, 11:27am UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/29 "2022-11-29T11:27:32Z")

</div>

Hi @dylanxyz, thank you for the response 🙂

What I am trying to do here is to learn about using Clang.jl as a wrapper. Because I stumbled upon a problem when I was trying to make a wrapper for cuQuantum. There is already JLL for this: [GitHub - JuliaBinaryWrappers/cuQuantum\_jll.jl](https://github.com/JuliaBinaryWrappers/cuQuantum_jll.jl). But I think something is missing, which makes an error.

So, technically it should be easier for me to build it. But, as I am still new to Julia and cuQuantum, I want to see more examples of how Clang.jl can be used.

If you have any information that someone is already made the wrapper for this project, it would be awesome 🙂

---

<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:** [December 4, 2022, 7:43am UTC](https://discourse.julialang.org/t/clang-vs-cbindinggen/56693/30 "2022-12-04T07:43:50Z")

</div>

Fixed by [Fix an oversight of #393 · JuliaInterop/Clang.jl@b5b2b58 · GitHub](https://github.com/JuliaInterop/Clang.jl/commit/b5b2b581e879a13710525738cb2c7aa42dd2f2b5)

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