[转]vs2010用 boost.python 编译c++类库 供python调用
2017-12-06 23:49
721 查看
转自:http://blog.csdn.net/wyljz/article/details/6307952
VS2010建立一个空的DLL
项目属性中配置如下
链接器里的附加库目录加入,python/libs(python的安装目录中),boost/vs2010/lib(生成的boost的目录中)
c/c++的附加库目录加入,boost(boost的下载目录),python/include(python的安装目录)
代码文件加入引用
#include <boost/python.hpp>
生成的DLL文件,需改成和导出模块一致的名称,后缀为PYD
将此PYD文件与生成的BOOST/LIB中的boost_python-vc100-mt-1_46_1.dll 一同拷入工作目录,在此目录中新建py文件,就可以 直接 import 模块名,进行使用
示例:hello.cpp
[cpp] view plain copy
#include <iostream>
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 继承hello的子类
[cpp] view plain copy
#pragma once
#include "hello.cpp"
class hc:public Hello
{
public:
hc(string str);
~hc(void);
int add(int a,int b);
};
hc.cpp
[cpp] view plain copy
#include "hc.h"
hc::hc(string str):Hello(str)
{
}
hc::~hc(void)
{
}
int hc::add(int a,int b)
{
return a+b;
}
导出的类pyhello.cpp
[cpp] view plain copy
#include "hc.h"
#include <boost/python.hpp>
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
class_<Hello>("Hello",init<std::string>())
.def("get",&Hello::get)
.def_readwrite("hestr",&Hello::hestr);
class_<hc,bases<Hello>>("hc",init<std::string>())
.def("add",&hc::add);
}
python项目中调用 dll的目录包含文件:
1,boost_python-vc100-mt-1_46_1.dll
2,dll.py 调用dll的py文件
3,hello_ext.pyd 由上述c++生成的dll文件改名而成
dll.py内容:
[python] view plain copy
import hello_ext
he=hello_ext.Hello("testdddd")
print he.get()
he.hestr="ggggddd"
print he.hestr
te=hello_ext.hc("fffff")
print te.get()
te.hestr="ddffg"
print te.hestr
print te.add(33,44)
VS2010建立一个空的DLL
项目属性中配置如下
链接器里的附加库目录加入,python/libs(python的安装目录中),boost/vs2010/lib(生成的boost的目录中)
c/c++的附加库目录加入,boost(boost的下载目录),python/include(python的安装目录)
代码文件加入引用
#include <boost/python.hpp>
生成的DLL文件,需改成和导出模块一致的名称,后缀为PYD
将此PYD文件与生成的BOOST/LIB中的boost_python-vc100-mt-1_46_1.dll 一同拷入工作目录,在此目录中新建py文件,就可以 直接 import 模块名,进行使用
示例:hello.cpp
[cpp] view plain copy
#include <iostream>
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 继承hello的子类
[cpp] view plain copy
#pragma once
#include "hello.cpp"
class hc:public Hello
{
public:
hc(string str);
~hc(void);
int add(int a,int b);
};
hc.cpp
[cpp] view plain copy
#include "hc.h"
hc::hc(string str):Hello(str)
{
}
hc::~hc(void)
{
}
int hc::add(int a,int b)
{
return a+b;
}
导出的类pyhello.cpp
[cpp] view plain copy
#include "hc.h"
#include <boost/python.hpp>
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
class_<Hello>("Hello",init<std::string>())
.def("get",&Hello::get)
.def_readwrite("hestr",&Hello::hestr);
class_<hc,bases<Hello>>("hc",init<std::string>())
.def("add",&hc::add);
}
python项目中调用 dll的目录包含文件:
1,boost_python-vc100-mt-1_46_1.dll
2,dll.py 调用dll的py文件
3,hello_ext.pyd 由上述c++生成的dll文件改名而成
dll.py内容:
[python] view plain copy
import hello_ext
he=hello_ext.Hello("testdddd")
print he.get()
he.hestr="ggggddd"
print he.hestr
te=hello_ext.hc("fffff")
print te.get()
te.hestr="ddffg"
print te.hestr
print te.add(33,44)
相关文章推荐
- vs2010用 boost.python 编译c++类库 供python调用
- 用python程序调用C/C++编译的可执行文件
- C++调用python配置及编译出现的问题
- python调用c++代码,.so库的编译
- boost.python,在python中调用c++接口注意的问题(python2.5.4)
- python调用C和C++代码(BOOST)
- Boost.Python C++与Python的互相调用之Python调用c/c++函数
- Boost.Python实现C++与Python互调用
- Boost.Python实现C++与Python互调用
- 如何在C++代码中调用python代码(MinGW+Boost.Python)
- 使用boost.python在python调用C++接口(windows)
- win7下用VS2010和Intel C++ 11编译Boost
- Linux下C++调用python,gcc和eclipse的编译方法
- 用boost封装C++的库供Python调用
- python嵌入C++------ boost.python如何在C++中调用含有不定长参数tuple变量和关键字参数dict变量的函数
- Python调用采用Boost Python封装的c++
- Windows下QT中用C++调用Python之二 - 编译BUG处理
- Python调用C/C++的接口(Boost)
- C++调用python的debug版本编译问题
- python 使用Boost.python 调用c++