Boost Python 的安装配置以及示例 (支持python3.x)
Boost Python 的安装配置以及示例 (支持python3.x)
目录
文章目录
简介
BoostPython 可以让 Python 和 C++ 最大程度的结合起来。
在与其他语言交互时,Boost Python 并不是像 execjs 在执行新的js代码时fork一个新的进程去执行,所有的执行的部分都发生在同一个进程中,只是当前程序的控制权在Python解释器手中还是在你预先编译好的lib中
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
由于官方示例较为简洁,所以个人又把官方示例上提到的点都实现了一遍
作者:zp0int
来源链接:https://blog.csdn.net/qq_31720329/article/details/78184262
版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。
2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。