Giter Site home page Giter Site logo

crystal_library's Introduction

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

Change CRYSTAL_ROOT to match your Crystal distribution (it's /opt/crystal by 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:

  1. Some crystal code. See src/counter.cr for a basic example.

  2. 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.

  3. A hack to avoid the generation of a main function 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.

  4. A C header to declare the function signatures to C. See src/counter.h.

  5. Use the library. See examples in test and Makefile for how to compile and link them.

Hack

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 CRYSTAL_PATH 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 always overwrites CRYSTAL_PATH.

crystal_library's People

Contributors

ysbaddaden avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

b-rich

crystal_library's Issues

PoC with crystal 0.24.2

Tried your PoC with crystal 0.24.2 on linux 64 bits. Alas did not work, there is no compile option, only a build option. With the build option it does not work

user1 ~/download/try/crystal_library (master) $ make CRYSTAL_ROOT=/opt/crystal
CRYSTAL_PATH="src:/opt/crystal/share/crystal/src" /opt/crystal/bin/crystal build \
	--release --cross-compile -o libcounter src/libcounter.cr
cc 'libcounter.o' -o 'libcounter'  -rdynamic  -lpcre -lgc -lpthread /opt/crystal/share/crystal/src/ext/libcrystal.a -levent -lrt -ldl -L/usr/lib -L/usr/local/lib
ar rcs libcounter.a libcounter.o
user1 ~/download/try/crystal_library (master) $ make test
cc test/counter_test.c ./libcounter.a /opt/crystal/share/crystal/src/ext/libcrystal.a -lgc -ldl -levent -lpcre -o counter_test
./libcounter.a(libcounter.o): In function `main':
/opt/crystal/share/crystal/src/crystal/main.cr:134: multiple definition of `main'
/tmp/ccowU1tv.o:counter_test.c:(.text+0x0): first defined here
/usr/bin/ld: cannot find -lgc
collect2: error: ld returned 1 exit status
Makefile:18: recipe for target 'counter_test' failed
make: *** [counter_test] Error 1

Update the POC

Hi :)
Now that crystal has support for not generating main , would it be easier to do this with less steps ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.