loading...

Calling C from Julia

aibhstin profile image Aibhstin ・1 min read

I recently bought "Grey Hat Python" as part of a Humble Bundle. The book is quite old now but still seems to be well regarded. I started working through the very first example of the book, where it uses ctypes in Python to call printf from libc, and I immediately thought of Julia. The ability to call C code from within Julia is one of it's core strengths. I've decided I will go through the entirety of GHP, but using Julia instead.

I won't include it here but the first example in the book is simply using libc to call printf. Here it is in julia:

julia> n = ccall((:printf, "libc.so.6"), Cint, (Ptr{Cchar},), "Hello world\n")
Hello world
12

I had some difficulty getting it to work initially with a problem that turned out to be my declaration of types. I find the way Julia makes calling C code elegant and I'm looking forward to using and learning Julia more.

Discussion

markdown guide