`
luozhaoyu
  • 浏览: 343488 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

python3.2下调用C动态链接库

阅读更多
python和C,我觉得这简直是无敌的组合啊。一般性的业务逻辑用python快速出模型,而碰到python运行缓慢的操作,则可以通过调用C编译好的链接库来完成。
在python3.2下,可以通过ctype模块单纯的访问C链接库,也可以通过传统方式访问。ctype模块固然方便,但若要求回调的话,还是得用传统方式操作。

今天查阅了官方manual和网上一些链接,感觉有些出处,在这写上一个3.2.2的简单程序:
编辑一个文件lzymodule.c
按官方命名方式,前面是模块名,后面是module.c
#include <Python.h> //必须先include Python.h,Python.h中包含了一些预编译宏,包括了一些stdlib.h之类的头文件

/* 将要调用的函数,我提出来了 */
long func1(long a, long b)
{
    return a + b;
}

/* 方法名字命名规范是“模块名_方法名”,我这里的方法名是system */
static PyObject *
lzy_system(PyObject *self, PyObject *args)
{
    const char *command;
    long a, b;
    int sts;

    if (!PyArg_ParseTuple(args, "ii", &a, &b)) //读入两个整形参数,转成C类型
        return NULL;

    //sts = system(command);
    sts = func1(a, b);
    return PyLong_FromLong(sts); //返回前,需要转成PyObject类型
}

/* 定义模块中的方法 */
static PyMethodDef LzyMethods[] =
{
    {"func", lzy_system, METH_VARARGS, "my first test function"}, //注意func是在python中调用的名字,这里应该取system才规范
    {NULL, NULL, 0, NULL}
};

/* 定义模块 */
static struct PyModuleDef lzymodule =
{
    PyModuleDef_HEAD_INIT,
    "lzy",
    NULL,
    -1,
    LzyMethods
};

/* 这个就相当于主函数了,命名一定要为“PyInit_模块名” */
PyMODINIT_FUNC PyInit_lzy(void)
{
    return PyModule_Create(&lzymodule);
}


之后进行编译
gcc lzymodule.c -shared -I /usr/local/include/python3.2m/ -o lzy.so -fPIC


最后进入python,试试
import lzy
lzy.func(1, 230)


有疑问请跟帖。

注,不愿意gcc的话,试试这个脚本
from distutils.core import setup, Extension
test_mod = Extension('lzy', sources = ['lzymodule.c'])
setup(name = 'lzy',
    version = '1.0',
    description = 'lzy test extension module',
    ext_modules = [test_mod])

python setup.py build
python setup.py install
0
0
分享到:
评论

相关推荐

    python2.7 python3.2

    python2.7 python3.2 包含windows,linux,一共5个分卷

    python3.2 Windows64位.txt

    python3.2 Windows64位安装包下载 (该文件包含了安装包的下载链接)

    python3.2手册

    python3.2 手册 python程序员必备说明书

    Python3.2软件

    Python3.2软件

    Python3.2 教程

    Python3.2实例 基础 教程 如果你是初学者可以看看,很有帮助的

    Python 3.2

    improvements to pdb, the Python debugger countless fixes regarding bytes/string issues; among them full support for a bytes environment (filenames, environment variables) many consistency and behavior...

    Python3.2的MySQL组件

    MySQL-python-1.2.3.win32-py3.2 用于Python3.2连接MySQL数据库 使用方法见 http://www.cnblogs.com/txw1958/archive/2012/07/22/python3-mysql.html

    python3.2的telnet登陆和返回数据获取程序

    这是一个python文件,使用的是python3.2和PyQt4的开发环境,python3.2中有telnetlib模块,这个模块里有相应的登陆和读取远程主机端shell命令行返回数据的函数,这个程序是一个完整的登陆和读取相应数据到界面文本...

    Python调用C#动态链接库 DLL

    Python调用C#动态链接库 DLL 案例 python 版本 3.x dll 版本 .net standard 2.0 调用库 clr 安装 pip install pythonnet 调用接口 clr.AddReference

    python3.2对应的机器学习环境相关库

    python3.2对应的机器学习环境相关库,包括,numpy,matplotlib,scipy,scikit-learn,easy_install文件等 http://blog.csdn.net/u013634684/article/details/48289459安装教程

    python3.2 Windows32位.txt

    python3.2 Windows32位安装包下载 (该文件包含了安装包的下载链接)

    Python_3.2_amd64.msi安装包

    Python_3.2_amd64.msi安装包,64位操作系统的可以安装!

    文件夹分割器(python 3.2)

    python 3.2 写的一个文件夹分割器,平均分割文件到新文件夹。界面为tkinter库。

    python3.2 Mac OS64位.txt

    python3.2 Mac OS64位安装包下载 (该文件包含了安装包的下载链接)

    PyQt4-Python3.2

    平台:Win32 Python 版本:3.2 Qt版本:4.9

    能使用boost.python的MinGw(GCC)动态链接库

    hello_ext.pyd就是python中能直接使用的动态链接库,windows一般以dll为后缀,而python只承认pyd文件. 下面来测试一下: import hello_ext print hello_ext.greet() print hello_ext.add(1,3) hello_ext.vprint() ...

Global site tag (gtag.js) - Google Analytics