欢迎转载,转载请注明原文地址: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. python是如何进行内存管理的?

    Python内存管理机制 Python内存管理机制主要包括以下三个方面: 引用计数机制 垃圾回收机制 内存池机制 引用计数 举个例子说明引用是什么: 1 如上为一个简单的赋值语句,1就是对象,a就是引 ...

  2. Django 1.10文档中文版Part4

    2.10 高级教程:如何编写可重用的apps 2.10.1 重用的概念 The Python Package Index (PyPI)有大量的现成可用的Python库.https://www.djan ...

  3. php PDO判断连接是否可用的方法

    转载自:傲雪星枫  原文地址: http://blog.csdn.net/fdipzone/article/details/53117541 mysql_ping() 检查到服务器的连接是否正常.如果 ...

  4. #include<stdarg.h> 可变参数使用

    今天上计算方法这课时觉得无聊至极,于是拿出C++编程之道来看了看..无意之中看到了#include<stdarg.h> va_list,va_start,va_end等东西,不知是怎么用的 ...

  5. Python抓取微博评论

    本人是张杰的小迷妹,所以用杰哥的微博为例,之前一直看的是网页版,然后在知乎上看了一个抓取沈梦辰的微博评论的帖子,然后得到了这样的网址 然后就用m.weibo.cn进行网站的爬取,里面的微博和每一条微博 ...

  6. [BZOJ3150][Ctsc2013]猴子 期望dp+高斯消元

    3150: [Ctsc2013]猴子 Time Limit: 20 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 163  Solved: 10 ...

  7. spring boot 使用不同的profile来加载不同的配置文件

    在开发过程之中,经常需要在开发和测试环境中进行互相切换,当切换的同时需要加载相应的配置文件,因此要经常 性的对配置文件进行相应的修改,长此以往感到十分痛苦.如果能针对开发和测试环境分别建两个不同的配置 ...

  8. pyqt5简单登陆界面

          登陆界面姓名输入错误会弹出错误信息.正确就会弹出第二个窗体. # -*- coding:utf-8 -*- import sys from PyQt5.QtWidgets import Q ...

  9. 11.Spark Streaming源码解读之Driver中的ReceiverTracker架构设计以及具体实现彻底研究

    上篇文章详细解析了Receiver不断接收数据的过程,在Receiver接收数据的过程中会将数据的元信息发送给ReceiverTracker:   本文将详细解析ReceiverTracker的的架构 ...

  10. javascript 进制转换(2进制、8进制、10进制、16进制之间的转换)

    //十进制转其他 var x=110; alert(x); alert(x.toString(8)); alert(x.toString(32)); alert(x.toString(16)); // ...