欢迎转载,转载请注明原文地址: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. python3.6升级及setuptools、pip安装

    升级python3.6 1.打开官网www.python.org,找到最新3.6.3版本,复制下载链接 2.创建/app目录,wget下载到该目录下,编译安装 mkdir /app cd /app w ...

  2. swiper (Table切换和动态加载时候出现的问题)

    本文为让心灵-去旅行原创,转载请说明.. 我们在写一个简单的swiper图片轮播的时候很简单,是写死的也就那么几张图片轮播.如果这时候图片和一些东西是后台的,你从js里动态添加到DOM时,这时候你就会 ...

  3. 51Nod 1352 集合计数(扩展欧几里德)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1352 题目大意: 给出N个固定集合{1,N},{2,N-1} ...

  4. MongoDB权威指南--笔记

    mongodb并不具备一些在关系型数据库中很普遍的功能,如连接和复杂的多行事务. 集合-->文档-->id id在文档所属的集合中是唯一的. db.help()查看数据库级别的帮助,db. ...

  5. 791. Custom Sort String

    S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...

  6. 《自己动手写docker》之namespace部门实验

    动手写一遍,印象不一样! package main import ( "log" "os" "os/exec" "syscall& ...

  7. C# HTML 生成 PDF

    原文出处:http://www.cnblogs.com/shanyou/archive/2012/09/07/2676026.html

  8. 【ASP.NET MVC】Ajax提交表单

    下面这段代码主要有几个特点: 1.Ajax提交表单 2.表单中有一个<input type="file"/> 3.当选择完图片后,利用AJAX提交表单,并在执行成功后返 ...

  9. Python学习之argparse

    http://www.2cto.com/kf/201412/363654.html https://docs.python.org/3.4/howto/argparse.html# 一.简介: arg ...

  10. promise总结

    new Promise( // 执行器 function (resolve, reject) { //一段耗时很长的异步操作 resolve(); //数据处理完成 reject(); //数据处理出 ...