您的位置:首页 > 编程语言 > Python开发

Boost.python的源码编译安装(尝试解决Boost.Python.ArgumentError)

2018-03-02 15:44 1306 查看

问题描述

在本人尝试用pycaffe运行一个程序(snntoolbox)时,出现了

Boost.Python.ArgumentError: Python argument types in Net.__init__(Net, unicode, int) did not match C++ signature:
的报错,在翻过各大论坛的相关回答和官方网站的教程之后,主要意见有:1.传递参数类型与函数声明中不一致;2.Boost不应该由
sudo apt install
安装,而应该直接由源码根据自身的python进行编译(build against python version)。

根据本人的情况分析(pycaffe+snntoolbox运行),更有可能是由于第二个原因引起,因此尝试解决这个问题的话,自然要学习如何通过源码编译安装。(结果学会了从源码安装boost,但是没能够解决报错,猜想可能是因为:a.卸载不干净,b.caffe的Makefile里面的设置问题)

本人对于boost是个门外汉,文章应该有蛮多纰漏的,欢迎指正。

boost简要介绍

boost: Boost库是为C++语言标准库提供扩展的一些C++程序库的总称。

boost.python:它是boost库的一部分,随boost一起安装,用来实现C++和Python代码的交互。

libboost-dev的卸载

如果你之前已经用
sudo apt install libboost-dev libboost-all-dev
的方式安装了boost(比如你在Ubuntu16.04下按照CUDA官网推荐方式安装了caffe的话),建议先卸载。

卸载方式如下:

sudo apt remove libboost-dev libboost-all-dev


Boost.python的编译安装

说明:因为caffe需要boost的支持,在卸载以后所有boost文件都需要通过安装(对于非boost.python来说,安装就是将头文件拷贝到PREFIX路径[默认为/usr/local]),不只boost.python(boost.python不是headers-only文件,需要单独build)

step1:源码文件的下载和解压

首先,按照boost官网下载指引描述,到所示网站下载boost源码的压缩文件,当前时间的文件是
boost_1_66_0.tar.bz2
,放在$HOME目录下。

~$ tar --bzip2 -xvf boost_1_66_0.tar.bz2
~$ cd boost_1_66_0
~/boost_1_66_0$ ./bootstrap.sh


#返回信息如下
Building Boost.Build engine with toolset gcc... tools/build/src/engine/bin.linuxx86_64/b2
Detecting Python version... 2.7
Detecting Python root... /usr
Unicode/ICU support for Boost.Regex?... not found.
Backing up existing Boost.Build configuration in project-config.jam.1
Generating Boost.Build configuration in project-config.jam... #配置文件名为project-config.jam

Bootstrapping is done. To build, run:

./b2 #通常的下一步操作

To adjust configuration, edit 'project-config.jam'.
Further information:

- Command line help:
./b2 --help #如果需要更详细的说明,运行左边的命令后再根据需要修改自己的命令

- Getting started guide: http://www.boost.org/more/getting_started/unix-variants.html 
- Boost.Build documentation: http://www.boost.org/build/doc/html/index.html[/code] 
我们应关注上述返回信息,包括配置文件名和一些使用提示。

step2:重新编译boost

这一步是为了保险起见,将所有boost相关文件都重新编译,运行一下命令:

~/boost_1_66_0$ ./b2 -a


关注shell返回信息中,建议将路径1加入#include path,将路径2加入#compiler path,以下根据指引设置环境变量。

step3:设置环境变量

~$ sudo gedit ~/.profile
#根据上一步shell中返回的信息,在打开的文件中加入如下两行信息
export BOOST_INCLUDE=$HOME/boost_1_66_0
export BOOST_LIB=$HOME/boost_1_66_0/stage/lib
#保存后关闭文件
~$source ~/.profile


step4:验证安装boost成功

对于我来说,因为caffe是需要boost支持的,验证方式是在caffe文件夹下重新编译Makefile.config,依次
make all
,
make test
,
make runtest
直至最后测试完全通过。

当然简单的方式,应该是调用库,运行一小段代码呗。

~$ touch sample.cpp
~$ sudo gedit sample.cpp


向打开的文件中填入以下代码

#include <string>
#include <iostream>
#include <boost/version.hpp>
#include <boost/timer.hpp>
using namespace std;
int main()
{
boost::timer t;
cout << "max timespan: " << t.elapsed_max() / 3600 << "h" << endl;
cout << "min timespan: " << t.elapsed_min() << "s" << endl;
cout << "now time elapsed: " << t.elapsed() << "s" << endl;
cout << "boost version" << BOOST_VERSION <<endl;
cout << "boost lib version" << BOOST_LIB_VERSION <<endl;
return 0;
}


~$ g++ sample.cpp -o sample.out #编译生成sample.out文件
~$ ./sample.out #查看输出


得到以下输出

max timespan: 2.56205e+09h
min timespan: 1e-06s
now time elapsed: 0.000135s
boost version105800#好吧,真的是没有卸载干净....
boost lib version1_58


参考资料

boost官方指引

Ubuntu上完全编译安装boost
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐