欢迎转载,转载请注明原文地址: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编译及演示样例的更多相关文章

  1. python解析文本文件演示样例

    目的:查找文本中还有Sum/Avg的行中低三个竖线后第一个浮点数 思路:先使用python读取文本中一行,然后切割字符串.查找含有Sum/Avgkeyword的行.取出想要的结果 文本局部: .... ...

  2. 1000个经常使用的Python库和演示样例代码

    以下是programcreek.com通过分析大量开源码,提取出的最经常使用的python库. 1. sys    (4627) 2. os    (4088)  3. re    (3563)  4 ...

  3. 支付宝即时到帐接口的python实现,演示样例採用django框架

    因工作须要研究了支付宝即时到帐接口.并成功应用到站点上,把过程拿出来分享. 即时到帐仅仅是支付宝众多商家服务中的一个,表示客户付款,客户用支付宝付款.支付宝收到款项后,立即通知你,而且此笔款项与交易脱 ...

  4. Libcurl的编译_HTTP/HTTPSclient源代码演示样例

    HTTP/HTTPSclient源代码演示样例 环境:  zlib-1.2.8  openssl-1.0.1g  curl-7.36 Author:  Kagula LastUpdateDate: 2 ...

  5. [Python] SQLBuilder 演示样例代码

    用Python写一个SQLBuilder.Java版能够从 http://www.java2s.com/Code/Java/Database-SQL-JDBC/SQLBuilder.htm 看到. 附 ...

  6. Python Web框架Tornado的异步处理代码演示样例

    1. What is Tornado Tornado是一个轻量级但高性能的Python web框架,与还有一个流行的Python web框架Django相比.tornado不提供操作数据库的ORM接口 ...

  7. Thrift的安装和简单演示样例

    本文仅仅是简单的解说Thrift开源框架的安装和简单使用演示样例.对于具体的解说,后面在进行阐述. Thrift简述                                           ...

  8. 展示C代码覆盖率的gcovr工具简单介绍及相关命令使用演示样例

    (本人正在參加2015博客之星评选,诚邀你来投票,谢谢:username=zhouzxi">http://vote.blog.csdn.net/blogstar2015/candida ...

  9. JDBC连接MySQL数据库及演示样例

    JDBC是Sun公司制定的一个能够用Java语言连接数据库的技术. 一.JDBC基础知识         JDBC(Java Data Base Connectivity,java数据库连接)是一种用 ...

随机推荐

  1. entos 7 mailx配置163邮箱发送邮件

    配置: .com .com set smtp-auth-user=wcczcl set smtp-auth=login set smtp-use-starttls set ssl-verify=ign ...

  2. hdu 5912(迭代+gcd)

    Fraction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  3. Android学习笔记(三) UI布局

    每一个布局都有其适合的方式,另外,这几个布局元素可以相互嵌套应用,做出美观的界面. 一.线性布局(LinearLayout) 线性布局,这个东西,从外框上可以理解为一个div,他首先是一个一个从上往下 ...

  4. day3 作业

    文件操作用户很广泛,我们经常对文件进行操作: global log 127.0.0.1 local2 daemon maxconn log 127.0.0.1 local2 info defaults ...

  5. 转: CreateProcessAsUser 0xC0000005访问冲突问题

    转:http://blog.csdn.net/glc22/article/details/77227367   在使用CreateProcessAsUser时出现了 0xC0000005访问冲突问题, ...

  6. CodeForces 805A Fake NP

    直觉. 一段区间中,肯定是$2$的倍数最多,因为区间长度除以$2$得到的数字最大.但只有$1$个数字的时候需要特判. #include <cstdio> #include <cmat ...

  7. 磁盘镜像分析工具TSK

    磁盘镜像分析工具TSK   TSK(The Sleuth Kit)是一款基于命令行的数字取证工具集,用于分析磁盘镜像.该工具支持常见的各种文件系统,如Ext2/Ext3/Ext4.Fat/exFat. ...

  8. PHP函数声明(三)

    /** * 一.任何参数的数量 * func_get_args()//接收一个数组,数组里面包含所有参数 * func_num_args()//取得共有几个参数 * func_get_arg(整数)/ ...

  9. Educational Codeforces Round 43 (Rated for Div. 2) ABCDE

    A. Minimum Binary Number time limit per test 1 second memory limit per test 256 megabytes input stan ...

  10. lucas定理 FOJ 2020 组合

     Problem 2020 组合 Accept: 886    Submit: 2084Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem ...