问题
默认安装boost的时候,没有指定Python版本,导致boost库里只有boost_python_27的版本。因为有需求使用python3,所以重新编译安装boost_python_37的版本
重新编译的过程
准备boost源码
在boost源码目录执行下述代码
./bootstrap.sh --with-python=/usr/bin/python3.7 --prefix=/home/boost/
//prefix 是指定安装路径
//with-python 指定python二进制的文件
修改boost下的project-config.jam文件,添加如下代码
# Python configuration
import python ;
if ! [ python.configured ]
{
using python : 3.7 : "/home/python3.7.5/bin/python3.7" : /home/python3.7.5/include/python3.7m : /home/python3.7.5/lib;
}
//格式:
//using python : python版本 : "python二进制路径" : python include 路径 : python lib 的路径
//貌似第2步做的也是这个事情
4. 编译安装boost
./b2
./b2 install
ldconfig /usr/local/lib
踩过的坑
Python编译安装时如果需要动态库的话的时候需要选择--enable-shared模式,不需要动态库的话可以去掉这个选项;--enable-optimizations 会开启优化,可能不利于DEBUG,也是选择性开启
./configure --prefix=/usr/local/python3 --enable-shared CFLAGS=-fPIC --enable-optimizations
实际上Boost Python 和Python 都可以使用对应的静态库,在使用cmake链接的时候强制指定静态库,不过这时候需要编译python 和 boost python的时候都指定对应的-fPIC模式。
// boost python 需要使用下面的方式编译fPIC
./b2 cxxflags=-fPIC cflags=-fPIC -a --with-python install
//python 的话则是需要重新去掉--enable-shared 重新编译对应的静态库文件
./configure CFLAGS=-fPIC
make
make install
通过ldd 查看编译出来的so文件,可以看出是否依赖python.so 和boost python.so