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 ...
随机推荐
- github pages + hexo 搭建 blog 遇到的问题
一. ERROR Deployer not found: git $ hexo d ERROR Deployer not found: git npm install --save hexo-depl ...
- Paid consultation (currently free 20190901)
Master of Electrical Engineering, Chongqing University Range:01 College entrance examination, major, ...
- Vue Cli3.0 使用jquery
参考链接:https://blog.csdn.net/ai520587/article/details/84098601
- CentOS7编译安装libc++和libc++abi
本文介绍了如何在CentOS 7中构建C++11构建环境 Clang的定制C++库是libc++(libcxx).然后,libcxx还需要一个ABI库,libc++abi(libcxxabi).不幸的 ...
- There are no packages available
{ "bootstrapped": true, "channels": [ "https://raw.githubusercontent.com/Ja ...
- 《C和指针》读书笔记
1. 三字母词 三字母词即用三个字符合起来表示另一个字符,它可以使C环境在某些缺少一些必需字符的字符集上实现. ??( [ ??< { ??= # ??) ] ??> } ??/ \ ?? ...
- 使用pycharm开发web——django2.1.5(五)表单和通用视图
看了刘江老师教程这么多天,卧槽,我才发现他也曾跻身于行伍之间,interesting 刘老师这波讲解很到位,告诉你如何编写单例视图的时候忽然告诉你,其实不用这么麻烦,我们有通用视图,那些总是要做相似的 ...
- LeetCode 第 15 场双周赛
1287.有序数组中出现次数超过25%的元素 1288.删除被覆盖区间 1286.字母组合迭代器 1289.下降路径最小和 II 下降和不能只保留原数组中最小的两个,hacked. 1287.有序数组 ...
- Dijstra_优先队列_前向星
Dijstra算法求最短路径 具体实现方式 设置源点,将源点从原集u{}中取出并放入新建集s{} 找出至源点最近的点q从原集取出放入新集s{} 由q点出发,更新所有由q点能到达的仍处于原集的点到源点的 ...
- php常用扩展有哪些
bcmath(精确数值处理) bz2 calendar Core ctype curl date dom ereg exif fileinfo filter ftp gettext hash icon ...