manpagez: man pages & more
info bigloo
Home | html | info | man
[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

27 The C interface

We call all the pieces of program devoted to the interactions between Scheme and another language a foreign interface. In Bigloo, the foreign interface allows Scheme’s functions and variables to be exported to a foreign language and foreign functions and variables to be imported into the Scheme code. Using the foreign interface requires two kind of operations.

Declarations take place in a special module clause, see Module declaration, and reference to foreign variables within Scheme code requires no special construction. The current release of Bigloo includes a C and a Java interface. The Java connection is specified by the means of a java clause (see section The Java interface). The C interface is active (that is the extern module clauses are read) only when compiling to C. So, when compiling to Jvm the binding declared in an extern clause are not bound.

Connecting Bigloo code with C is generally straightforward. To illustrate this simplicity, let us consider a simple example involving two source files. First a simple C file sum.c containing a single declaration:

int sum(int x, int y) { return x + y; }

Then, let us assume a Bigloo source code main.scm that makes uses of that C function:

(module foo
   (extern (sum::int (::int ::int) "sum"))
   (main main))

(define (main x)
   (print (sum (length x) 10)))

With a Unix installation of Bigloo, this program can be compiled and executed with the following commands:

  $ gcc sum.c -c
  $ bigloo main.scm sum.o -o main
  $ ./main 1 2 3

The connection between Scheme and C is made particularly easy by Bigloo because the programmer is free from inserting conversion between Scheme values and C values. When needed, these are automatically inserted by the compiler.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on March 31, 2014 using texi2html 5.0.

© manpagez.com 2000-2017
Individual documents may contain additional copyright information.