linux平台上编译安装boost库

from [url]http://dev.firnow.com/course/3_program/c++/cppjs/20101102/547433.html[/url]
[b]linux平台上编译安装boost库[/b]
[quote]
1.Boost库的下载地址:http://sourceforge.net/projects/boost/files/boost/1.44.0/
2.Boost库的编译,安装
下载boost_1_44_0.tar.gz
tar -zxvf boost_1_44_0.tar.gz

然后进入解压缩后的文件夹编译boost的编译器jam
cd boost_1_44_0\tools\jam
./build_dist.sh

编译完后在这个目录下有编译出的bjam文件
boost_1_44_0\tools\jam\stage\bin.linuxx86

把它copy到boost_1_44_0 然后在这个目录下运行命令编译:
./bjam "-sTOOLS=gcc" "--includedir=/usr/include" "--libdir=/usr/lib/boost" install
./bjam --toolset=gcc --includedir=/usr/local/include --libdir=/usr/local/lib/boost install
开始编译,等待编译完成,需很长时间。
关于bjam的后面的参数的设置:
-sTOOLS=gcc 指定编译器为GCC
--includedir=/usr/include/ 指定头文件的安装目录,我安装在/usr/include下。如果安装成功,将在/usr/include/生成目录boost_1_33,该目录下就是boost的头文件目录
--libdir=/usr/lib/boost 指定boost的库文件的存放位置, 生成的 .a .so 文件将放在该目录下
install 编译并安装boost
3.注意事项
安装完成后,为了每次使用时不用输入太多的目录,可以在/etc/profile中加入:
BOOST_INCLUDE=/usr/include/boost
BOOST_LIB=/usr/lib/boost
export BOOST_INCLUDE BOOST_LIB
以后在编译程序时,只需要用:-I$BOOST_INCLUDE -L$BOOST_LIB 即可,还要使用-l指定了链接库。
也许会出现编译时或者运行时找不到动态库(*.so)的错误
把/usr/lib/boost追加到动态链接库配置文件/etc/ld.so.conf中,然后直接运行ldconfig,即可!
{仅测试成功一次的方式:./bjam --toolset
lib:/newdisk/hotevent/boost_1_44_0/stage/lib
include:/newdisk/hotevent/boost_1_44_0
ln -s /newdisk/hotevent/boost_1_44_0 /usr/include/boost
并把/newdisk/hotevent/boost_1_44_0/stage/lib加入到/etc/ld.so.conf,用命令ldconfig更新一下。
在编译程序时直接用g++ re.cpp -lboost_regex -o re即可!}


4.Boost库的使用举例
第一个测试文件是lex.cpp:
#include <boost/lexical_cast.hpp>
#include <iostream>

int main()
{
using boost::lexical_cast;
int a = lexical_cast<int>("123");
double b = lexical_cast<double>("123.12");
std::cout<<a<<std::endl;
std::cout<<b<<std::endl;
return 0;
}编译:g++ lex.cpp -I$BOOST_INCLUDE -o lex
运行:./lex
输出:
123
123.12


我们的第二个例子是re.cpp:
#include <iostream>
#include <string>
#include <boost/regex.hpp>

int main() {

std::string s = "who,lives:in-a,pineapple under the sea?";

boost::regex re(",|:|-|\\s+");
boost::sregex_token_iterator
p(s.begin( ), s.end( ), re, -1);
boost::sregex_token_iterator end;

while (p != end)
std::cout << *p++ << '\n';
}编译:g++ re.cpp -I$BOOST_INCLUDE -lboost_regex -o re运行:./re输出:
who
lives
in
a
pineapple
under
the
sea?


附注:为了让动态链接库为系统所共享,还需运行动态链接库的管理命令--ldconfig
ldconfig 命令的用途,主要是在默认搜寻目录(/lib和/usr/lib)以及动态库配置文件/etc/ld.so.conf内所列的目录下,搜索出可共享的动态 链接库(格式如前介绍,lib*.so*),进而创建出动态装入程序(ld.so)所需的连接和缓存文件.缓存文件默认为 /etc/ld.so.cache,此文件保存已排好序的动态链接库名字列表.
[b]ldconfig的使用(这个很重要,我就是在这个问题上耗了很多时间)[/b]详解Linux动态库被系统共享的三种办法
linux共享库位置配置(LD_LIBRARY_PATH环境变量 或者 更改/etc/ld.so.conf 或者 使用-R选项)


安装过程参考的博文:linux下安装boost ,boost库linux编译安装,Linux中安装boost库详解


Boost下载安装编译配置使用指南(含Windows和Linux)

文章出处:飞诺网(www.firnow.com):http://dev.firnow.com/course/3_program/c++/cppjs/20101102/547433.html[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值