一、环境搭建

1.安装boost库,配置环境变量D:\boost_1_52_0;D:\boost_1_52_0\stage\lib;

2.修改C:\Users\Administrator\AppData\Local\Microsoft\MSBuild\v4.0下的Microsoft.Cpp.Win32.user.props及Microsoft.Cpp.x64.user.props文件

D:\boost_1_52_0\;

D:\boost_1_52_0\stage\lib;

3.安装python配置环境变量D:\Python27\include;

2.修改C:\Users\Administrator\AppData\Local\Microsoft\MSBuild\v4.0下的Microsoft.Cpp.Win32.user.props及Microsoft.Cpp.x64.user.props文件

节点处添加D:\boost_1_52_0\;

节点处添加D:\boost_1_52_0\stage\lib;

二、代码示例

创建dll工程

=====================================

hello.cpp

#include

using namespace std;

class hello {


public:

string hestr;

private:

string title;

public:

hello(string str){


this->title = str;

}

string get() {


return this->title;

}

};

==========================================

hc.h

#pragma  once

#include "hello.cpp"


class hc:public hello{


public:

hc(string str);

~hc(void);

int add(int a,int b);

};

=============================================

#include "hc.h"


hc::hc(string str):hello(str){


}


hc::~hc(void){


}


int hc::add(int a,int b){


return a+b;

}

========================

pyhello.cpp 连接桥梁

#include "hc.h"  

#include  

BOOST_PYTHON_MODULE(hello_ext)  

{  

using namespace boost::python;

class_("hello",init())  

.def("get",&hello::get)  

.def_readwrite("hestr",&hello::hestr);  

class_>("hc",init())  

.def("add",&hc::add);  

}

编译生成dll

将生成的dll文件  修改为hello_ext.pyd

===========================================

python调用

import hello_ext

he=hello_ext.hello("testddd")

print he.get()


he.hestr="ggggddd"

print he.hestr


te=hello_ext.hc("ffff")

print te.get()

te.hestr="ddffg"

print te.hestr

print te.add(33,44)

运行结果