Python调用C库文件的两种方法
2018-01-26 15:32
232 查看
1. 使用dlopen函数调用库文件然后编译成python扩展库
1)$ vim func.c
#include <stdio.h> int sum(int a, int b){ printf("%d+%d=", a, b); return a+b; }
2)
$ gcc -o libfunc.so -shared -fPIC func.c
3)
$ vim mydlopen.c
#include <Python.h> #include <dlfcn.h> #include <stdio.h> #include <time.h> #include <string.h> #define LIBPATH "/root/test/libfunc.so" const int (*funcSum)(int a, int b); static PyObject *getSum(PyObject *self, PyObject *args) { int a=0; int b=0; if (!PyArg_ParseTuple(args, "ii", &a, &b)) { PyErr_SetString(PyExc_TypeError, "parms error"); return NULL; } void *handle = dlopen(LIBPATH, RTLD_LAZY); if (handle == NULL) { return NULL; } funcSum = dlsym(handle,"sum"); int sum = funcSum(a, b); return Py_BuildValue("i", sum); } static PyMethodDef myMethods[] = { {"getSum", getSum, METH_VARARGS}, {NULL, NULL} }; void initmydlopen() { Py_InitModule("mydlopen", myMethods); }
4)
$ vim setup.py
from distutils.core import setup, Extension MOD = "mydlopen" setup(name=MOD, packages=[], ext_modules=[Extension(MOD, sources=["mydlopen.c"], include_dirs=["/usr/include"], library_dirs=["/usr/lib64"], libraries=["dl"])])
5)
$ python setup.py install
running install running build running build_ext building 'mydlopen' extension gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/include -I/usr/local/include/python2.7 -c mydlopen.c -o build/temp.linux-x86_64-2.7/mydlopen.o mydlopen.c:36: 警告:函数声明不是一个原型 gcc -pthread -shared build/temp.linux-x86_64-2.7/mydlopen.o -L/usr/lib64 -L/usr/local/lib -ldl -lpython2.7 -o build/lib.linux-x86_64-2.7/mydlopen.so running install_lib copying build/lib.linux-x86_64-2.7/mydlopen.so -> /usr/local/lib/python2.7/site-packages running install_egg_info Removing /usr/local/lib/python2.7/site-packages/mydlopen-0.0.0-py2.7.egg-info Writing /usr/local/lib/python2.7/site-packages/mydlopen-0.0.0-py2.7.egg-info
6)
$ python
Python 2.7.13 (default, Jun 27 2017, 13:39:52) [GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import mydlopen >>> mydlopen.getSum(1,3) 1+3=4
2. 利用ctypes库直接调用库文件
1)$ python
Python 2.7.13 (default, Jun 27 2017, 13:39:52) [GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import ctypes >>> dl = ctypes.cdll.LoadLibrary >>> lib = dl("./libfunc.so") >>> lib.sum(1,4) 1+4=5
相关文章推荐
- Python 中调用其他文件的两种方法 input 和raw_input
- python 调用系统命令的两种方法
- Shell脚本中调用、引用、包含另外一个脚本文件的两种方法
- Shell中调用/引用/包含另外的脚本文件的两种方法
- Genymotion调用本地文件的两种方法
- Python中文件遍历的两种方法
- [C#] 计算大文件的MD5的两种方式(直接调用方法计算,流计算-适用于大文件)
- python文件调用另一个python class文件中的方法
- python调用so的库文件方法
- python文件中的__name__=='__main__'的使用及调用其他py文件中的函数方法
- Python对象方法两种调用方式
- shell中调用另外的脚本文件的两种方法
- jQuery ajax调用后台aspx后台文件的两种常见方法(不是ashx)
- python下载文件的两种方法
- Python遍历目录文件的两种方法
- java 执行命令、调用bat文件、调用外部程序的两种方法
- Makefile文件中调用python文件及perl文件的方法
- Python 调用 Stanford Parser 两种方法
- 在python中调用dll,并将dll以及python合并打包成一个exe文件的方法
- 用两种不同的方法导出ORACLE 查询数据为CSV 文件 (python 代码 与 使用 utl_file 包)