步骤0:swig简介
swig是一种可以将C++代码转换为多种脚本语言封装的工具,可以在swig官网www.swig.org下载,解压后将swig.exe的路径添加到环境变量path中即可使用swig
步骤1:准备C++代码
编写需要在Python中调用的C++代码,最好将函数和类的声明统一放到头文件中,函数和类的实现放到源文件中
C++头文件
头文件主要包括:
-
#include
调用(例如#include <iostream>
) - 命名空间指定
using namespace std;
- 函数和类的声明
#include <iostream>
using namespace std;
class example {
private:
int num;
public:
void say_hello(void);
void change(int din);
int get_num();
};
C++源文件
源文件为头文件中函数和类的实现
#include "example.h"
void example::say_hello(void) {
cout << &#