Python使用ctype调用C链接库


相对于传统的C调用(见 ),使用ctype实在是太简单了

编写一个动态链接库ctype_test.c,

  1. #include <stdlib.h>   
  2.   
  3. int foo(int a, int b)   
  4. {   
  5.     printf("Your input %i and %i\n", a, b);   
  6.     return a + b;   
  7. }  

编译

gcc -o ctype.so -shared -fPIC ctype_test.c  

在python下试用一下吧

  1. import ctypes   
  2. ll = ctypes.cdll.LoadLibrary # 我这是在linux下,windows调用windll之类的   
  3. lib = ll("./ctype.so")   
  4. lib.foo(13)  

相关内容