[gambit-list] Creating scheme modules with functions callable from C main program

Mikael mikael.rcv at gmail.com
Fri Aug 31 14:49:26 EDT 2012


Hi Andrew,

It's a really central and recurring question you raised. If you'd want to
share your wisdom on this on the Wiki in a way that would have been of help
to you in the first place would be great.

Ah btw, regarding C stack frames, remember you need to rewind them in the
right order and that they never GC. Gambit does not throw an exception when
rewinding in the wrong order (for performance reasons I suppose), but
generally the app fails or something, don't remember.

Brgds,
Mikael

2012/7/3 Andrew Suttles <andrew.suttles at gmail.com>

> Folks -
>
>
> I am writing a simple Windows native app in C using Win32 libraries.  I'd
> like to to have some of my Windows callback functions call routines that
> I've written (or will write) in GambitC (because scheme is more fun to
> write and maintain than C).  I've found material on how to call functions
> written in C in the scheme interpreter process, but not the other way
> around.  In other words, when I compile a module of scheme functions into
> an object file, I don't have a header file to include in my main program
> written in C.  How do I call those scheme functions?
>
> Thanks again for the help.  I'm fairly new to Scheme and I'm *very new* to
> GambitC, so please excuse the the naivete of my question.
>
> Andrew
>


2012/7/3 Marc Feeley <feeley at iro.umontreal.ca>

>
> On 2012-07-03, at 10:09 AM, Andrew Suttles wrote:
>
> > Folks -
> >
> >
> > I am writing a simple Windows native app in C using Win32 libraries.
>  I'd like to to have some of my Windows callback functions call routines
> that I've written (or will write) in GambitC (because scheme is more fun to
> write and maintain than C).  I've found material on how to call functions
> written in C in the scheme interpreter process, but not the other way
> around.  In other words, when I compile a module of scheme functions into
> an object file, I don't have a header file to include in my main program
> written in C.  How do I call those scheme functions?
>
> To expose a C function to Scheme you use c-lambda as in
>
> (define puts (c-lambda (char-string) int "puts"))
>
> The string "puts" is the name of the function in C, and the Scheme puts
> global variable is what is used to call the function from Scheme code, i.e.
> (puts "hello!").
>
> To expose a Scheme function to C you use c-define as in
>
> (c-define (write-log s) (char-string) void "write_log" ""
>   (display "log: ")
>   (display s))
>
> The string "write_log" is the name of the C function to call to execute
> the write-log Scheme function.  In general you will also want to have a
> header file, say log.h which contains the prototype of this function:
>
> /* File: log.h */
> void write_log(char *s);
>
> So a module of C code using the write_log function will look like:
>
> #include "log.h"
>
> int main()
> {
>   write_log("hello\n");
>   return 0;
> }
>
> Don't forget that you need to initialize the Scheme program and runtime
> system by firs calling ___setup (see the example in examples/pthread).
>
> Marc
>
> Marc
>
> _______________________________________________
> Gambit-list mailing list
> Gambit-list at iro.umontreal.ca
> https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20120831/e660448c/attachment.htm>


More information about the Gambit-list mailing list