Static/Shared libraries in Crystal
Write C static (or shared) libraries in Crystal. This is just a Proof of Concept that exemplifies how to expose a Crystal library to the outside world, eg: C or any language with bindings to C.
Build the library
CRYSTAL_ROOT to match your Crystal distribution (it's
default on Linux):
$ make CRYSTAL_ROOT=/opt/crystal
You may now test that it works:
$ make test
How does it work?
We need Crystal 0.18.0+ and:
Some crystal code. See
src/counter.crfor a basic example.
An API, thankfully written in Crystal to expose the Crystal internals as proper C functions with symbols that won't be mangled. See
src/libcounter.cr. You'll notice that it requires a bunch of boilerplate and casting objects with Box to exchange references as void pointers with the outside world.
A hack to avoid the generation of a
mainfunction by Crystal, and have an alternative function that must be manually called from the outside world to initialize the GC, Crystal constants, etc. See Hack below.
A C header to declare the function signatures to C. See
Use the library. See examples in
Makefilefor how to compile and link them.
In order to compile, we need to avoid loading the original
main.cr from the
Crystal, and replace it with. This PoC does that by hacking the
environment variable so it references the local
src folder first. This will
work with distributions of Crystal, but won't from a Git clone of Crystal, which