在python中使用ctypes模塊調用c函數,具體方法如下:
from ctypes import * #導入ctypes模塊
handle = cdll.LoadLibrary('libtest.so')
func = handle.printStr
func.argtypes = (c_char_p,c_char_p)
func.restype = c_char_p
tmp = handle.printStr("hello".encode("utf-8"),"world".encode("utf-8"))
print(tmp.decode("utf-8"))
輸出結果為:
helloworld