# 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 `libpcre` for it’s `Regex` implementation.

Like I said it’s super easy to write bindings for C. Crystal itself links to `libpcre` like this

```ruby
@[Link("pcre")]
lib LibPCRE
...
end
```

With just 3 lines of code you we’re linked to `libpcre` :) We use `lib` keyword to group functions and types that belong to a library. And it’s a good convetion to start with `Lib` for your C library declarations.

Next we bind to C functions with the `fun` keyword.

```ruby
@[Link("pcre")]
lib LibPCRE
  type Pcre = Void*
  fun compile = pcre_compile(pattern : UInt8*, options : Int, errptr : UInt8**, erroffset : Int*, tableptr : Void*) : Pcre
end
```

Here we binded to `libpcre`s compile function with the matching types. Now we can easily access this function in our Crystal code.

```ruby
LibPCRE.compile(..)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.crystalforrubyists.com/book/chapter-10-c-bindings.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
