转自: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. 【转】 ISP-黑电平校正(BLC)

    转自:https://blog.csdn.net/xiaoyouck/article/details/72824534 介绍黑电平(Black Level Correction)也就是黑色的最低点,以 ...

  2. localStorage(本地存储器)、sessionStorage(会话存储)

      设置:localStorage.setItem("token", JSON.parse(res).data.token);   获取:that.token = localSto ...

  3. jQuery 省份选择

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. YAML基础语法

    正如YAML所表示的YAML Ain’t Markup Language,YAML 是一种简洁的非标记语言.YAML以数据为中心,使用空白,缩进,分行组织数据,从而使得表示更加简洁易读. 一边学习规则 ...

  5. LSTM如何解决梯度消失或爆炸的?

    from:https://zhuanlan.zhihu.com/p/44163528 哪些问题? 梯度消失会导致我们的神经网络中前面层的网络权重无法得到更新,也就停止了学习. 梯度爆炸会使得学习不稳定 ...

  6. ORACLE中使用row_number over()排序

    from:http://blog.csdn.net/iw1210/article/details/11937085 意图:实现select top 1 * from tablename Oracle  ...

  7. Vue(六) 表单与 v-model

    学习使用 v-model 指令完成表单数据双向绑定 基本用法 表单控件在实际业务较为常见,比如单选,多选.下拉选择.输入框等,用他们可以完成数据的录入.校验.提交等.Vue.js 提供了 v-mode ...

  8. javaWeb 字体替换过滤器

    字体替换过滤器继承HttpServletRequestWrapper,重定义了: getParameter(),getParameterValues(),getParameterMap(),增强字节替 ...

  9. linux 安装【jdk、tomcat】查看对外开放端口(防火墙拦截处理)

    1.安装 jdkhttps://www.cnblogs.com/xu-dong/p/6422938.html 查看Linux下查看JDK安装路径: https://www.cnblogs.com/im ...

  10. ubuntu1604使用之旅——软件源更新(vim安装)

    sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup sudo gedit /etc/apt/sources.list # deb cd ...