我使用的是vs2008和python3.3以及swig3.0.2
例子采用swig中的simple例子
把代码贴上:
/* 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;
}
以下是指令文件,也称接口文件
/* File : example.i */
%module example
%inline %{
extern int gcd(int x, int y);
extern double Foo;
%}
下面开始具体操作步骤:
1.运行cmd,找到swig.exe的目录,输入swig -python example.i 注:写明example.i的路径
运行成功后会在c文件目录下生成两个文件:example.py 和example_wrap.c
2.在vs中新建工程,将example.c 、example_wrap.c、example.i添加到工程,同时设置工程-属性-链接器-常规-输出 为$(OutDir)\_$(ProjectName).pyd
3.debug工程,会在debug目录下生成_工程名.pyd
4.将上述.pyd文件放置到example.py目录下,在idle中运行,即可使用c文件中的函数。
可以参考如下帖子:http://bbs.csdn.net/topics/300099682