Embed Racket in C and call Racket procedure

416 views
Skip to first unread message

Anthony Sterrett

unread,
Mar 30, 2016, 10:02:24 PM3/30/16
to Racket Users
I appear to be swimming upstream. There are all sorts of tutorials and references for *extending* Racket with C, but precious little about *embedding* it. Unfortunately, I require the latter.

I have a file hw.rkt with the following contents:

#lang racket/base

(provide hw)

(define (hw)
(displayln "Hello, world!"))

I would like to write a C program which uses an embedded Racket runtime to load and call the (hw) procedure.

Resources I've found so far:

* http://docs.racket-lang.org/inside/embedding.html#%28part._3m_.Embedding%29

I have built and run the example program successfully, and thereby learned how to call a procedure provided by racket/base. I assume the same pattern applies for any library compiled with raco ctool. My efforts to apply this knowledge to hw.rkt have so far failed me, though.

* http://docs.racket-lang.org/raco/c-mods.html

This page appears to say that what I want to do is *possible*, and tells me how to compile hw.rkt to a C file (which I have successfully done), but not how to use it afterward.

I have asked this question on Stack Overflow and on Reddit, to no avail:

http://stackoverflow.com/q/36286921/2209270

https://www.reddit.com/r/Racket/comments/4ci29u/call_userprovided_racket_procedure_from_c_xpost/

The Stack Overflow link also includes what I've tried so far, and the error messages I've received for my efforts.

An ideal answer would provide an example program in C that I could copy, compile, and run. An acceptable answer would tell me which C functions I'm supposed to use, and how to invoke them.

I will appreciate any assistance you're able to render. Thank you.

Matthew Flatt

unread,
Mar 30, 2016, 10:33:35 PM3/30/16
to Anthony Sterrett, Racket Users
You were close with

((dynamic-require 'hw 'hw))

but you're missing a quote:

((dynamic-require ''hw 'hw))


When you make "base.c" as

raco ctool --c-mods base.c hw.rkt

then it's like

(module hw racket/base
(provide hw)
(define (hw) (displayln "Hello, world!")))

at a REPL, and you'd require that module with

(require 'hw)


When using `dynamic-require`, the module name must be quoted, which
means an odd-looking extra quote in this case:

(dynamic-require ''hw ....)

Along similar lines, instead of

v = scheme_intern_symbol("hw");
scheme_namespace_require(v);

you'd need to use

v = scheme_make_pair(scheme_intern_symbol("quote"),
scheme_make_pair(scheme_intern_symbol("hw"),
scheme_make_null()));
scheme_namespace_require(v);

The enclosed "hw.c" shows how to use scheme_dynamic_require(), which is
similar and probably what you want.


The documentation for `raco ctool --c-mods` doesn't explain what it
does with a file like "hw.rkt" --- i.e., that it's like declaring a
module 'hw, and that any connection to a "hw.rkt" file is discarded ---
and I'll work on the documentation.
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
hw.c

Anthony Sterrett

unread,
Mar 31, 2016, 7:18:25 AM3/31/16
to Racket Users, vl.ar...@gmail.com

Works like an absolute charm. Thank you for your help! I really appreciate it. I'm at once chagrined and glad to know that my error was "not quoting things enough". :P

Reply all
Reply to author
Forward
0 new messages