Chapter 10: C Bindings
There are lots of useful C libraries out there. It’s important that we make use of them instead of rewriting every single of them.
In Crystal, It’s super easy to use existing C libraries with bindings. Even Crystal itself uses C libraries.
For example Crystal uses
Like I said it’s super easy to write bindings for C. Crystal itself links to
With just 3 lines of code you we’re linked to
libpcre:) We use
libkeyword to group functions and types that belong to a library. And it’s a good convetion to start with
Libfor your C library declarations.
Next we bind to C functions with the
type Pcre = Void*
fun compile = pcre_compile(pattern : UInt8*, options : Int, errptr : UInt8**, erroffset : Int*, tableptr : Void*) : Pcre
Here we binded to
libpcres compile function with the matching types. Now we can easily access this function in our Crystal code.