- Download all files.
- Build the Crystal part:
crystal build --release --cross-compile --prelude ./no_main library.cr
^ You can leave this out
^ We want to link ourselves!
^ Use our custom prelude!
- Link the Crystal part into a shared library. The previous command gives you the line, though we have to modify it a bit:
gcc library.o -o libfoo.so -rdynamic -shared -lpcre -lgc -lpthread /usr/lib/crystal/ext/libcrystal.a -levent -lrt -ldl -L/usr/lib -L/usr/local/lib
^ Add this to link into a shared lib
^ We want a decent name for the shared lib too.
- Build and link the C program:
gcc -o program program.c -L$PWD -lfoo
^ We stored our lib right 'here'
^ And we want to link to it
- Run the program:
./program
- Verify (Linux only):
Using ldd program
, we see the dynamic dependencies of our program:
linux-vdso.so.1 (0x00007ffc56d8c000)
libfoo.so (0x00007f80846e9000) <---- Here it is!
libc.so.6 => /usr/lib/libc.so.6 (0x00007f8084343000)
libpcre.so.1 => /usr/lib/libpcre.so.1 (0x00007f80840d0000)
libgc.so.1 => /usr/lib/libgc.so.1 (0x00007f8083e66000)
libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007f8083c48000)
libevent-2.1.so.6 => /usr/lib/libevent-2.1.so.6 (0x00007f80839f2000)
librt.so.1 => /usr/lib/librt.so.1 (0x00007f80837ea000)
libdl.so.2 => /usr/lib/libdl.so.2 (0x00007f80835e6000)
libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x00007f80833cf000)
/lib64/ld-linux-x86-64.so.2 (0x00007f8084909000)
libatomic_ops.so.1 => /usr/lib/libatomic_ops.so.1 (0x00007f80831cc000)
libcrypto.so.1.1 => /usr/lib/libcrypto.so.1.1 (0x00007f8082d4c000)
Hi @Papierkorb Thank you for this gist, can you update the
no_main.cr
file? Looks like is not working on v0.24.1Oh, this trick also works with C#/Mono as well 😄
Outputs: