一、文件:手写了三个文件:
1. add_function.h:
float add_function(float, float);
2. add_function.c:
float add(float a, float b){
return a+b;
}
3. add_function.i:
/* file: add_function.i */
// assgin the module name, which will be called in python. for example, import arith
%module arith
%{
/* Everything in this block will be copied in the wrap file. we need "add_function.h" to use the function "add" in add_function.c, so we need to include the head file.
#include "add_function.h"
%}
// list all the functions to be interfaced.
float add(float a, float b);
//or else, we can use
//%include "add_function.h"
//to automatically include all the functions declared in "add_function.h"
为了方便,三个文件放到了同一个文件夹。
二、 步骤
1. 如果没有安装vs,那么,下载一个专门为PYTHON编译C、C++代码的VS工具,其实就是VS用于编译C和C++代码的命令以及windows下的库。有了vs,可以用界面的方式来编译,而使用这套工具,则可以只用命令行来进行编译。
https://www.microsoft.com/en-us/download/details.aspx?id=44266
Microsoft Visual C++ Compiler for Python 2.7
这个工具仅用于python 2.7。下载解压后,里边既有x86的库,也有x64的库。使用vcvarsall.bat来设置环境变量,使得你的程序可以方便的找到相应的包含文件和库。
注意,如果是x86(32位的操作系统),在命令行运行命令: vcvarsall.bat x86;如果是x64(64位的操作系统),在命令行运行命令:vcvarsall.bat x64
2. 我们还需要下载一个swig工具。
http://www.swig.org/download.html
请下载windows专用的包,直接解压得到预编译好的exe文件。包的名字类似:swigwin-版本号。
然后,解压该包,将其中swig.exe所在的目录加入到环境变量path中去,这样可以在任意工作目录调用swig.exe。
3. 搭建好环境,安装好swig工具之后,我们开始正式的生成可用于python调用的库pyd。
这第一步就是手写一个.i文件,这里就是上边提供的add_function.i文件。主要包含三个基础的部分:
1. 要用于导入的module的名称,格式为 %module name,这样以后在python中import name即可使用
2. 列举编译源C文件,也就是add_function.c文件需要的头文件。写在%{与%}之间,这一段要被拷贝到新的wrap.c中,所以直接写C语言的格式,也即是#include "XX.h"或者#include <XX.h>
3. 列举(声明)所有要放到库中的api函数。写法与在头文件中声明一个函数是一样的。或者使用%include "XX.h",直接导入这个头文件中声明的所有函数和变量。
4. 复杂的用法请参考swig的文档。
&nbs
1. add_function.h:
float add_function(float, float);
2. add_function.c:
float add(float a, float b){
return a+b;
}
3. add_function.i:
/* file: add_function.i */
// assgin the module name, which will be called in python. for example, import arith
%module arith
%{
/* Everything in this block will be copied in the wrap file. we need "add_function.h" to use the function "add" in add_function.c, so we need to include the head file.
#include "add_function.h"
%}
// list all the functions to be interfaced.
float add(float a, float b);
//or else, we can use
//%include "add_function.h"
//to automatically include all the functions declared in "add_function.h"
为了方便,三个文件放到了同一个文件夹。
二、 步骤
1. 如果没有安装vs,那么,下载一个专门为PYTHON编译C、C++代码的VS工具,其实就是VS用于编译C和C++代码的命令以及windows下的库。有了vs,可以用界面的方式来编译,而使用这套工具,则可以只用命令行来进行编译。
https://www.microsoft.com/en-us/download/details.aspx?id=44266
Microsoft Visual C++ Compiler for Python 2.7
这个工具仅用于python 2.7。下载解压后,里边既有x86的库,也有x64的库。使用vcvarsall.bat来设置环境变量,使得你的程序可以方便的找到相应的包含文件和库。
注意,如果是x86(32位的操作系统),在命令行运行命令: vcvarsall.bat x86;如果是x64(64位的操作系统),在命令行运行命令:vcvarsall.bat x64
2. 我们还需要下载一个swig工具。
http://www.swig.org/download.html
请下载windows专用的包,直接解压得到预编译好的exe文件。包的名字类似:swigwin-版本号。
然后,解压该包,将其中swig.exe所在的目录加入到环境变量path中去,这样可以在任意工作目录调用swig.exe。
3. 搭建好环境,安装好swig工具之后,我们开始正式的生成可用于python调用的库pyd。
这第一步就是手写一个.i文件,这里就是上边提供的add_function.i文件。主要包含三个基础的部分:
1. 要用于导入的module的名称,格式为 %module name,这样以后在python中import name即可使用
2. 列举编译源C文件,也就是add_function.c文件需要的头文件。写在%{与%}之间,这一段要被拷贝到新的wrap.c中,所以直接写C语言的格式,也即是#include "XX.h"或者#include <XX.h>
3. 列举(声明)所有要放到库中的api函数。写法与在头文件中声明一个函数是一样的。或者使用%include "XX.h",直接导入这个头文件中声明的所有函数和变量。
4. 复杂的用法请参考swig的文档。
&nbs