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

Marc Feeley feeley at iro.umontreal.ca
Tue Jul 3 10:21:47 EDT 2012


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




More information about the Gambit-list mailing list