Linux编译安装boost1.48

本文详细介绍了Boost库的下载、安装步骤,包括设置环境变量、解决常见错误等,并提供了简单的测试代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[b]1.下载站:[/b]
[url]http://sourceforge.net/projects/boost/files/boost/1.48.0/[/url]
[b]2.安装[/b]

tar -xzvf boost_1_48_0.tar.g -C ~/source
cd source/boost_1_48_0/
./bootstrap.sh
sudo ./bjam --layout=versioned --build-type=complete --toolset=gcc install

[b]3.设置环境变量[/b]
在/etc/profile.d新建可执行文件boost.sh,添加文件内容:

#!/bin/sh
BOOST_INCLUDE=/usr/local/include/boost
BOOST_LIB=/usr/local/lib
export BOOST_INCLUDE BOOST_LIB

命令行执行:source /etc/profile.d/boost.sh
以后在编译程序时,只需要用:-I$BOOST_INCLUDE -L$BOOST_LIB 即可,还要使用-l指定了链接库。
[b]4.设置共享库链接目录[/b]
/etc/ld.so.conf文件内容后面追加/usr/local/lib
命令行执行

sudo ldconfig

[b]5.遇到的问题及解决方案[/b]
安装过程爆出很多和bz相关的错误,解决方案如下

sudo apt-get install libbz2-dev

PS 安装过程中的错误:

libs/iostreams/src/bzip2.cpp:20:56: error: bzlib.h: 没有那个文件或目录
libs/iostreams/src/bzip2.cpp:31: error: ‘BZ_OK’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:32: error: ‘BZ_RUN_OK’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:33: error: ‘BZ_FLUSH_OK’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:34: error: ‘BZ_FINISH_OK’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:35: error: ‘BZ_STREAM_END’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:36: error: ‘BZ_SEQUENCE_ERROR’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:37: error: ‘BZ_PARAM_ERROR’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:38: error: ‘BZ_MEM_ERROR’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:39: error: ‘BZ_DATA_ERROR’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:40: error: ‘BZ_DATA_ERROR_MAGIC’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:41: error: ‘BZ_IO_ERROR’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:42: error: ‘BZ_UNEXPECTED_EOF’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:43: error: ‘BZ_OUTBUFF_FULL’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:44: error: ‘BZ_CONFIG_ERROR’ was not declared in this scope
libs/iostreams/src/bzip2.cpp:48: error: ‘BZ_FINISH’ was not declared in this scope
//mostly like above....

[b]6.测试[/b]

#include <boost/python.hpp>
char const* greet()
{
return "hello, world";
}

BOOST_PYTHON_MODULE(hello)
{
using namespace boost::python;
def("greet", greet);
}

把代码存为hello.cpp, 编译成so库

g++ hello.cpp -o hello.so -shared -I/usr/include/python2.6 -I /usr/lib/python2.6/config/ -lboost_python

此处python路径设为你的python路径, 并且必须加-lboost_python, 这个库名不一定是这个, 去/user/local/lib查
然后在有此so库的目录, 进入python, 可以如下使用

>>> import hello
>>> hello.greet()
'hello, world'

[b]7.测试遇到问题及解决方案[/b]
如果遇到问题:

/usr/bin/ld: cannot find -lboost_python
collect2: ld returned 1 exit status

解决方案:
[list]
[*] 确认/etc/ld.so.conf.d/相关配置文件已包含boost_python.so所在目录
[*] boost_python库所在目录(默认安装在/usr/local/lib)存在libboost_python.so,若不存在则从libboost_python.so.***建软链接到libboost_python.so
[/list]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值