知道boost很久了,一直没有时间去看,今天空下来时间,决定搞一下boost。
用百度搜索了一下boost,相关的内容很少,大部分都是讲boost如何安装的。具体如何使用,涉猎的很少,不知道是会用boost的同学觉得 boost用法太简单,不需要描述,还是因为英文文档已经足够,不需要再做翻译这样的无用功。但我一向认为,中国大多数程序员,英文水平都是非常有限的, 包括我在内,看到英文文档就会头疼。
这篇文章主要还是介绍如何安装boost,介绍boost的库的一些使用方法(我几乎找不到关于boost库使用的中文资料)
开始前,推荐几个网址给大家,看这些网址里的东西会比我这篇文章更有效:
英文好的请直接去boost的官方网站:
http://www.boost.org
中文方面,直接访问
Boost 中文站
言归正传,开始boost之旅吧,本文主要翻译来自 Boost Getting Started ,对其中部分内容做一点翻译和加工,英文好的同学,请直接查看该网页。
1、获取安装包
#include <boost/***.hpp>
或者
#include "boost/***.hpp"
哪种格式取决于你如何引用了。
3、头文件就是库
使用者最常问的问题就是“我该怎么安装Boost”,这个也是我一开始最关心的问题,Boost这点做的很好,将大部分实现都封装在头文件里,所以对于一些基本的Boost库,其实是不需要安装的,只需要将头文件include到自己的程序里,当然前提是你把Boost的所有用到的头文件都拷贝了一份。
Boost是如何做到这点的?
这是因为Boost的头文件(*.hpp)包含了模板和内联函数,这点随便找一个hpp文件来看你就明白了,所以不需要去静态链接活动态链接二进制lib库了。
不过Boost的某些库还是需要生成二进制的库的,如果你要使用他们,必须编译安装哦,他们是:
4、编写你的第一个Boost程序
下面这个例子就是告诉你如何编写一个不需要链接boost二进制库,又能使用boost库的简单程序。
test.cpp:
#include
<
boost
/
lambda
/
lambda.hpp
>
#include
<
iostream
>
#include
<
iterator
>
#include
<
algorithm
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
int
main()
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
using namespace boost::lambda;
std::cout << "Hello Boost!" << std::endl;
return 0;
}
假定boost的头文件被安装在/home/kunp/usr/local/include/boost-1_34_1
执行下列语句进行编译:
g++ -I /home/kunp/usr/local/include/boost-1_34_1/ test.cpp -o test
5、使用Boost二进制库的准备
用百度搜索了一下boost,相关的内容很少,大部分都是讲boost如何安装的。具体如何使用,涉猎的很少,不知道是会用boost的同学觉得 boost用法太简单,不需要描述,还是因为英文文档已经足够,不需要再做翻译这样的无用功。但我一向认为,中国大多数程序员,英文水平都是非常有限的, 包括我在内,看到英文文档就会头疼。
这篇文章主要还是介绍如何安装boost,介绍boost的库的一些使用方法(我几乎找不到关于boost库使用的中文资料)
开始前,推荐几个网址给大家,看这些网址里的东西会比我这篇文章更有效:
英文好的请直接去boost的官方网站:
http://www.boost.org
中文方面,直接访问
Boost 中文站
言归正传,开始boost之旅吧,本文主要翻译来自 Boost Getting Started ,对其中部分内容做一点翻译和加工,英文好的同学,请直接查看该网页。
1、获取安装包
最好的办法就是从 SourceForge 下载boost安装包
-
Download boost_1_34_1.tar.bz2.
-
在你希望解压的目录下,解压安装包,执行以下命令
tar --bzip2 -xf /path/to/boost_1_34_1.tar.bz2
目录结构(略)
头文件的组织结构:
Boost库的头文件一般按下列规则存放:
-
boost/ 目录下存放旧库以及小库。.
-
boost/的各个子目录存放大多数库的公共头文件, 比如, 你可以这样找到Python库的def.hpp 头文件:
boost/python/def.hpp.
-
一些库会在boost/目录下存放一个“聚合头文件”,包含该库所有的头文件。如, Boost.Python'的聚合头文件:
boost/python.hpp.
-
大多数库拥有私有的目录detail/, 或者 aux_/. 这些目录下的头文件不能被用户调用。
#include <boost/***.hpp>
或者
#include "boost/***.hpp"
哪种格式取决于你如何引用了。
3、头文件就是库
使用者最常问的问题就是“我该怎么安装Boost”,这个也是我一开始最关心的问题,Boost这点做的很好,将大部分实现都封装在头文件里,所以对于一些基本的Boost库,其实是不需要安装的,只需要将头文件include到自己的程序里,当然前提是你把Boost的所有用到的头文件都拷贝了一份。
Boost是如何做到这点的?
这是因为Boost的头文件(*.hpp)包含了模板和内联函数,这点随便找一个hpp文件来看你就明白了,所以不需要去静态链接活动态链接二进制lib库了。
不过Boost的某些库还是需要生成二进制的库的,如果你要使用他们,必须编译安装哦,他们是:
- 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.Thread
- Boost.Wave
4、编写你的第一个Boost程序
下面这个例子就是告诉你如何编写一个不需要链接boost二进制库,又能使用boost库的简单程序。
test.cpp:
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/1327ab569c1ae82736693a50b8e33378.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/0196c3df5ea9e936f21e9932cca91014.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
假定boost的头文件被安装在/home/kunp/usr/local/include/boost-1_34_1
执行下列语句进行编译:
g++ -I /home/kunp/usr/local/include/boost-1_34_1/ test.cpp -o test
5、使用Boost二进制库的准备
5.1、简易安装
$ cd path/to/boost_1_34_1
$ ./configure --help
这个可以帮你查看configure的参数,不过最有用的还是
$ ./configure --prefix=path/to/installation/prefix
将Boost二进制库安装到指定目录。
在我们这次的例子里,指定目录为/home/kun/usr/local,如果不指定目录,默认的目录会是/usr/local,这个通过看生成的Makefile,就能知道了。
所以我们执行以下命令:
___FCKpd___3nbsp;./configure --prefix=/home/kunp/usr/local/
___FCKpd___3nbsp;make install
这个需要很长时间,编译好了以后,你会看到在/home/kunp/usr/local/目录下多了lib/和include/两个子目录,其中lib/目录存放所有生成的静态和动态链接库,inclue/留了一份所有头文件的拷贝。
5.2、高阶安装(指定编译器等等)
高阶的安装超过本文的范畴,直接略过。6、使用Boost二进制库
下面的程序将使用Boost.Regex 库,该库就是需要Boost独立的二进制库
example.cpp:
#include < boost / regex.hpp >
#include < iostream >
#include < string >
int main()
... {
std::string line;
boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );
while (std::cin)
...{
std::getline(std::cin, line);
boost::smatch matches;
if (boost::regex_match(line, matches, pat))
std::cout << matches[2] << std::endl;
}
}
使用动态库进行编译:
g ++ - I / home / kunp / usr / local / include / boost - 1_34_1 / example . cpp - o example
- L / home / kunp / usr / local / lib / - lboost_regex - gcc32 - mt - d
在我的机器上编译通过,当然,前提是你的路径写对。
6.1、Boost库的命名规则
以 libboost_regex-vc71-mt-d-1_34.lib 为例:-
lib
- Prefix: 除了Microsoft Windows操作系统, 每一个Boost库都以lib为开始。在Windows下, 只有静态库使用 lib 开始. boost_regex
- 库名: 所有Boost库都以 boost_为库名. -vc71
- Toolset tag: 表示编译二进制的工具的名称和版本号. -mt
- Threading tag: 标明该库支持多线程。 -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:
Key Use this library when: 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.5 p using the STLPort standard library rather than the default one supplied with your compiler. n using STLPort's deprecated “native iostreams” feature.6 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 (except for static libraries built by the gcc toolset, whose names always end in .a) .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.