next up previous contents
Next: 8. Running QScheme Up: QScheme Documentation Previous: 6. QScheme procedures   Contents

Subsections

7. Foreign function interface

QScheme provides a way to dynamicaly load dynamic libary, to call functions of that libary and to share variables with libraries.

7.1 Loading dynamic library

To load

(load-library name)-> <boolean>

Dynamically try to load library. If dynamic linking is successfull and the library contains a function like scm_init_name QScheme will call this function to initialise the module. (See scm_init_regex in regex.c for an example)

7.2 Calling a foreign function

To call a foreign function, you will first have to declare the foreign function for QScheme. For example, this is the delaration of the system and printf functions from the libc:

(define system (make-extfunc *lib* :int "system" '(:string)))

(define printf (make-extfunc *lib* :void "printf" '(:string . :any)))

After that, you can use system and printf just like if it was a part of QScheme:

> (printf "May I ? %d %s\nIt works...\n" 10 "hello world") 

May I ? 10 hello world It works...

>

(make-extfunc libname ret-type ext-name arglist)-> <extfunc>

Create a new scheme object which can be use to call the foreign function ext-name

7.3 Using a foreign variable

Suppose you want to share variable value between C and QScheme. In your C code, you have the following:

int shared_var;

void test_func() {

  printf("shared_var = %d\n", testvar_w);

}

And you declare it in QScheme like this:

(define shared-var (make-extern-variable *lib* :int "shared_var")

(define test-func (make-extfunc *lib* :void "test_func" '())

Then you can get and set values like this:

> (set! shared-var 100)

> (test-func)

shared_var = 100


next up previous contents
Next: 8. Running QScheme Up: QScheme Documentation Previous: 6. QScheme procedures   Contents
Daniel Crettol
1999-07-19