配置环境

$ 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. Eclipse debug neutron-server

    1 首先停掉neutron-server kill neutron-server in screen by ctr-c q-svc 2 cp /usr/local/bin/neutron-server ...

  2. pugixml 1.9 manual解读(部分)

    Plain character data nodes (node_pcdata) represent plain text in XML. PCDATA nodes have a value, but ...

  3. JavaWeb中文件的上传和下载

    JavaWeb中文件的上传和下载 转自: JavaWeb学习总结(五十)——文件上传和下载 - 孤傲苍狼 - 博客园https://www.cnblogs.com/xdp-gacl/p/4200090 ...

  4. 六 Django框架,models.py模块,数据库操作——链表结构,一对多、一对一、多对多

    链表操作 链表,就是一张表的外键字段,连接另外一张表的主键字段 一对多 models.ForeignKey()外键字段一对多,值是要外键的表类 from __future__ import unico ...

  5. Git_学习_05_ 解决冲突

    二.参考资料 1.使用git pull文件时和本地文件冲突怎么办? 2.git 冲突解决

  6. Git_学习_00_资源帖

    1.廖雪峰: (1)Git教程 2.阮一峰: (1)Git分支管理策略 (2)Git远程操作详解 (3)Git 使用规范流程 (4)Github 的清点对象算法 (5)常用 Git 命令清单 (6)G ...

  7. jQuery插件--图片文字向上向左循环滚动

    需要引用jquery 调用非常简单: 一. 向上滚动 $(".scroll_two").jScroll({vertical: true}); <div class=" ...

  8. CURL抓取网页内容

    <?php $curl = curl_init();//初始化一个cURL对象 $url = "http://cart.jd.com/cart/cart.html?backurl=ht ...

  9. 用Rem来无脑还原Web移动端自适应的页面

    (function (win,doc){ if (!win.addEventListener) return; var html=document.documentElement; function ...

  10. 【LeetCode】014. Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 题解: 简单的暴力遍历解决 ...