---
title: "linux平台上编译安装boost库"
author: "sealbird"
date: "2011-01-06T09:33:21Z"
tags: ["Cache", "GCC", "Linux", "Windows", "配置管理"]
categories: ["编程语言"]
slug: "linux-boost"
draft: false
lastmod: "2011-01-06T09:34:14Z"
id: 860256
url: "https://sealbird.iteye.com/blog/860256"
---

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 #include int main() { using boost::lexical\_cast; int a = lexical\_cast("123"); double b = lexical\_cast("123.12"); std::cout&lt; #include #include 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 &lt;&lt; \*p++ &lt;&lt; '\\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]