Linux安装Boost Python

2,382 阅读3分钟

在Python中,直接使用C语言代码是很容易的,用ctypes即可。 而如果需要使用C++的类、对象、方法、虚函数等高级语言特性,则没那么简单。

而 Boost::Python 就是一个高度封装好的 Python / C API,它能简化 C++ 代码,使得为 Python 编写 C++ 扩展更为简单方便。甚至还能以 OOP 风格在 C++ 中编写 Python 对象的操作。

官网上介绍的tutorial,并不是很友好。对于新手来说,不一定能快速跑通hello world例子。因此,记录一下我在安装过程的步骤,让初学者能快速跑通hello world例子。

下载源代码

  1. SourceForge下载boost源代码。我用了最新了boost_1_74_0。
wget https://netix.dl.sourceforge.net/project/boost/boost/1.74.0/boost_1_74_0.tar.gz
  1. 解压缩
tar -zxvf boost_1_74_0.tar.gz

编译安装

  1. 进入解压缩后的目录
cd boost_1_74_0
  1. 执行boostrap.sh脚本
./bootstrap.sh --with-python=/usr/bin/python

执行成功后,将在当前目录下,生成b2可执行文件

  1. 编译、安装
./b2 install --with-python -q

加-q参数是在碰到第一个error时,就终止。可以及时发现编译时的问题

在写Python扩展的时候,如果想编译静态库,则这里的编译参数如下:

./b2  cxxflags="-fPIC" install --with-python -q

编译hello world

  1. 进入hello world目录
cd boost_1_74_0/libs/python/example/tutorial
  1. 执行编译命令
boost_1_74_0/b2

顺利的话,就会在当前目录生成hello_ext.so文件。如果能一把顺利完成,这月估计也能摇到车牌了。

如果想要编译完成的hello_ext.so不依赖libboot_python.so,则这里的命令改成如下:

/root/boost_1_74_0/b2 link=static cxxflags="-fPIC"

并且,需要在/usr/local/lib创建软链

ln -s libboost_python27.a libboost_python.a

遇见的问题汇总

  • fatal error: pyconfig.h No such file or directory

解决方案

apt-get install python-dev
  • 在编译hello world时报错:/usr/bin/ld: cannot find -lboost_python

"g++" -o "hello_ext.so" -Wl,-h -Wl,hello_ext.so -shared -Wl,--start-group "hello.o" -Wl,-Bstatic -Wl,-Bdynamic -lboost_python -ldl -lpthread -lutil -Wl,--end-group -fPIC -g

解决方案

  1. 先用find命令,查看是否已有相关的so文件
find / -name libboost_python*
结果
/usr/local/lib/libboost_python27.so.1.74.0
/usr/local/lib/libboost_python27.so
  1. 创建路软链
cd /usr/local/lib
ln -s libboost_python27.so.1.74.0 libboost_python.so
  • ImportError: libboost_python27.so.1.74.0: cannot open shared object file: No such file or directory

解决方案

echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib" > ~/.bashrc
source ~/.bashrc

Docker环境编译安装

记录一下在Docker环境下,安装boost::python的步骤。

  • Dockerfile
FROM debian:9

COPY ./sources.list /etc/apt/sources.list

RUN apt-get update

RUN apt-get install -y wget
RUN apt-get install -y vim
RUN apt-get install -y make
# 在Docker Debian容器中安装ps,top等命令
RUN apt-get install -y procps

RUN apt-get install -y gcc
RUN apt-get install -y g++
# ADD ./boost_1_74_0 /root/boost_1_74_0
  • sources.list
deb http://mirrors.163.com/debian/ stretch main non-free contrib
deb http://mirrors.163.com/debian/ stretch-updates main non-free contrib
deb http://mirrors.163.com/debian/ stretch-backports main non-free contrib
deb-src http://mirrors.163.com/debian/ stretch main non-free contrib
deb-src http://mirrors.163.com/debian/ stretch-updates main non-free contrib
deb-src http://mirrors.163.com/debian/ stretch-backports main non-free contrib
deb http://mirrors.163.com/debian-security/ stretch/updates main non-free contrib
deb-src http://mirrors.163.com/debian-security/ stretch/updates main non-free contrib
  • 编译docker镜像
docker build --no-cache=true -f Dockfile -t boost_1_74_0 .
  • 运行镜像
docker run -it boost_1_74_0:latest bash
  • 下载boost 源码
wget https://netix.dl.sourceforge.net/project/boost/boost/1.74.0/boost_1_74_0.tar.gz
  • 安装python
apt-get install python
apt-get install python-dev