boost.python编译及演示样例
欢迎转载,转载请注明原文地址:http://blog.csdn.net/majianfei1023/article/details/46781581
linux编译boost的链接:http://blog.csdn.net/majianfei1023/article/details/46761029
昨天编译安装好boost,今天准备使用boost.python写个python调用c++代码的样例,结果踩了非常多坑。
首先贴上代码:
1.student.cpp,一个普通的c++类
#include <iostream>
#include <string>
using namespace std; class student
{
public:
void setname(string str)
{
name_ = str;
} string getname()
{
return name_;
} void setage(int age)
{
age_ = age;
} int getage()
{
return age_;
} private:
string name_;
int age_; };
2.student2py.cpp,把c++封装成python模块的代码,使用了boost.python
#include <boost/python.hpp>
#include "student.cpp"
using namespace boost::python;
BOOST_PYTHON_MODULE(example) //python模块
{
class_<student>("student")
.def("setname",&student::setname)
.def("getname",&student::getname)
.def("setage",&student::setage)
.def("getage",&student::getage)
.add_property("name",&student::getname,&student::setname)
.add_property("age",&student::getage,&student::setage)
;
}
3.makefile
example.so:student.o student2py.o
g++ student2py.o -o example.so -shared -fPIC -I/usr/include/python2.6 -I/home/mjf/lib/include -L/usr/lib/python2.6 -L/home/mjf/lib/lib -lboost_python
student.o:
g++ -c student.cpp -o student.o
student2py.o:student.o
g++ -c student2py.cpp -o student2py.o -fPIC -I/usr/include/python2.6 -I/home/mjf/lib/include clean:
rm -rf student.o student2py.o
rm -rf example.so
4.example.py,python调用*.so的演示样例代码
import example
stu = example.student()
stu.setname("mjf")
stu.setage(25)
print stu.name
print stu.age
本来以为一帆风顺。结果make的时候出了各种纠结的问题:
1.
../boost/python/detail/wrap_python.hpp:50:23: error: pyconfig.h: No such file or directory
./boost/python/detail/wrap_python.hpp:75:24: error: patchlevel.h: No such file or directory
./boost/python/detail/wrap_python.hpp:78:2: error: #error Python 2.2 or higher is required for
./boost/python/detail/wrap_python.hpp:142:21: error: Python.h: No such file or directory
./boost/python/instance_holder.hpp:34: error: ‘PyObject’ has not been declared
./boost/python/instance_holder.hpp:41: error: expected ‘;’ before ‘(’ token
./boost/python/instance_holder.hpp:45: error: ‘PyObject’ has not been declared
./boost/python/detail/wrapper_base.hpp:21: error: expected initializer before ‘*’ token
./boost/python/detail/wrapper_base.hpp:23: error: expected initializer before ‘*’ token
各种查资料发现是python的问题。
缺少依赖库 python-devel,要安装一下:
sudo yum install python-devel
2.攻克了上面的问题。又发现了新的问题。
/usr/bin/ld: cannot find -lboost_python
一查,果然发现没有libboost_python.so,安装boost的时候我的确是全然安装的。不知道怎么搞的,没装好预计。
又一次装了一下boost.python
./bootstrap.sh --prefix=/home/mjf/lib
sudo ./b2 --with-python install
大功告成,花了接近两个小时解决一些问题。能够成功用python调用example.so
最后:感谢stackoverflow,非常多问题的答案都能在上面找得到。
boost.python编译及演示样例的更多相关文章
- python解析文本文件演示样例
目的:查找文本中还有Sum/Avg的行中低三个竖线后第一个浮点数 思路:先使用python读取文本中一行,然后切割字符串.查找含有Sum/Avgkeyword的行.取出想要的结果 文本局部: .... ...
- 1000个经常使用的Python库和演示样例代码
以下是programcreek.com通过分析大量开源码,提取出的最经常使用的python库. 1. sys (4627) 2. os (4088) 3. re (3563) 4 ...
- 支付宝即时到帐接口的python实现,演示样例採用django框架
因工作须要研究了支付宝即时到帐接口.并成功应用到站点上,把过程拿出来分享. 即时到帐仅仅是支付宝众多商家服务中的一个,表示客户付款,客户用支付宝付款.支付宝收到款项后,立即通知你,而且此笔款项与交易脱 ...
- Libcurl的编译_HTTP/HTTPSclient源代码演示样例
HTTP/HTTPSclient源代码演示样例 环境: zlib-1.2.8 openssl-1.0.1g curl-7.36 Author: Kagula LastUpdateDate: 2 ...
- [Python] SQLBuilder 演示样例代码
用Python写一个SQLBuilder.Java版能够从 http://www.java2s.com/Code/Java/Database-SQL-JDBC/SQLBuilder.htm 看到. 附 ...
- Python Web框架Tornado的异步处理代码演示样例
1. What is Tornado Tornado是一个轻量级但高性能的Python web框架,与还有一个流行的Python web框架Django相比.tornado不提供操作数据库的ORM接口 ...
- Thrift的安装和简单演示样例
本文仅仅是简单的解说Thrift开源框架的安装和简单使用演示样例.对于具体的解说,后面在进行阐述. Thrift简述 ...
- 展示C代码覆盖率的gcovr工具简单介绍及相关命令使用演示样例
(本人正在參加2015博客之星评选,诚邀你来投票,谢谢:username=zhouzxi">http://vote.blog.csdn.net/blogstar2015/candida ...
- JDBC连接MySQL数据库及演示样例
JDBC是Sun公司制定的一个能够用Java语言连接数据库的技术. 一.JDBC基础知识 JDBC(Java Data Base Connectivity,java数据库连接)是一种用 ...
随机推荐
- CentOS7.4 安装 oracle12c
安装依赖 yum install -y binutils.x86_64 compat-libcap1.x86_64 gcc.x86_64 gcc-c++.x86_64 glibc.i686 glibc ...
- JDK动态代理小例子
一个小汽车,有一个跑run()的方法,我们想使用jdk动态代理使小汽车执行run之前 加点油,run之后洗车. 有四个类,接口Car(小汽车)Kayan(具体实现类(卡宴)) CarProxy(汽车的 ...
- HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...
- Python股票信息抓取~
本来想把股票的涨跌抓取出来,用汇通网的股票为例,就找了国际外汇为例. 页面里有xhr请求,并且每个xhr的url请求的 http://api.q.fx678.com/history.php?symbo ...
- Pylint在项目中的使用
需求背景: Pylint 是一个 Python 代码分析工具,它分析 Python 代码中的错误,查找不符合代码风格标准和有潜在问题的代码. Pylint 是一个 Python 工具,除了平常代码分析 ...
- 分布式跟踪系统zipkin+mysql
1.初始化数据库: 1) CREATE TABLE IF NOT EXISTS zipkin_spans ( trace_id_high BIGINT NOT NULL DEFAULT 0 COMME ...
- GO基本数据结构练习:数组,切片,映射
按<GO IN ACTION>的书上进行. 应该是第二次了哦~~ package main import ( "fmt" ) func main() { array : ...
- Weblogic常用监控指标以及监控工具小结
https://blog.csdn.net/hualusiyu/article/details/39583549
- Flume(一)Flume的基础介绍与安装
一.背景 Hadoop业务的整体开发流程: 从Hadoop的业务开发流程图中可以看出,在大数据的业务处理过程中,对于数据的采集是十分重要的一步,也是不可避免的一步. 许多公司的平台每天会产生大量的日志 ...
- Java学习笔记之:Java Servlet环境配置
一.介绍 Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中间层. 使用 ...