Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Python使用ctype调用C链接库

相对于传统的C调用(见 http://www.linuxidc.com/Linux/2012-02/55037.htm),使用ctype实在是太简单了编写一个动态链接库ctype_test.c,
  1. #include <stdlib.h>   
  2.   
  3. int foo(int a, int b)   
  4. {   
  5.     printf("Your input %i and %i ", 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)