1. 必須先安裝 MinGW 軟體
   2. 安裝 MinGW 後,須手動 添加 {MinGW Home}\bin 到系統變數 PATH 裡
   3. 準備 setup.py (*1) 與 要包裝的 C 的檔案,這裡以 PyMod.c(*2) 為例
   4. 打開 cmd line
   5. → python.exe setup.py build -c mingw32
   6. build 完後會產生 build 資料夾 在當前路徑下,內有 lib.win32-2.4 與 temp.win32-2.4 二個資料夾, pyd 檔在 lib.win32-2.4
   7. 準備一個 測試用的 py 檔 , testPyd.py (*3),用來測試 pyd 檔是否可以使用



(*1) setup.py
from distutils.core import setup, Extension
 
module1 = Extension('PyMod', sources = ['PyMod.c'])
 
setup (name = 'PackageName',
        version = '1.0',
        description = 'This is a demo package',
        ext_modules = [module1])

(*2) PyMod.c
/*
   This program makes interface of two C functions to Python methods.

   Project: PC FARM Computing Centre ACDEMIA Sinica
 
   last update: Sep. 10, 1997

   Problem:

       There are two C functions named:
           int   CFunction1(char*, int, float);
           char* CFucntion2(long int, double);
       We want to have two method in Python module 'test':
           Method1(...) ---- which associated with CFunction1(...)
           Method2(...) ---- which associated with CFunction2(...)
*/

#include <stdlib.h>
#include <stdio.h>
#include "c:/portablepython1.0/include/Python.h"

/*******************************************************
  Declaration of C functions
********************************************************/

// a C function....
// just print the argument which user inputs.
int CFunction1( char* ptr, int a, float b){
   printf("string: %s\n",ptr);
   printf("int: %i   float: %f\n",a,b);
   return 1;
}

// another C function....
// just print the argument which user inputs.
char* CFunction2(long int a, double b){
   static char string[] = "CFunction2";
   printf("long int: %d    double: %g\n",a,b);
   return string;
}

/********************************************************
  Interface functions
********************************************************/

// interface function for CFunction1
static PyObject* Call_CFunction1(PyObject *self, PyObject* args){
   char *pstr;
   int a, result;
   float b;
   if(!PyArg_ParseTuple(args,"sif",&pstr,&a,&b)) return NULL;
   result = CFunction1(pstr,a,b);
   return Py_BuildValue("i",result);
}

// interface function for CFunction2
static PyObject* Call_CFunction2(PyObject* self, PyObject* args){
   char* result;
   long int a;
   double b;
   if(!PyArg_ParseTuple(args,"ld",&a,&b)) return NULL;
   result = CFunction2(a,b);
   return Py_BuildValue("s",result);
}

/***********************************************************
  Regiestration table
***********************************************************/
static struct PyMethodDef test_method[] = {
  {"Method1", Call_CFunction1, 1},
  {"Method2", Call_CFunction2, 1},
  {NULL,NULL}
};

void initPyMod(void){
   Py_InitModule("PyMod", test_method);
}

(*3) testPyd.py
import PyMod

a = PyMod.Method1("test", 10, 20.03)
b = PyMod.Method2(1234567890, 3.14159265)


參考文件:
   1. Python 編譯 pyd
   2. MinGW 官網
   3. MinGW Download

    全站熱搜

    雪 薄草 發表在 痞客邦 留言(0) 人氣()