标签:
Python调用C库比较简单,不经过任何封装打包成so,再使用python的ctypes调用即可。
1. C语言文件:pycall.c
#include <stdio.h> #include <stdlib.h> int foo(int a, int b) { printf("you input %d and %d\n",a,b); return a+b; }
2. gcc编译成动态库libpycall.so: gcc -o libpycall.so -shared -fPIC pycall.c
3. python调用动态库的文件:pycall.py
import ctypes ll = ctypes.cdll.LoadLibrary lib = ll("./libpycall.so") num = lib.foo(1,3) print num print ‘***finish****‘
4. 运行结果
you input 1 and 3 4 ***finish****
标签:
原文地址:http://www.cnblogs.com/ryuham/p/4976627.html