# Calling SDL2 Functions in Julia

**URL:** <https://discourse.julialang.org/t/calling-sdl2-functions-in-julia/6075>\
**Category:** General Usage\
**Created:** [September 25, 2017, 10:14pm UTC](https://discourse.julialang.org/t/calling-sdl2-functions-in-julia/6075 "2017-09-25T22:14:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeftyGuitar](https://avatars.discourse-cdn.com/v4/letter/l/d2c977/32.png) [@LeftyGuitar](https://discourse.julialang.org/u/LeftyGuitar)\
**Post date:** [September 25, 2017, 10:14pm UTC](https://discourse.julialang.org/t/calling-sdl2-functions-in-julia/6075/1 "2017-09-25T22:14:41Z")

</div>

I am trying to call some SDL2 functions from the SDL2.dll in Julia, but I keep getting unable to load error. I’m not sure what I’m doing wrong. I followed the help files in the calling C and Fortran help files.

```julia
#flags
const SDL_INIT_TIMER = 1
const SDL_INIT_AUDIO = 16
const SDL_INIT_VIDEO = 32
const SDL_INIT_JOYSTICK = 512
const SDL_INIT_HAPTIC = 4096
const SDL_INIT_GAMECONTROLLER = 8192
const SDL_INIT_EVENTS = 16384
const SDL_INIT_NOPARACHUTE = 1048576

sdl2 = dlopen("SDL2.dll")

SDL_Init = ccall(:SDL_Init,"sdl2"), UInt32,(UInt32), flags)

```

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [September 25, 2017, 10:30pm UTC](https://discourse.julialang.org/t/calling-sdl2-functions-in-julia/6075/2 "2017-09-25T22:30:00Z")

</div>

What example did you follow? `ccall` doesn’t have a signature like this, and I can’t even recognize what you are trying to do. What do you think each argument to `ccall` means in your code?

---

<div class="post-metadata">

**Author:** ![LeftyGuitar](https://avatars.discourse-cdn.com/v4/letter/l/d2c977/32.png) [@LeftyGuitar](https://discourse.julialang.org/u/LeftyGuitar)\
**Post date:** [September 25, 2017, 11:16pm UTC](https://discourse.julialang.org/t/calling-sdl2-functions-in-julia/6075/3 "2017-09-25T23:16:37Z")

</div>

I followed the example on the CCall example from the help pages. I changed it, but I still get the same error. SDL\_Init is an unsigned int var that has another unsigned int for its flags.

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [September 26, 2017, 6:47am UTC](https://discourse.julialang.org/t/calling-sdl2-functions-in-julia/6075/4 "2017-09-26T06:47:42Z")

</div>

> [@LeftyGuitar](#):
>
> SDL\_Init = ccall(:SDL\_Init,“sdl2”), UInt32,(UInt32), flags)

Well, there are still at least 3 syntactic error here that don’t depend on `ccall`:

- parentheses are not balanced
- `(UInt32)` is not a tuple, `(UInt32,)` is
- variable `flags` is never defined

There may also be 2 logical errors:

- for `ccall((:func, "lib"), ...)` to work “lib” should be on your loading path; if it’s not, you should use `ccall((:func, "/full/path/to/lib"), ...)` instead, otherwise `ccall` just won’t know where to look for your function
- according to [SDL2 docs](https://wiki.libsdl.org/SDL_Init), `SDL_Init` returns `int` in C and thus `Cint` in Julia, which is an alias to `Int32`, not `UInt32`.

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [September 26, 2017, 1:18pm UTC](https://discourse.julialang.org/t/calling-sdl2-functions-in-julia/6075/5 "2017-09-26T13:18:17Z")

</div>

I have a semi-functional wrap of SDL2 that I made using Clang.jl (that automatically write all the function signatures from the C headers), I can make a repository if you are interested, it’s easier than trying to wrap things by hand.

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [September 26, 2017, 3:43pm UTC](https://discourse.julialang.org/t/calling-sdl2-functions-in-julia/6075/6 "2017-09-26T15:43:01Z")

</div>

Did you have a look at GLFW?! At least when i was deciding for a windowing library, i decided for GLFW for it’s active development and nicely wrapped Julia library. Its Julia wrapper lives in [GLFW.jl](https://github.com/JuliaGL/GLFW.jl)!  
Ignore this, when you actually rather need the wrappers for sound etc, which isn’t part of GLFW.

---

<div class="post-metadata">

**Author:** ![LeftyGuitar](https://avatars.discourse-cdn.com/v4/letter/l/d2c977/32.png) [@LeftyGuitar](https://discourse.julialang.org/u/LeftyGuitar)\
**Post date:** [September 26, 2017, 7:07pm UTC](https://discourse.julialang.org/t/calling-sdl2-functions-in-julia/6075/7 "2017-09-26T19:07:53Z")

</div>

@dfdx  
I see. I think I am getting a better understanding of how to use the ccall function.

@jonathanBieler  
That would be nice. I tried searching to see if there was already a SDL2 wrapped for Julia, but I didn’t see any.

GLFW is a great library, but the SDL2 has many more functions and is a great multimedia library.
