1. 安装依赖库
sudo apt-get install python-dev
sudo apt-get install mpi-default-dev #安装mpi库
sudo apt-get install libicu-dev #支持正则表达式的UNICODE字符集
sudo apt-get install libbze-dev # 如果编译出现错误:bzlib.h : No such file or directory
sudo apt-get update
2. 手动安装Boost
2.1 下载 boost源代码,然后编译 boost (C++11 Python fPIC),或者访问 boost.org.
#解压
unzip boost_1_63_0.zip
cd boost_1_63_0
./bootstrap.sh --with-python=PYTHON
./bootstrap.sh --with-libraries=system,thread,python
会生成 b2和bjam文件。可查看使用方法
./bootstrap.sh -help
2.2 编译 boost
sudo ./b2 install
./b2 cxxflags=-fPIC cflags=-fPIC --c++11
成功后会出现
The Boost C++ Libraries were successfully built!
2.3 建立一个 test.cpp文件测试
gedit test.cpp
添加如下内容
#include<iostream>
#include<boost/bind.hpp>
using namespace std;
using namespace boost;
int fun(int x, int y){return x+y;}
int main(){int m=1; int n=2;cout<<boost::bind(fun,_1,_2)(m,n)<<endl;return 0;}
编译
g++ test.cpp -o test
执行
./test
结果
3
3. 安装gflags
3.1 下载 gflags
安装命令,安装参数详细介绍见INSTALL.md
3.2 编辑CMakeList
gedit CMakeLists.txt
添加 -fPIC选项(在文件靠近开头的地方添加下面一行):
set (CMAKE_POSITION_INDEPENDENT_CODE True)
生成Makefile
cmake .
检查是否在CMakeFiles/flags.cmake中成功生成-fPIC
grep -R fPIC ./build/CMakeFiles/
出现如下结果
./build/CMakeFiles/gflags_nothreads_static.dir/flags.make:CXX_FLAGS = -03 -DNDEBUG -fPIC
./build/CMakeFiles/gflags_static.dir/flags.make:CXX_FLAGS = -03 -DNDEBUG -fPIC
3.3 编译 gflags (+ fPIC)
make
make test #(optional)
sudo make install
4. 配置
4.1 在工程的根目录下创建boost-build.jam,添加下列行
cd boost_1_63_0
gedit bootstrap.jam
添加如下内容
boost-build /home/bids/boost_1_63_0/tools/build/src
其中后面的这个路径应该为你本机里包含bootstrap.jam的路径。
然后在包装c++函数给python的源码文件目录中添加Jamroot文件。
4.2 编辑 jamroot
gedit 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 : /home/bids/boost_1_63_0/tools/build/src ; # 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 ;
参考文献
在Ubuntu上安装boost库
Ubuntu 14.04 安装boost-python并配置工程指南