用ctypes做简单的c调用还是蛮方便的
hello.c
编译动态链接库
$ gcc -o libhello.so -fpic -shared hello.c
返回指针:
>>> import ctypes
>>> hello = ctypes.CDLL("/path/to/libhello.so")
>>> p = hello.foo()
>>> ctypes.c_char_p(p).value
>>> hello world
传入buffer:
>>> b = ctypes.create_string_buffer(12)
>>> hello.foo1(b)
>>> b.value
>>> hello world
hello.c
char *foo(){
char *p = "hello world";
return p;
}
void foo1(char *p){
strcpy(p, "hello world");
}
编译动态链接库
$ gcc -o libhello.so -fpic -shared hello.c
返回指针:
>>> import ctypes
>>> hello = ctypes.CDLL("/path/to/libhello.so")
>>> p = hello.foo()
>>> ctypes.c_char_p(p).value
>>> hello world
传入buffer:
>>> b = ctypes.create_string_buffer(12)
>>> hello.foo1(b)
>>> b.value
>>> hello world