配置环境

$ 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. Hadoop- 集群启动详解

    NameNode启动过程详解 第一次启动:HDFS格式化后,生成fsimage文件 hdf

  2. Javascript两个数的比较

    Strict equality using === 比较之前不转换类型, 如果不同类型,不相等, 如果相同类型:如果两个都不是numbers,只有自己和自己比较才相等,其他都不相等: 如果两个都是nu ...

  3. C++(五)— 控制保留小数位数

    1.C++中输出指定保留的小数位数. 这里还要注意,每次输出只要设置一次就行了,因为这两个的作用范围是后续对象,而不是仅对后一个对象起作用. #include<iostream> #inc ...

  4. ashx页面缓存

    当用户访问页面时,整个页面将会被服务器保存在内存中,这样就对页面进行了缓存.当用户再次访问该页,页面不会再次执行数据操作,页面首先会检查服务器中是否存在缓存,如果缓存存在,则直接从缓存中获取页面信息, ...

  5. codeforces 653A A. Bear and Three Balls(水题)

    题目链接: A. Bear and Three Balls time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  6. 【遍历二叉树】06二叉树曲折(Z字形)层次遍历II【Binary Tree Zigzag Level Order Traversal】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的Z字形层次 ...

  7. innerdb disable error

    innodb=OFF ignore-builtin-innodb skip-innodbdefault-storage-engine=myisam default-tmp-storage-engine ...

  8. APIO2017 游记

    参加了APIO的同学肯定知道我为什么只写标题不写内容. QAQ

  9. 记一次编译tensorflow-gpu爬过的坑

    废话不多说,先说最终成功的版本:系统=>centos7 ,cuda=>10.0 ,cudnn=>7.5 ,nccl=>源码编译, tensorflow=>最新版本源码编译 ...

  10. bzoj 2096 [POI2004]ZAW——二进制枚举

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2069 可以把直接相连的点分成  从1点出的一部分  和  走向1点的一部分.多起点最短路就和 ...