星期六, 十二月 09, 2006

在Mac OS X下尝试ctypes

今天在mac下尝试了一下ctypes,感觉不错,又温习了一下N久没碰的C语言,唯一要注意的就是gcc的编译选项,在Mac下比较怪异。

hello.h
int add(int a, int b);

hello.c
#include "hello.h"
int add(int a,int b)
{
return a+b;
};

编译出hello.o
cc -c hello.c

编译成动态链接库hello.so
cc -bundle -flat_namespace -undefined suppress -o hello.so hello.o

python
from ctypes import *
lib=CDLL("hello.so")
lib.add(1,2)
=>3

参考资料:
http://wiki.woodpecker.org.cn/moin/FeihuTryPyrex
http://www.pgsqldb.org/pgsqldoc-cvs/xfunc-c.html

标签: , , ,

0 Comments:

发表评论

<< Home