python学习笔记:安装boost python库以及使用boost.python库封装
学习是一个累积的过程。在这个过程中,我们不仅要学习新的知识,还需要将以前学到的知识进行回顾总结。
前面讲述了Python使用ctypes直接调用动态库和使用Python的C语言API封装C函数, C++写python扩展模块有很多种方式,我选择的是boost.python来编写的,感觉这个要比其他的方式要简单很多,本文概述方便封装C++类给Python使用的boost_python库。

:
sudo aptitude install libboost-python-dev
示例
下面代码简单实现了一个普通函数maxab()和一个Student类:
#include <iostream>
#include <string> int maxab(int a, int b) { return a>b?a:b; } class Student {
private:
int age;
std::string name; public:
Student() {}
Student(std::string const& _name, int _age) { name=_name; age=_age; } static void myrole() { std::cout << "I'm a student!" << std::endl; } void whoami() { std::cout << "I am " << name << std::endl; } bool operator==(Student const& s) const { return age == s.age; }
bool operator!=(Student const& s) const { return age != s.age; } };
使用boost.python库封装也很简单,如下代码所示:
#include <Python.h>
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <vector> #include "student.h" using namespace boost::python; BOOST_PYTHON_MODULE(student) {
// This will enable user-defined docstrings and python signatures,
// while disabling the C++ signatures
scope().attr("__version__") = "1.0.0";
scope().attr("__doc__") = "a demo module to use boost_python.";
docstring_options local_docstring_options(true, false, false);
def(
"maxab", &maxab, "return max of two numbers.\n"
); class_<Student>("Student", "a class of student")
.def(init<>())
.def(init<std::string, int>())
// methods for Chinese word segmentation
.def(
"whoami", &Student::whoami, "method's doc string..."
)
.def(
"myrole", &Student::myrole, "method's doc string..."
)
.staticmethod("myrole");
// 封装STL
class_<std::vector<Student> >("StudentVec")
.def(vector_indexing_suite<std::vector<Student> >())
;
}
上述代码还是include了Python.h文件,如果不include的话,会报错误:
wrap_python.hpp:50:23: fatal error: pyconfig.h: No such file or directory
编译
编译以上代码有两种方式,一种是在命令行下面直接使用g++编译:
g++ -I/usr/include/python2.7 -fPIC wrap_student.cpp -lboost_python -shared -o student.so
首先指定Python.h的路径,如果是Python 3的话就要修改为相应的路径,编译wrap_student.cpp要指定-fPIC参数,链接(-lboost_python)生成动态库(-shared)。 生成的student.so动态库就可以被python直接import使用了
In [1]: import student In [2]: student.maxab(2, 5)
Out[2]: 5 In [3]: s = student.Student('Tom', 12) In [4]: s.whoami()
I am Tom In [5]: s.myrole()
I'm a student!
另外一直方法是用python的setuptools编写setup.py脚本:
#!/usr/bin/env python from setuptools import setup, Extension setup(name="student",
ext_modules=[
Extension("student", ["wrap_student.cpp"],
libraries = ["boost_python"])
])
然后执行命令编译:
python setup.py build
or
sudo python setup.py install
学习笔记整理于猿人学教程
python学习笔记:安装boost python库以及使用boost.python库封装的更多相关文章
- Python学习笔记——安装
最近打算使用下GAE,便准备学习一下python.我对python是一窍不通,因此这里将我的学习历程记录下来,方便后续复习. 安装python: 可以从如下地址:http://www.python.o ...
- Python学习笔记-CGI编程(如何在IIS上挂Python开发的Webservice)
一.如何用Python开发一个简单的Webservice 利用python的cgi编程,可以传入参数将结果输出. 定义需要编码以及需要引用的模块 #conding=utf-8 #修正中文乱码 impo ...
- Python学习笔记【第十五篇】:Python网络编程三ftp案例练习--断点续传
开发一个支持多用户在线的FTP程序-------------------主要是学习思路 实现功能点 1:用户登陆验证(用户名.密码) 2:实现多用户登陆 3:实现简单的cmd命令操作 4:文件的上传( ...
- python学习笔记(二)---编辑工具sublimeText3运行python
转载地址:https://blog.csdn.net/Maek_Tyx/article/details/76933897 1. 打开Sublime text 3 安装package controlSu ...
- Python学习笔记【第十四篇】:Python网络编程二黏包问题、socketserver、验证合法性
TCP/IP网络通讯粘包问题 案例:模拟执行shell命令,服务器返回相应的类容.发送指令的客户端容错率暂无考虑,按照正确的指令发送即可. 服务端代码 # -*- coding: utf- -*- # ...
- Python学习笔记【第十二篇】:Python异常处理
什么是异常 异常就是程序运行时发生错误的信号,在python中,错误触发的异常如下 错误类型分为两种:语法错误和业务逻辑错. 异常的类型 AttributeError 试图访问一个对象没有的树形,比如 ...
- 【Python学习笔记】Coursera课程《Using Databases with Python》 密歇根大学 Charles Severance——Week4 Many-to-Many Relationships in SQL课堂笔记
Coursera课程<Using Databases with Python> 密歇根大学 Week4 Many-to-Many Relationships in SQL 15.8 Man ...
- 【目录】Python学习笔记
目录:Python学习笔记 目标:坚持每天学习,每周一篇博文 1. Python学习笔记 - day1 - 概述及安装 2.Python学习笔记 - day2 - PyCharm的基本使用 3.Pyt ...
- Python学习笔记(15)- os\os.path 操作文件
程序1 编写一个程序,统计当前目录下每个文件类型的文件数,程序实现如图: import os def countfile(path): dict1 = {} # 定义一个字典 all_files = ...
- Python学习笔记之基础篇(-)python介绍与安装
Python学习笔记之基础篇(-)初识python Python的理念:崇尚优美.清晰.简单,是一个优秀并广泛使用的语言. python的历史: 1989年,为了打发圣诞节假期,作者Guido开始写P ...
随机推荐
- ubuntu报错解决和注意事项
1.在容器中安装expect报错 [root@kube-node3 target]# docker exec -it 36563e55c42b sh$ sudo apt-get install exp ...
- Maven打包将依赖的jar一同打进去
在pom.xml文件中添加: <build> <plugins> <plugin> <artifactId>maven-assembly-plugin& ...
- python进阶----logging模块
在工作中经常要打印一些日志,下面介绍一下python中的logging模块 首先,先了解一下日志的级别,主要分为以下5种: debug 最低级别,一般开发用来打印一些调试信息 info ...
- pip install locustio报错
安装locust时, 执行pip install locustio时报错 ERROR: Cannot uninstall 'requests'. It is a distutils installed ...
- 【计算机】DMA原理2
DMA (直接存储器访问) DMA(Direct Memory Access,直接内存存取) 是所有现代电脑的重要特色,它允许不同速度的硬件装置来沟通,而不需要依赖于 CPU 的大量中断负载.否则,C ...
- 使用socket.io实现双向实时消息传递,(客服聊天功能)
思否 https://segmentfault.com/a/1190000010974426 博客园 https://www.cnblogs.com/limitcode/p/7845168.html ...
- noi openjudge7627:鸡蛋的硬度
http://noi.openjudge.cn/ch0206/7627/ 描述 最近XX公司举办了一个奇怪的比赛:鸡蛋硬度之王争霸赛.参赛者是来自世界各地的母鸡,比赛的内容是看谁下的蛋最硬,更奇怪的是 ...
- 【IDEA插件】—— 代码量统计工具Statistic
1.下载 1.打开idea设置界面,选择 plugins标签 2.搜索“Statistic”插件,点击 install 3.重启IDEA 2.使用 1.菜单栏中找到view 2.在下层目录中找到Sta ...
- 新拉的项目在idea中启动时报如下错误:org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:
今天真的是很苦恼,之前启动项目没有任何问题,今天突然启动时给我报了如下一个错误. 详细报错信息: org.apache.catalina.core.ContainerBase.addChildInte ...
- Web文件上传靶场 - 通关笔记
Web应用程序通常会提供一些上传功能,比如上传头像,图片资源等,只要与资源传输有关的地方就可能存在上传漏洞,上传漏洞归根结底是程序员在对用户文件上传时控制不足或者是处理的缺陷导致的,文件上传漏洞在渗透 ...