我是天才
伟大的野心家,实践家; 程序员,技术问题解决者.

导航

 

假设c语言写的二个函数为

void hello_user(const char *name) {
    printf("Hello, %s.\n", name);
    return;
}
void goodbye() {
    printf("Goodbye!\n");
    return;
}

如果想在chibi-scheme中调用c写的函数

第一步,建stub文件

可以建一个test.stub文件,内容如下

(define-c void hello-user (string))
(define-c void (so-long goodbye) ())
默认会把hello-user转化为c语言的hello_user,短横转化为下划线。或者提供别名,c的goodbye在scheme命名为so-long
第二步,加工stub文件
chibi-scheme提供了一个工具chibi-ffi,如果是自己编译的该文件在./tools/chibi-ffi
$ chibi-scheme .../tools/genstubs.scm test.stub
这样会生成一个test.c
第三步,编译成动态链接库
$ gcc -fPIC -shared -I.../include/ -L... -o hello.so hello.c test.c -lchibi-scheme
第四步,加载
在scheme中加载hello.so文件,即可调用上面的函数了
$ chibi-scheme
> (load "./hello.so")
> (hello-user "Taylor")
Hello, Taylor.
> (so-long)
Goodbye!



posted on 2012-02-15 22:02  Genius0101  阅读(807)  评论(0编辑  收藏  举报