python ctypes - python调用C语言库

转自:http://blog.sina.com.cn/s/blog_967817f20101a958.html


from ctypes import *

so = CDLL('./foo.so')

myprint = so.myprint
myprint.argtypes = [POINTER(c_char)]    #arguments types
myprint.restype = c_char_p              #return type
res = myprint("hello")
print res

 

add = so.add
add.argtypes = [c_float, c_float]
add.restype = c_float
print add(1.2, 3.3)

 

from ctypes.util import find_library

libc = find_library('c')
libc_so = CDLL(libc)
printf = libc_so.printf
printf("%d\n",2*2)


你可能感兴趣的:(c,python)