Ubuntu 14.04 安装boost-python并配置工程指南

先是在MAC OSX上折腾半天无关,又花了点时间折腾Ubuntu,好歹OK了,在这里记录一下过程。


1. 安装python-dev

sudo apt-get install python-dev

2. 手动安装boost

下载boost源代码并解压到某个目录(假设为[boost-src]),然后编译boost(c++11 +python +fPIC)

./bootstrap --with-python=[python-bin-path]
./b2 cxxflags=-fPIC cflags=-fPIC --c++11
注:不太记得--c++11是否应该添加在./booststrap命令行中或是都不添加,可以都试试flag是否被识别。

3. 手动安装gflags

下载gflags源代码并解压到某个目录(假设为[gflags-src]),然后编辑CMakeList,添加-fPIC选项(在文件靠近开头的地方添加下面一行):

set (CMAKE_POSITION_INDEPENDENT_CODE True)
然后生成Makefile

cmake .
然后检查是否在CMakeFiles/flags.cmake中成功生成-fPIC
grep -R fPIC CMakeFiles/
编译gflags(+fPIC)
make
sudo make install


4. 正确配置工程

在工程的根目录下创建boost-build.jam,添加下列行

boost-build /home/ubuntu/package/boost_1_60_0/tools/build/src ;
其中后面的这个路径应该为你本机里包含bootstrap.jam的路径,通常就在[boost-src]里。

然后在包装c++函数给python的源码文件目录中添加Jamroot文件,示例如下(详见中文注释):

# Copyright David Abrahams 2006. Distributed under the Boost
# Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

import python ;

if ! [ python.configured ]
{
    ECHO "notice: no Python configured in user-config.jam" ;
    ECHO "notice: will use default configuration" ;
    using python ;
}

# Specify the path to the Boost project.  If you move this project,
# adjust this path to refer to the Boost root directory.

# 注:此目录应该指向工程自己的boost-build.jam所在目录
use-project boost
  : ../../.. ;

# Set up the project-wide requirements that everything uses the
# boost_python library from the project whose global ID is
# /boost/python.
# 注:下面示例动态库so工程,文件路径应该指向你自己的这两个.a文件的安装路径
project
  : requirements <link>shared
                 <library-file>/usr/local/lib/libboost_python.a
                 <library-file>/usr/local/lib/libgflags.a
  ;

# Declare the three extension modules.  You can specify multiple
# source files after the colon separated by spaces.
# 注:此处应包含所有生成so所需要编译的源代码文件
python-extension word_net : wordnet_python_wrapper.cc wordnet_tool.cc word_tool.cc utils.cc ;

# Put the extension and Boost.Python DLL in the current directory, so
# that running script by hand works.
install convenient_copy 
  : word_net 
  : <install-dependencies>on <install-type>SHARED_LIB <install-type>PYTHON_EXTENSION 
    <location>. 
  ;

# A little "rule" (function) to clean up the syntax of declaring tests
# of these extension modules.
local rule run-test ( test-name : sources + )
{
    import testing ;
    testing.make-test run-pyd : $(sources) : : $(test-name) ;
}

# Declare test targets
# 注:建议添加test脚本配置
run-test wordnet : word_net wordnet_python_test.py ;




阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页