1.
编写example.c
编写example.c
//<--------example.c------------------
/* File : example.c */
/* A global variable */
double Foo = 3.0;
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;
}
int gcd(int x, int y) {
int g;
g = y;
while (x > 0) {
g = x;
x = y % x;
y = g;
}
return g;
}
//>--------example.c------------------
2.
编写example.i
编写example.i
//<-------------example.i---------------
/* File: example.i */
%module example
%module example
extern int gcd(int x, int y);
extern double Foo;
extern double Foo;
//>-------------example.i---------------
3.
运行如下命令,即会生成example_wrap.c:
swig -python example.i
运行如下命令,即会生成example_wrap.c:
swig -python example.i
4.
在VC6中创建一个windows dll工程(注意不是console),把example.c和example_wrap.c加入工程,
并设置生成的dll名称为 _example.dll(注意要有下划线),然后编译。
在VC6中创建一个windows dll工程(注意不是console),把example.c和example_wrap.c加入工程,
并设置生成的dll名称为 _example.dll(注意要有下划线),然后编译。
5.
将上一步生成的dll拷贝到python相应目录下。
将上一步生成的dll拷贝到python相应目录下。
6.
在python中使用example:
在python中使用example:
>>> import example
>>> print example.cvar.Foo
3.0
>>> a = example.gcd( 42,23)
>>> print a
1
>>> print example.cvar.Foo
3.0
>>> a = example.gcd( 42,23)
>>> print a
1