zoukankan      html  css  js  c++  java
  • Windows下为Python编译C扩展模块

    工具:CodeBlocks 13.12

    步骤


    1 打开CodeBlocks新建工程:Shared library   --   c    --  sample    [默认GUN GCC Complier就行]

      右键sample目录,Build options添加以下三项内容:

      Linker settings  -- add  --  C:Python34libspython34.lib     [库文件]

      Search directories  --  Linker  --  C:Python34libs

      Search directories  --  Compiler  --  C:Python34include      ['Python.h'等头文件]


    2 编辑main.c文件

    #include <stdio.h>
    #include <Python.h>
    
    int c_add(int x, int y)     // 正常的C格式
    {
        return x+y;
    }
    
    static PyObject *add(PyObject *self, PyObject *args)   // 以下都是Python.h中的类型、方法
    {
        int a, b;
        if(!PyArg_ParseTuple(args,"ii", &a, &b))
        {
            return NULL;
        }
        return (PyObject*)Py_BuildValue("i", c_add(a,b));
    }
    
    /*模块方法表*/
    static PyMethodDef AddMethods[] = { {"plus", add, METH_VARARGS, "add a and b"}, //add方法在模块中的名字 { NULL, NULL, 0, NULL} };
    /*模块结构*/
    static struct PyModuleDef addmodule = { PyModuleDef_HEAD_INIT, "sample", //模块名 "A add module", //文档字符串 -1, //返回状态 AddMethods       // 上面的方法表 }; PyMODINIT_FUNC PyInit_add(void) { return PyModule_Create(&addmodule); }

    3 编译

    在/bin/Debuge/目录下的libsample.dll就是我们所需要的文件,

    重命名sample.pyd,放到import可以找到的地方,就可以在python中import sample了(一般放在C:Python34DLLs中)

    更具体的看CookBook官方文档

    KEEP LEARNING!
  • 相关阅读:
    判断DataSet为空
    msxml3.dll 错误 '800c0008'
    google Map api地理位置坐标转换
    C# .net中cookie值为中文时的乱码解决方法
    windows pear 安装
    smarty2 设置、变量、函数
    简单模板类
    mysql 1366 插入错误
    Oracle修改账户口令
    C# Winform验证码
  • 原文地址:https://www.cnblogs.com/roronoa-sqd/p/5446304.html
Copyright © 2011-2022 走看看