转自: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

  1. #include <iostream>
  2. using namespace std;
  3. class Hello
  4. {
  5. public:
  6. string hestr;
  7. private:
  8. string title;
  9. public:
  10. Hello(string str){this->title=str;}
  11. string get(){return this->title;}
  12. };

示例:hc.h 继承hello的子类

  1. #pragma once
  2. #include "hello.cpp"
  3. class hc:public Hello
  4. {
  5. public:
  6. hc(string str);
  7. ~hc(void);
  8. int add(int a,int b);
  9. };

hc.cpp

  1. #include "hc.h"
  2. hc::hc(string str):Hello(str)
  3. {
  4. }
  5. hc::~hc(void)
  6. {
  7. }
  8. int hc::add(int a,int b)
  9. {
  10. return a+b;
  11. }

导出的类pyhello.cpp

  1. #include "hc.h"
  2. #include <boost/python.hpp>
  3. BOOST_PYTHON_MODULE(hello_ext)
  4. {
  5. using namespace boost::python;
  6. class_<Hello>("Hello",init<std::string>())
  7. .def("get",&Hello::get)
  8. .def_readwrite("hestr",&Hello::hestr);
  9. class_<hc,bases<Hello>>("hc",init<std::string>())
  10. .def("add",&hc::add);
  11. }

python项目中调用 dll的目录包含文件:

1,boost_python-vc100-mt-1_46_1.dll

2,dll.py  调用dll的py文件

3,hello_ext.pyd  由上述c++生成的dll文件改名而成

dll.py内容:

  1. import hello_ext
  2. he=hello_ext.Hello("testdddd")
  3. print he.get()
  4. he.hestr="ggggddd"
  5. print he.hestr
  6. te=hello_ext.hc("fffff")
  7. print te.get()
  8. te.hestr="ddffg"
  9. print te.hestr
  10. print te.add(33,44)

[转]vs2010用 boost.python 编译c++类库 供python调用的更多相关文章

  1. Go 程序编译成 DLL 供 C# 调用。

    Go 程序编译成 DLL 供 C# 调用. C# 结合 Golang 开发   1. 实现方式与语法形式 基本方式:将 Go 程序编译成 DLL 供 C# 调用. 1.1 Go代码 注意:代码中 ex ...

  2. Matlab函数编译成dll供c调用

    一 编译dll 在Command Window窗口中输入mbuild -setup,然后会出现语句,是否安装编译器,选择n,因为机子上已经安装了C/C++/C#的编译器,选择VS2010.

  3. OWIN 自宿主模式WebApi项目,WebApi层作为单独类库供OWIN调用

    OWIN是Open Web Server Interface for .NET的首字母缩写,他的定义如下: OWIN在.NET Web Servers与Web Application之间定义了一套标准 ...

  4. vs2010配备boost编程环境

    vs2010配备boost编程环境 vs2010配置boost编程环境 第一步:下载boost,我下载的方法是从http://www.boost.org/上找最新的下载.名字叫boost_1_53_0 ...

  5. python 网络请求类库 requests 使用

    python 网络请求类库 requests 使用 requests是 为python封装的强大 REST 操作类库 githubhttps://github.com/kennethreitz/req ...

  6. Boost库编译安装

    一.Boost库介绍         Boost库是一个经过千锤百炼.可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一.Boost库由C++标准委员会库工作组成员发起,其 ...

  7. python安装第三方类库的方法

    1.先到官网 http://pypi.python.org/pypi/setuptools 下载setuptools.exe文件并安装 点击 ez_setup.py进入, 并将内容复制下来, 保存为本 ...

  8. 2.准备Python编译环境

    2.准备Python编译环境 2.1下载Python2.7.6.tgz.ipython1.2.1.tgz.sqlite-autoconf-3071401.tar.gz 2.2安装Python2.7.6 ...

  9. linux gcc 编译动态类库(.so)和静态类库(.a)

    linux gcc 编译动态类库(.so)和静态类库(.a) 我的编译环境 ubuntu desktop 16.04 一:测试代码 测试有3个文件:AB.h,AB.c,test.c //AB.h vo ...

随机推荐

  1. 特效 左右滑动轮播图jQuery思路

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 每天跟着书敲Mysql

    要深入学下Mysql操作啦 CRUD,create,retrieve,update,delete

  3. 版本控制工具Git的复杂用法的情境分析

    Git的版本库中的文件的三种状态 对于任何一个文件,在 Git 内都只有三种状态: 已提交(committed),已修改(modified)和已暂存(staged). 已提交表示该文件已经被安全地保存 ...

  4. windows service 2008 R2 安装net4.6环境失败,windows service 2008 R2 升级sp1问题

    一.错误 1.因为我的程序是以vs2017开发的,在windows service 2008 R2  IIS部署项目文件报出错误,因此要安装net4.6的环境. 2.windows service 2 ...

  5. 选择器:first-child与:last-child失效的解决方法

    作为还在努力练习的代码小白来说,有时类名或者ID名太多很容易就会搞混,为此,在练习中会想着借用多样的选择器来设置而不是每一个标签都设一个类名(Id名),在此次练习中使用选择器:first-child与 ...

  6. 【转】CSS3属性 @font-face 整理

    原文: http://www.w3cplus.com/content/css3-font-face 出自: w3cplus.com 一.语法规则 @font-face { font-family: & ...

  7. 获取当前页面url并截取所需字段

    let url = window.location.href; // 动态获取当前url // 例: "http://i.cnblogs.com/henanyundian/web/app/# ...

  8. mstOne

    云鸟(2017年6月6日) 1.html标签语义化的好处 a:利于seo优化 b:在样式丢失的时候,还是可以比较好的呈现结构 c:更好的支持各种终端,例如无障碍阅读和有声小说等. d:利于团队开发和维 ...

  9. 用python处理html代码的转义与还原

    用python处理html代码的转义与还原   转义 escape: import cgi s = cgi.escape("""& < >" ...

  10. learning makefile automatic dependency generation