Skip to content

Instantly share code, notes, and snippets.

@metacritical
Forked from Papierkorb/000_HOWTO.md
Created May 25, 2024 08:25
Show Gist options
  • Save metacritical/d60c25b2787e5c91895567917f715f2b to your computer and use it in GitHub Desktop.
Save metacritical/d60c25b2787e5c91895567917f715f2b to your computer and use it in GitHub Desktop.

Revisions

  1. @Papierkorb Papierkorb revised this gist Sep 26, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions 000_HOWTO.md
    Original file line number Diff line number Diff line change
    @@ -14,8 +14,8 @@ crystal build --release --cross-compile --prelude ./no_main library.cr

    ```
    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.
    ^ Add this to link into a shared lib
    ^ We want a decent name for the shared lib too.
    ```

    * Build and link the C program:
  2. @Papierkorb Papierkorb revised this gist Sep 26, 2017. No changes.
  3. @Papierkorb Papierkorb revised this gist Sep 26, 2017. 1 changed file with 23 additions and 1 deletion.
    24 changes: 23 additions & 1 deletion 000_HOWTO.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Writing a dynamic library in Crystal, and calling it from C

    * Download all files.
    * Build the Crystal part:

    @@ -11,7 +13,7 @@ crystal build --release --cross-compile --prelude ./no_main library.cr
    * Link the Crystal part into a shared library. The previous command gives you the line, though we have to modify it a bit:

    ```
    gcc foo.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
    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.
    ```
    @@ -30,4 +32,24 @@ gcc -o program program.c -L$PWD -lfoo
    ./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)
    ```

    ## Enjoy!
  4. @Papierkorb Papierkorb created this gist Sep 26, 2017.
    33 changes: 33 additions & 0 deletions 000_HOWTO.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    * 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 foo.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
    ```


    4 changes: 4 additions & 0 deletions library.cr
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    # The function we will call from C. Add more `fun`s as you see fit.
    fun hello_crystal : Void
    puts "Hello from Crystal!"
    end
    79 changes: 79 additions & 0 deletions no_main.cr
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    # Copied from my prelude.cr. Only commented line 50 in this file as change.

    # Entries to this file should only be ordered if macros are involved -
    # macros need to be defined before they are used.
    # A first compiler pass gathers all classes and methods, removing the
    # requirement to place these in load order.
    #
    # When adding new files, use alpha-sort when possible. Make sure
    # to also add them to `docs_main.cr` if their content need to
    # appear in the API docs.

    # This list requires ordered statements
    require "lib_c"
    require "macros"
    require "object"
    require "comparable"
    require "exception"
    require "iterable"
    require "iterator"
    require "indexable"
    require "string"

    # Alpha-sorted list
    require "array"
    require "atomic"
    require "bool"
    require "box"
    require "char"
    require "char/reader"
    require "class"
    require "concurrent"
    require "deque"
    require "dir"
    require "enum"
    require "enumerable"
    require "env"
    require "errno"
    require "ext"
    require "file"
    require "float"
    require "gc"
    # require "gc/null"
    require "gc/boehm"
    require "hash"
    require "iconv"
    require "int"
    require "intrinsics"
    require "io"
    require "kernel"
    # require "main" #### We don't want this!
    require "math/math"
    require "mutex"
    require "named_tuple"
    require "nil"
    require "number"
    require "pointer"
    require "pretty_print"
    require "primitives"
    require "proc"
    require "process"
    require "raise"
    require "random"
    require "range"
    require "reference"
    require "reflect"
    require "regex"
    require "set"
    require "signal"
    require "slice"
    require "static_array"
    require "struct"
    require "symbol"
    require "system"
    require "thread"
    require "time"
    require "tuple"
    require "unicode"
    require "union"
    require "value"
    5 changes: 5 additions & 0 deletions program.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    void hello_crystal(); // Manually declare Crystal `fun`

    int main() {
    hello_crystal(); // Call to Crystal
    }