Boost Python 的安装配置以及示例 (支持python3.x)

6 篇文章 0 订阅

Boost Python 的安装配置以及示例 (支持python3.x)


目录


简介

BoostPython 可以让 Python 和 C++ 最大程度的结合起来。

在与其他语言交互时,Boost Python 并不是像 execjs 在执行新的js代码时fork一个新的进程去执行,所有的执行的部分都发生在同一个进程中,只是当前程序的控制权在Python解释器手中还是在你预先编译好的lib中

关于 BoostPython 的详细介绍


Boost Python 安装 (Ubuntu)

测试环境: Ubuntu 16.04
我默认使用g++ 作为编译器,使用其他工具请参考 说明

  • 编译安装
    下载 最新版 boost
# 下载完成后
tar -xzvf boost_1_65_1.tar.gz
cd boost_1_65_1

# sudo find / -name "python3.5m" # 寻找你系统下的 python3.5m
# which python3 # 寻找你系统下 python3 的安装路径
echo "using mpi ;
using gcc :  : g++ ;
using python : 3.5 : /usr/bin/python3 : /usr/include/python3.5m : /usr/local/lib ;" > ~/user-config.jam

./bootstrap.sh --with-python=/usr/bin/python3 --with-python-version=3.5 --with-python-root=/usr/local/lib/python3.5 --prefix=/usr/local
sudo ./b2 install -a --with=all
sudo ldconfig
  • apt 安装
    目前apt安装为1.58版本,若要安装更新的版本请进行编译安装
sudo apt-get install libboost-all-dev
# 搜索 libpython3.5m.so
sudo find / -name "libpython3.5m.so"
# cd 到上面搜索到的目录
cd /usr/lib/x86_64-linux-gnu/
sudo ln -s libboost_python-py35.so libboost_python3.so

提示:
-如果在编译安装时更改了 prefix path, 需要更改下面的 makefile 里的 BOOST_INC and BOOST_LIB
-我系统默认的python版本为 python3.5, 若要更改为python2.7, 请更改 makefile 里的 “PYTHON_VERSION”

安装完成


Hello World 示例

python3.5
git clone https://github.com/zpoint/Boost-Python-Examples.git
cd Boost-Python-Examples/Examples/hello_ext
make

python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello_ext
>>> hello_ext.greet()
'hello, world'
python2.7
git clone https://github.com/zpoint/Boost-Python-Examples.git
cd Boost-Python-Examples/Examples/hello_ext
vim makefile # 把第一行的 "PYTHON_VERSION = 3.5" 更改成 "PYTHON_VERSION = 2.7"
make

python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello_ext
>>> hello_ext.greet()
'hello, world'

其他示例

Boost Python 官方示例
个人对官方示例的实现 github
由于官方示例较为简洁,所以个人又把官方示例上提到的点都实现了一遍


  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值