boost的简单编译过程:
从 boost 的官方网址 http://www.boost.org/ 下载 bz2、zip、7z 等格式的源码发布包。最新的发布版是 1.43.0 (May 6th, 2010)。
酱子解压 bz2 包(嫌输出诊断文本烦的话,把 -v 去掉或 > file.log):
tar -xvjf boost_[ver].tar.bz2
Boost Getting Started Guide
Boost Getting Started Guide
Boost Getting Started on Windows
Header-Only Libraries
boost 中有些库是不需要编译成独立的库文件的,所有的特性都在头文件中实现,叫做 header-only lib,比如:lambda。这时只要 include boost 的头文件就可以了,例子:
查看源代码
打印帮助
01 #include <boost/lambda/lambda.hpp>
02 #include <iostream>
03 #include <iterator>
04 #include <algorithm>
05
06 int main()
07 {
08 using namespace boost::lambda;
09 typedef std::istream_iterator<int> in;
10
11 std::for_each(
12 in(std::cin), in(), std::cout << (_1 * 3) << " " );
13 }
用 gcc 的 -I 选项指定头文件的搜索路径。
必需独立编译的库有:
Boost.Filesystem
Boost.IOStreams
Boost.ProgramOptions
Boost.Python (see the Boost.Python build documentation before building and installing it)
Boost.Regex
Boost.Serialization
Boost.Signals
Boost.System
Boost.Thread
Boost.Wave
有些库可以编译也可以 header-only 方式使用,视你使用的特性而定:
Boost.DateTime has a binary component that is only needed if you're using its to_string/from_string or serialization features, or if you're targeting Visual C++ 6.x or Borland.
Boost.Graph also has a binary component that is only needed if you intend to parse GraphViz files.
Boost.Test can be used in “header-only” or “separately compiled” mode, although separate compilation is recommended for serious use.
上述库都可以在 Getting Started 上找到链接。
编译 boost 库
需要独立编译的 boost 库叫做 separately-compiled lib。
通常使用一个叫 bjam 的配置管理工具来编译 boost,就像是 make。在 这里 去找 bjam 的信息。
确定你的编译工具集(Toolset),比如 msvc、gcc 等。这里 有完整的工具集列表。对于 gcc,boost 的 bjam 支持 Cygwin 和 MinGW 两种主流的发行版。
我用gcc 编译,从 bash 进入到 boost 的源码目录下,运行 bootstrap.sh,然后再运行 ./bjam 就完成编译了,注意:一定要使用 boost 目录下由 bootstrap.sh 生成的 bjam,而不是从网上下载的已编译好的 bjam。如果你想指定编译选项,或选择性的指定要编译的 boost 特性库,可以用 bootstrap.sh --help 查看有哪些选项。如下:
查看源代码
打印帮助
1 $ cd /path/to/boost
2 $ ./bootstrap.sh --help
3 (一堆帮助)
4
5 $ ./bootstrap.sh
6 $ ./bjam stage
stage 方式指编译后把库(.a、.dll)放到 boost 源码包的目录下(默认在 stage/lib 下),不进行向安装目录的头文件和库文件的拷贝,与 stage 方式相对的是 install 方式。
有一些编译选项控制生成库的 debug/release 和 static/shared 编译方式,在文档中称为 build variants of the lib。和这些相关的选项有:variant=debug|release,link=static|shared,runtime- link=static|shared,--build-type=minimal|complete 等,详细的说明参考 bjam --help
用 bjam 的 --show-libraries 显示支持编译的特性库,用 --with-library-name 和 --without-library-name 定制编译的特性库。
默认的编译过程很慢,请耐心等待……
boost 库的命名规范
生成的 boost 库有一定的命名规范,来表示它的编译特征,如:静态/动态,调试版/发布版,编译使用的工具集等,详见:Library Naming。如下:
In order to choose the right binary for your build configuration you need to know how Boost binaries are named. Each library filename is composed of a common sequence of elements that describe how it was built. For example, libboost_regex-vc71-mt-d-1_34.lib can be broken down into the following elements:
lib
Prefix: except on Microsoft Windows, every Boost library name begins with this string. On Windows, only ordinary static libraries use the lib prefix; import libraries and DLLs do not.
boost_regex
Library name: all boost library filenames begin with boost_.
-vc71
Toolset tag: identifies the toolset and version used to build the binary.
-mt
Threading tag: indicates that the library was built with multithreading support enabled. Libraries built without multithreading support can be identified by the absence of -mt.
-d
ABI tag: encodes details that affect the library's interoperability with other compiled code. For each such feature, a single letter is added to the tag:
s: linking statically to the C++ standard library and compiler runtime support libraries.
g: using debug versions of the standard and runtime support libraries.
y: using a special debug build of Python.
d: building a debug version of your code.
p: using the STLPort standard library rather than the default one supplied with your compiler.
n: using STLPort's deprecated "native iostreams" feature.
For example, if you build a debug version of your code for use with debug versions of the static runtime library and the STLPort standard library in “native iostreams” mode, the tag would be: -sgdpn. If none of the above apply, the ABI tag is ommitted.
-1_34
Version tag: the full Boost release number, with periods replaced by underscores. For example, version 1.31.1 would be tagged as "-1_31_1".
.lib
Extension: determined according to the operating system's usual convention. On most unix-style platforms the extensions are .a and .so for static libraries (archives) and shared libraries, respectively. On Windows, .dll indicates a shared library and .lib indicates a static or import library. Where supported by toolsets on unix variants, a full version extension is added (e.g. ".so.1.34") and a symbolic link to the library file, named without the trailing version number, will also be created.
使用、链接 boost 库
要参考 Getting Started on Unix Variants 的 Link Your Program to a Boost Library,而不是 Windows 的。
示例代码,用到了 Boost.Regex 特性库:
查看源代码
打印帮助
01 #include <boost/regex.hpp>
02 #include <iostream>
03 #include <string>
04
05 int main()
06 {
07 std::string line;
08 boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );
09
10 while (std::cin)
11 {
12 std::getline(std::cin, line);
13 boost::smatch matches;
14 if (boost::regex_match(line, matches, pat))
15 std::cout << matches[2] << std::endl;
16 }
17 }
像酱子设定编译、链接选项:
查看源代码
打印帮助
1 g++ -o foo.exe -I/path/to/boost -L/path/to/boost/stage/lib -lboost_regex foo.cpp
-l 指定库名字时,去掉前缀 lib 和后缀 .a/.lib。gcc 会自动选择 static 或 shared (dynamic) 的库,比如:在 stage/lib 中有两套 boost.regex 的库,静态库 libboost_regex.a,动态库和其导入库 cygboost_regex.dll/libboost_regex.dll.a,如果仅指定 -lboost_regex,gcc 会链接到动态库 cygboost_regex.dll 上,这时在运行客户程序时需要动态库 cygboost_regex.dll 能被 Windows 加载器找到(当前目录、System32、PATH 目录等搜索顺序)。如果要链接静态库,需要使用 -static 选项,试验后发现仅使用这个选项不行,它会强制链接 gcc 的 RT 静态库,然后提示 cannot find -lgcc_s,不过可以不使用 -L/-l 自动搜索库选项,而是直接在链接选项中输入静态库 libboost_regex.a 的全路径来完成 boost 静态库的链接。
测试例子:
To: George Shmidlap
From: Rita Marlowe
Subject: Will Success Spoil Rock Hunter?
---
See subject.
上面文本保存为 test.mail,然后运行我们的程序 foo.exe:./foo.exe < test.mail。OK,boost.regex 将会抽出 subject 的文本并打印出来。
原文
http://hi.baidu.com/jzinfo/blog/item/ae41d73924fd03fa3b87cefd.html
安装别的软件时遇到两个问题:
一、 error: 'class boost::filesystem3::directory_entry' has no member named 'leaf'
解决办法:
If there's a lot of problems like this, compile with
-DBOOST_FILESYSTEM_VERSION=2 and have upstream decide what to do
long-term. In this particular case, you should be able to replace
leaf() with path().filename().string().
二、老是连接不成功。link error 修改 /etc/ld.so.conf