# C lib bindings on different platforms

**URL:** <https://forum.crystal-lang.org/t/c-lib-bindings-on-different-platforms/5006>\
**Category:** Help & Support\
**Created:** [October 8, 2022, 2:49pm UTC](https://forum.crystal-lang.org/t/c-lib-bindings-on-different-platforms/5006 "2022-10-08T14:49:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![homodeluna](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/homodeluna/32/1755_2.png) [@homodeluna](https://forum.crystal-lang.org/u/homodeluna)\
**Post date:** [October 8, 2022, 2:49pm UTC](https://forum.crystal-lang.org/t/c-lib-bindings-on-different-platforms/5006/1 "2022-10-08T14:49:32Z")

</div>

If I want to write a shards that binds a C library(gtk for example), how should I do to let it support different platforms?  
There are, as far as I known, Three ways:

- One is to bring pre-built libs together with the binding code, each lib for each platform. PIP is going this way, it packs wheels for every architecture and operatimg systems, so you can see numpy variants from linux-arm64 to windows-x86 to mac-amd64 and so on.
- The other is to equip the language with a compiler toolchain, bring source code together with the binding code, and compile the lib locally.Gem is going this way.(but as a Windows user, I frequenty get stuck by errors beyond my level when compiling in the msys environment)
- Or do neither of this. instead, you either re-write every required lib with native Crystal, or assume that the shards user already have the lib exactly as you want. Cargo seems like going this way.  
Which way does Crystal go on? and what should I do to have my bindings compatible to most platforms?

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [October 8, 2022, 3:12pm UTC](https://forum.crystal-lang.org/t/c-lib-bindings-on-different-platforms/5006/2 "2022-10-08T15:12:42Z")

</div>

I’m not sure I follow… For example the standard library binds to libxml2. It doesn’t need to compile anything. It assumes you have libxml installed on your system. For gtk we can assume the user already has the require libraries in their system. If they don’t they get a linker error.

Am I missing something?

Note that unlike Rust, Crystal modules or files aren’t compiled independently.

---

<div class="post-metadata">

**Author:** ![homodeluna](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/homodeluna/32/1755_2.png) [@homodeluna](https://forum.crystal-lang.org/u/homodeluna)\
**Post date:** [October 8, 2022, 3:39pm UTC](https://forum.crystal-lang.org/t/c-lib-bindings-on-different-platforms/5006/3 "2022-10-08T15:39:27Z")

</div>

Therefore I should download or install gtk, place it in `CRYSTAL_LIBRARY_PATH`,and add `@[link("gtk")]` on my lib declaration, get it.

> Note that unlike Rust, Crystal modules or files aren’t compiled independently.

What does this mean? does it mean that I can’t compile a crystal code to a static lib, and link it to crystal core afterwards, but instead I must compile all the code into an executable in just one shot?

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [October 8, 2022, 4:41pm UTC](https://forum.crystal-lang.org/t/c-lib-bindings-on-different-platforms/5006/4 "2022-10-08T16:41:14Z")

</div>

Exactly. There’s no way to compile some Crystal code and later link it with some other Crystal code. Crystal only supports going from an entire program to an executable, linking everything it finds along the way.

---

<div class="post-metadata">

**Author:** ![bcardiff](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/bcardiff/32/3_2.png) [@bcardiff](https://forum.crystal-lang.org/u/bcardiff)\
**Post date:** [October 8, 2022, 5:59pm UTC](https://forum.crystal-lang.org/t/c-lib-bindings-on-different-platforms/5006/5 "2022-10-08T17:59:33Z")

</div>

@homodeluna the Link attribute has some options that integrated with pkg-config to lookup where the library files are located. If the library you use provides a .pc file and is registered in PKG\_CONFIG\_PATH then you need to match the lib name in the Link attribute.

See [Link - Crystal 1.6.0](https://crystal-lang.org/api/1.6.0/Link.html)

---

<div class="post-metadata">

**Author:** ![hugopl](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/hugopl/32/970_2.png) [@hugopl](https://forum.crystal-lang.org/u/hugopl)\
**Post date:** [October 16, 2022, 3:11am UTC](https://forum.crystal-lang.org/t/c-lib-bindings-on-different-platforms/5006/6 "2022-10-16T03:11:32Z")

</div>

> [@homodeluna](#):
>
> If I want to write a shards that binds a C library(gtk for example)

> **[GitHub - hugopl/gtk4.cr: GTK4 bindings for Crystal](https://github.com/hugopl/gtk4.cr)**
>
> GTK4 bindings for Crystal. Contribute to hugopl/gtk4.cr development by creating an account on GitHub.

I never tried it on Windows, but it’s expected that who is trying to compile it to have it proper installed and available through pkg-config or in some default system specific path.
