linux boost python 编译,Ubuntu 15.04 安装 boost-python

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

#include

using namespace std;

using namespace boost;

int fun(int x, int y){return x+y;}

int main(){

int m=1; int n=2;

cout<<:bind>

return 0;

}

编译

g++ test.cpp -o test

执行

./test

结果

3

3. 安装gflags

3.1 下载 gflags

或者

git clone https://github.com/google/glog

安装命令,安装参数详细介绍见INSTALL.md

3.2 编辑CMakeList

gedit CMakeLists.txt

添加 -fPIC选项(在文件靠近开头的地方添加下面一行):

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 ..//

运行后,会出现一个单独的交互式界面,可以配置一些相关的参数

操作: c -- continue

e -- 忽略警告

g -- 生成对应的make配置文件等,并退出(出现 g 选项就可以退出了)

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 shared

/usr/local/lib/libboost_python.a

/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

: on SHARED_LIB PYTHON_EXTENSION

.

;

# 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并配置工程指南

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值