Crystal wrapper generator for C/C++ libraries

Crystal allows you to bind to existing C libraries without writing a single line in C.

[Warning] => struct bitfields ard not supported

[Warning] => Thread are not supported (yet) ?

C bindings

ptr = Pointer(Int32).malloc(1)
ptr.as(Int8*) # :: Pointer(Int8)

Note that a reference has to be kept on the object on crystal side. The reason is that if we don’t do it, and our code doesn’t reference it anymore, the GC will collect it. The C library will of course store the callback, but Crystal’s GC has no way of knowing that.

  • libgen - Libgen aims to ease the port of big C libraries and to provide a convinient way to define re-generable bindings, through config files:
    • Lib generation is parametrized using a YAML or JSON configuration file (how to generate).
    • Bindings definitions are specified in YAML, JSON or Crystal files or in the generation config file directly (what to generate).
  • Bindgen - Standalone C, C++, and/or Qt binding and wrapper generator.

  • bind your own c library
  • crystal lang discussion
Written on August 14, 2020, Last update on September 1, 2020
crystal binding c++