python中调用c文件中的函数(ubuntu)

gcc -fPIC -shared bob_hash.c -o bob_hash.so      //把bob_hash.c文件编译成动态库

 

函数定义:

             int hash_string(void *tmpstr)

 

python:

>>> from ctypes import CDLL,c_int,c_void_p
>>> bob_hash = CDLL('/home/gby/workspace/bob_hash.so')
>>> hash_string = bob_hash.hash_string
>>> hash_string.argtypes = [c_void_p]
>>> hash_string.restype = c_int
>>> a = hash_string('234')
>>> print a
124

posted @ 2013-08-03 11:49  李伯波  阅读(454)  评论(0编辑  收藏  举报