欢迎转载,转载请注明原文地址: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. 在Ubuntu上安装搜狗输入法

    1.进入搜狗输入法官网 2.下载Linux版本,选择64bit 下载 3.等待下载的同时,进行系统配置 进入系统设置->语言支持->进行更新(需要输入登录密码)->在键盘输入法系统选 ...

  2. ASP.NET Core 上传大文件无法接收的问题

    解决办法:在API项目中配置 1. 在 web.config 文件中 <system.webServer>里加入 <security> <requestFiltering ...

  3. Codeforces 822C Hacker, pack your bags!(思维)

    题目大意:给你n个旅券,上面有开始时间l,结束时间r,和花费cost,要求选择两张时间不相交的旅券时间长度相加为x,且要求花费最少. 解题思路:看了大佬的才会写!其实和之前Codeforces 776 ...

  4. selenium webdriver操作各浏览器

    描述 本文主要是针对Chrome 62 , firefox57 ,和IE11 三个版本的操作.相关的driver .可点击以下链接.所有的driver 建议放在浏览器的目录下,本文中所有的driver ...

  5. php强制输出到浏览器下载

    $file_name="test.mp3"; $mp3_url = "";header( "Pragma: public" );header ...

  6. 爬虫基础库之beautifulsoup的简单使用

    beautifulsoup的简单使用 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: ''' Beautiful Soup提供一些简单的.p ...

  7. 浅谈BUFF设计

    Buff在游戏中无处不在,比如WOW.DOTA.LOL等等,这些精心设计的BUFF,让我们击节赞叹,沉迷其中. 问:BUFF的本质是什么? BUFF 是对一项或多项数据进行瞬间或持续作用的集合.(持续 ...

  8. ajax json 学习笔记

    json = { } JSON 字符串必须使用双引号,单引号会出现错误 三种类型: 简单值:字符串.数值.布尔值.null 对象:无序的键值对儿 数组:有序的值列表 解析:JSON.eval()   ...

  9. 【转载】Xutils3源码解析

    Github源码地址:https://github.com/wyouflf/xUtils3 原文地址 :http://www.codekk.com/blogs/detail/54cfab086c476 ...

  10. java _循环练习和数组练习

    练习 1.输出所有的水仙花数,所谓水仙花数是指一个数3位数,其每位数字立方和等于其本身,如153 = 1*1*1 + 3*3*3 + 5*5*5(很经典的题目) 分析: 通过观察发现,本题目要实现打印 ...