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

来源:互联网 发布:ubuntu 没有rc.local 编辑:程序博客网 时间:2024/06/02 19:07

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


目录

  • Boost Python 的安装配置以及示例 支持python3x
      • 目录
      • 简介
      • Boost Python 安装 Ubuntu
      • Hello World 示例
        • python35
        • python27
      • 其他示例


简介

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

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

关于 BoostPython 的详细介绍


Boost Python 安装 (Ubuntu)

测试环境: Ubuntu 16.04
我默认使用g++ 作为编译器,要使用cmake或者其他配置请参考 说明

  • 编译安装
    下载 最新版 boost
# 下载完成后tar -xzvf boost_1_65_1.tar.gzcd 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/localsudo ./b2 install -a --with=allsudo ldconfig
  • apt 安装
    目前apt安装为1.58版本,若要安装更新的版本请进行编译安装
sudo apt-get install libboost-all-dev# 搜索 libpython3.5m.sosudo 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.gitcd Boost-Python-Examples/Examples/hello_extmakepython3Python 3.5.2 (default, Nov 17 2016, 17:05:23)[GCC 5.4.0 20160609] on linuxType "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.gitcd Boost-Python-Examples/Examples/hello_extvim makefile # 把第一行的 "PYTHON_VERSION = 3.5" 更改成 "PYTHON_VERSION = 2.7"makepythonPython 2.7.12 (default, Nov 19 2016, 06:48:10)[GCC 5.4.0 20160609] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import hello_ext>>> hello_ext.greet()'hello, world'

其他示例

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


阅读全文
1 0
原创粉丝点击