C++嵌入python ,通过boost.python
来源:互联网 发布:广联达软件下载4.0 编辑:程序博客网 时间:2024/05/17 01:29
转载自:wyljz vs2010用 boost.python 编译c++类库 供python调用
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
- #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的子类
- #pragma once
- #include "hello.cpp"
- class hc:public Hello
- {
- public:
- hc(string str);
- ~hc(void);
- int add(int a,int b);
- };
hc.cpp
- #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.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内容:
- 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)
- C++嵌入python ,通过boost.python
- boost.python的嵌入
- boost C++中嵌入python
- boost.python学习之----python 嵌入c++
- boost.python库应用之嵌入python
- boost.python入门教程 ----python 嵌入c++
- boost.python库应用之嵌入python
- boost.python入门教程 ----python 嵌入c++
- Python嵌入C++(1)
- python 嵌入c
- windows下,多线程c代码嵌入python例子,测试通过
- 利用boost在C++中嵌入python
- boost Python 编译通过记录
- 使用 Boost.Python 嵌入 Python 模块到 C++
- 使用 Boost.Python 嵌入 Python 模块到 C++
- c++中嵌入python入门4 之 Boost.Python
- Python嵌入C++(2)
- Python嵌入C中了
- Jquery的右键菜单插件ContextMenu
- java数据类型转换
- sqlserver2005创建连接数据库dblink
- use window default tool to record bug steps-"Problem Step Record"
- 八年级作文-亲情树
- C++嵌入python ,通过boost.python
- java反射reflect
- I can't open Codeblocks! Gives me TinyXML error message
- Oracle Exadata 简介
- perl变量作用域--local,my,our
- 《将心注入》书摘
- PHP教程实例:用PHP打造动态聊天室系统
- AppStore新应用上传指南
- opengl学习笔记(5)