当前位置: 代码迷 >> 综合 >> python ctypes - python调用C语言库
  详细解决方案

python ctypes - python调用C语言库

热度:71   发布时间:2023-12-22 08:33:27.0

转自: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)