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
./bootstrap.sh -help
2.2 编译 boost
sudo ./b2 install
./b2 cxxflags=-fPIC cflags=-fPIC --c++11
成功后会出现
@H_404_96@The @H_404_96@Boost @H_404_96@C++ @H_404_96@Libraries @H_404_96@were @H_404_96@successfully @H_404_96@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;
}
编译
@H_404_96@g++ @H_404_96@test.@H_404_96@cpp -@H_404_96@o @H_404_96@test
执行
./test
结果
3
3. 安装gflags
3.1 下载 gflags
或者
git clone https:@H_404_96@//github.com/google/glog
安装命令,安装参数详细介绍见INSTALL.md
3.2 编辑CMakeList
gedit CMakeLists.txt
set (CMAKE_POSITION_INDEPENDENT_CODE True)
生成Makefile
cmake ..
检查是否在CMakeFiles/flags.cmake中成功生成-fPIC
cd gflags
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
mkdir build && cd build
ccmake ..@H_404_96@//
@H_404_96@运行后,会出现一个单独的交互式界面,可以配置一些相关的参数
@H_404_96@操作: @H_404_96@c -- @H_404_96@continue
@H_404_96@e -- @H_404_96@忽略警告
@H_404_96@g -- @H_404_96@生成对应的make配置文件等,并退出(出现 @H_404_96@g @H_404_96@选项就可以退出了)
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 ;
参考文献