本文共 1944 字,大约阅读时间需要 6 分钟。
接前面 Python与C相互调用(见 http://www.linuxidc.com/Linux/2012-02/55040.htm ),继续一点一点学习,顺便记录下笔记。
大致计划如下(测试平台仅限于Windows 和 Linux,编译器限于 MSVC、Mingw及linux gcc):
传统方式调用 C 模块
用 ctypes 调用C动态库
如有精力和兴趣,简单熟悉一下swig、boost.python, sip, shiboken
用 C 调用 Python
如果只是简单地调用系统调用或c函数,应该考虑使用ctypes。但传统方式更通用,比如C模块中同时要调用python。
简单例子
示例代码
//hellomodule.c
#include
#include
static PyObject * message(PyObject *self, PyObject *args)
{
char *src, res[100];
if (! PyArg_Parse(args, "(s)", &src))
{
return NULL;
}
else
{
strcpy(res, "Hello,");
strcat(res, src);
return Py_BuildValue("s", res);
}
}
static struct PyMethodDef methods[] =
{
//导出到Python中的名字、函数指针,参数类型,函数描述
{"message", message, 1, "descript of message"},
//结束
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC inithello()
{
// 导出的模块名,前面结构数组的指针
(void) Py_InitModule("hello", methods);
}
先定义一个函数,函数的参数和返回值都是PyObject* 类型。(函数内需要将其转成C语言的类型,最后转成PyObject* 类型返回)
定义一个结构数组,包含我们模块内函数的信息
定义一个 init 函数,该函数在模块被导入时执行。前面的 PyMODINIT 隐藏windows下__declspec(dllexport)这个东西
编译
用 msvc 编译
cl /LD hellomodule.c /ID:\python26\include /LIBPATH:D:\python26\libs D:\python26\libs\python26.lib /ohello.pyd
用 mingw 编译
gcc hellomodule.c -shared -ID:\Python26\include -LD:\Python26\libs -lpython26 -o hello.pyd
linux 下编译
gcc -shared hellomodule.c -I/usr/include/python2.6 -ohello.so -L/usr/lib/python2.6 -lpython2.6
采用distutils方式
前面都是直接调用编译器来生成C模块,有时输这么多命令总是不太方便。幸好,Python 在这方面提供了一个工具。我们只需要编写一个简单的Python文件:
#setup.py
from distutils.core import setup, Extension
example_mod = Extension('hello', sources = ['hellomodule.c'])
setup(name = "hello",
version = "1.0",
description = "A sample extension module",
ext_modules = [example_mod], #setup()中,只有这行是必须的
)
然后执行python setup.py build
即可。
也可以直接执行python setup.py install
将模块安装到Python路径下
测试运行
import hello
print hello.message("OK")
备忘:小插曲,采用msvc编译后,运行时始终找不到 message 函数。最终发现和注释中有中文有关(可能是和unix的换行符结合使用的问题),细节部分序进一步试验。
重新审视例子
包含#include
方法函数(主角)PyObject* func(PyObject* self, PyObject * args)
结构数组 (包含函数信息)
模块初始化函数 (注册结构数组)
转载地址:https://blog.csdn.net/weixin_40006779/article/details/110343124 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!