配置环境

$ cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
UBUNTU_CODENAME=xenial # apt install libboost-python-dev cmake

导出C++函数

创建工程目录

$ mkdir Lesson1
$ cd Lesson1

编写C++函数实现

$ vim greet.cpp
char const* greet()
{
return "hello world";
}

编写Boost.Python文件

$ vim greet_wrapper.cpp
#include <boost/python.hpp>
#include "greet.cpp" BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}

为库编写CMakeLists.txt

$ vim CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(greet) ### 此处的动态库名必须和BOOST_PYTHON_MODULE()中定义的保持一致,即最后生成的库必须名为hello_ext.so
set(greetSRC greet_wrapper.cpp)
add_library(hello_ext SHARED ${greetSRC})
set_target_properties(hello_ext PROPERTIES PREFIX "") #dependencies
INCLUDE(FindPkgConfig)
pkg_check_modules(PYTHON REQUIRED python)
include_directories(/usr/include ${PYTHON_INCLUDE_DIRS})
target_link_libraries(hello_ext boost_python)

编译库

$ mkdir build
$ cd build
$ cmake ..
$ make

运行python测试库文件

### 在build目录下执行,即hello_ext.so存在的目录(可以将so移至其他目录,这样就可以在其他目录下打开python终端)
$ python
>>> import hello_ext
>>> help(hello_ext)
>>> hello_ext.greet()
'hello world'

导出C++类

编写C++类实现

$ vim world.h
#include <string.h> struct World
{
void set(std::string msg) { this->msg = msg; }
std::string greet() { return msg; }
std::string msg;
};

编写Boost.Python文件

$ vim world_wrapper.cpp
#include <boost/python.hpp>
#include "world.h"
using namespace boost::python; BOOST_PYTHON_MODULE(hello)
{
class_<World>("World")
.def("greet", &World::greet)
.def("set", &World::set)
;
}

运行python测试库文件

$ python
>>> import hello
>>> planet = hello.World()
>>> planet.set('howdy')
>>> planet.greet()
'howdy'

导出C++类(带构造函数)

编写C++类实现

$ cat world.h
#include <string.h> struct World
{
World(std::string msg): msg(msg) {} // added constructor
void set(std::string msg) { this->msg = msg; }
std::string greet() { return msg; }
std::string msg;
};

编写Boost.Python文件

$ vim world_wrapper.cpp
#include <boost/python.hpp>
#include "world.h"
using namespace boost::python; BOOST_PYTHON_MODULE(hello)
{
class_<World>("World", init<std::string>())
.def("greet", &World::greet)
.def("set", &World::set)
;
}

运行python测试库文件

>>> import hello
>>> planet = hello.World("test")
>>> planet.greet()
'test'

导出C++类(带数据成员)

编写C++类实现

$ vim var.h
#include <string.h> struct Var
{
Var(std::string name) : name(name), value() {}
std::string const name;
float value;
};

编写Boost.Python文件

$ vim var_wrapper.cpp
#include <boost/python.hpp>
#include "var.h"
using namespace boost::python; BOOST_PYTHON_MODULE(hello)
{
class_<Var>("Var", init<std::string>())
.def_readonly("name", &Var::name)
.def_readwrite("value", &Var::value);
}

运行python测试库文件

>>> import hello
>>> x = hello.Var('pi')
>>> x.value = 3.14
>>> print x.name, 'is around', x.value
pi is around 3.1400001049

Boost Python官方样例(一)的更多相关文章

  1. Boost Python官方样例(三)

    导出C++类(纯虚函数和虚函数) 大致做法就是为class写一个warp,通过get_override方法检测虚函数是否被重载了,如果被重载了调用重载函数,否则调用自身实现,最后导出的时候直接导出wa ...

  2. Boost Python官方样例(二)

    返回值 使用return_by_value有点像C++ 11的auto关键字,可以让模板自适应返回值类型(返回值类型必须是要拷贝到新的python对象的任意引用或值类型),可以使用return_by_ ...

  3. Python word_cloud 样例 标签云系列(三)

    转载地址:https://zhuanlan.zhihu.com/p/20436642word_cloud/examples at master · amueller/word_cloud · GitH ...

  4. gtk+3.0的环境配置及基于gtk+3.0的python简单样例

    /*********************************************************************  * Author  : Samson  * Date   ...

  5. ShardingSphere 知识库更新 | 官方样例集助你快速上手

    Apache ShardingSphere 作为 Apache 顶级项目,是数据库领域最受欢迎的开源项目之一.经过 5 年多的发展,ShardingSphere 已获得超 14K Stars 的关注, ...

  6. Python代码样例列表

    扫描左上角二维码,关注公众账号 数字货币量化投资,回复“1279”,获取以下600个Python经典例子源码 ├─algorithm│       Python用户推荐系统曼哈顿算法实现.py│    ...

  7. 基于类和基于函数的python多线程样例

    不断的练,加深记忆吧. #!/usr/bin/env python # -*- coding: utf-8 -*- import threading import time exitFlag = 0 ...

  8. 维特比算法(Viterbi)及python实现样例

    维特比算法(Viterbi) 维特比算法 维特比算法shiyizhong 动态规划算法用于最可能产生观测时间序列的-维特比路径-隐含状态序列,特别是在马尔可夫信息源上下文和隐马尔科夫模型中.术语“维特 ...

  9. 【React Native开发】React Native配置执行官方样例-刚開始学习的人的福音(8)

    ),React Native技术交流4群(458982758),请不要反复加群! 欢迎各位大牛,React Native技术爱好者加入交流!同一时候博客左側欢迎微信扫描关注订阅号,移动技术干货,精彩文 ...

随机推荐

  1. UOJ136 开学前的作文

    描述 红包是一个萌萌的男孩子. 红包由于 NOI 惨挂,直到前不久依然无心写作业.如今快开学了,他决定好好完成作业. 对于可以交电子稿的作文,红包有特殊的完成技巧,大致流程是依次选中一段内容→按下 C ...

  2. JAVA发送手机短信

    <p><span>JAVA发送手机短信,流传有几种方法:(1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册 ...

  3. HTML5 canvas save()和restore()方法讲解

    我们尝试用这个连续矩形的例子来描述 canvas 的状态堆是如何工作的.第一步是用默认设置画一个大四方形,然后保存一下状态.改变填充颜色画第二个小一点的白色四方形,然后再保存一下状态.再次改变填充颜色 ...

  4. spring boot: Bean的初始化和销毁 (一般注入说明(三) AnnotationConfigApplicationContext容器 JSR250注解)

    import org.springframework.context.annotation.AnnotationConfigApplicationContext; 使用AnnotationConfig ...

  5. 【DP专辑】ACM动态规划总结(转)

    http://blog.csdn.net/cc_again/article/details/25866971 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少,多元性强, ...

  6. IronPython 源码剖析系列(1):IronPython 编译器

    自 IronPython 正式发布以来,由于对 Python 语言的喜爱所驱使,同时我想藉此去了解一下编程语言的编译器,分析器等程序是什么原理,如何运作的,所以我开始了对 IronPython 源代码 ...

  7. 洛谷【P2115】[USACO14MAR]破坏Sabotage

    我对二分的理解:https://www.cnblogs.com/AKMer/p/9737477.html 题目传送门:https://www.luogu.org/problemnew/show/P21 ...

  8. CentOS 6.X 安装 EPEL 源

    CentOS 6.X 自带的软件源可选的并不多,有时候要找到一个偏门一些的软件,用命令一搜怎么都没有源,考虑到使用软件源配合 yum 命令安装可以自动安装依赖,所以加一个新的软件源迫在眉睫. 考虑到同 ...

  9. HDOJ1272(并查集,判断是否为树)

    0 0 Yes 1 1 0 0 Yes 1 2 2 1 0 0 No //自回路不算一条边的! 居然有 0 0 这样的测试数据 #include<iostream> #include< ...

  10. Spring 3.1新特性之四:p命名空间设置注入(待补充)

    https://www.ibm.com/developerworks/cn/java/j-lo-jparelated/ http://www.ibm.com/developerworks/cn/jav ...