Python extension

xiaoxiao2021-03-06  101

1.

Write an Example.c

/ /< --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/ * File: eXample.c * /

/ * A global variable * /

Double foo = 3.0;

/ * Compute the greatest common divisor of positive integers * /

INT GCD (int X, int y) {

INT G;

g = y;

While (x> 0) {

g = x;

x = y% x;

y = g;

}

Return G;

}

/% ----------------------------

2.

Write Example.i

/ /< -------------Example.i ----------------

/ * File: eXample.i * /

% Module EXAMPLE

Extern Int GCD (INT X, INT Y);

Extern double foo;

/) --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

3.

Run the following command, you will generate an example_wrap.c:

SWIG -PYTHON EXAMPLE.I

4.

Create a Windows DLL project in VC6 (notice that not console), join the example.c and example_wrap.c,

And set the generated DLL name to _example.dll (note that there is an underscore), then compile.

5.

Copy the DLL of the previous step to the corresponding directory of Python.

6.

Use Example in Python:

>>> IMPORT EXAMPLE

>>> Print example.cvar.foo

3.0

>>> a = esample.gcd (42, 23)

>>> Print A

1

转载请注明原文地址:https://www.9cbs.com/read-100019.html

New Post(0)