The Boost library is a portable, source code C++ library that serves as a fallback to the standard library and is one of the development engines for the C++ standardization process. The Boost library was initiated by members of the C++ Standards Committee's Library Working Group, and some of it is expected to become the next generation of the C++ Standard Library content. It has a great influence in the C++ community and is an uncompromising "quasi" standard library. Boost, because of its emphasis on cross-platform, emphasis on standard C++, is not platform-independent. Most of the boost library functions only need to include the corresponding header files, and a few (such as regular expression libraries, file system libraries, etc.) need to link libraries.
installation steps:
wget https://dl.bintray.com/boostorg/release/1.64.0/source/boost_1_64_0.tar.gz
tar zxvf boost_1_64_0.tar.gz
cd boost_1_64_0.tar.gz
./bootstrap.sh --with-libraries=all --with-toolset=gcc ##--with-libraries指定编译哪些boost库,all的话就是全部编译,只想编译部分库的话就把库的名称写上,之间用 , 号分隔即可,可指定的库下面介绍。--with-toolset指定编译时使用哪种编译器,Linux下使用gcc即可,如果系统中安装了多个版本的gcc,在这里可以指定gcc的版本,比如--with-toolset=gcc-4.4
- 1
- 2
- 3
- 4
After the command is executed, it is successful if you see the following:
Building Boost.Build engine with toolset gcc... tools/build/src/engine/bin.linuxx86_64/b2
Detecting Python version... 2.6
Detecting Python root... /usr
Unicode/ICU support for Boost.Regex?... not found.
Generating Boost.Build configuration in project-config.jam...
Bootstrapping is done. To build, run:
./b2
To adjust configuration, edit 'project-config.jam'.
Further information:
- Command line help:
./b2 --help
- Getting started guide:
http://www.boost.org/more/getting_started/unix-variants.html
- Boost.Build documentation:
http://www.boost.org/build/doc/html/index.html
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
compile
./b2 toolset=gcc
- 1
install boost
./b2 install --prefix=/usr ##--prefix=/usr用来指定boost的安装目录,不加此参数的话默认的头文件在/usr/local/include/boost目录下,库文件在/usr/local/lib/目录下。这里把安装目录指定为--prefix=/usr则boost会直接安装到系统头文件目录和库文件目录下,可以省略配置环境变量。
- 1
Finally, if you want to use the boost library to compile immediately after installation, you need to execute this command:
ldconfig
- 1
Libraries that can be specified are:
library name description
atomic
chrono
context
coroutine
date_time
exception
filesystem
graph graph components and algorithms
graph_parallel
iostreams
locale
log
math
mpi metaprogramming framework implemented with templates
program_options
python mapping C++ classes and functions to Python
random
regex regular expressions type library
serialization
signals
system
test
thread portable C++ multithreading library
timer
wave
Boost usage test
takes boost_thread as an example to test whether the newly installed boost library can be used correctly. The test code is as follows:
#include <boost/thread/thread.hpp> //包含boost头文件
#include <iostream>
#include <cstdlib>
using namespace std;
volatile bool isRuning = true;
void func1()
{
static int cnt1 = 0;
while(isRuning)
{
cout << "func1:" << cnt1++ << endl;
sleep(1);
}
}
void func2()
{
static int cnt2 = 0;
while(isRuning)
{
cout << "\tfunc2:" << cnt2++ << endl;
sleep(2);
}
}
int main()
{
boost::thread thread1(&func1);
boost::thread thread2(&func2);
system("read");
isRuning = false;
thread2.join();
thread1.join();
cout << "exit" << endl;
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
Compiler:
g++ main.cpp -g -o main -lboost_thread
- 1
If the installation location of the boost library is not in the system directory, you also need to add -I and -L to specify the location of the boost header files and library files when compiling
After the compilation is successful, run the program, and the multi-threaded task implemented by boost runs correctly:
If you encounter the error shown in the figure when compiling:
implement:
LD_LIBRARY_PATH=/usr/local/lib64/:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
- 1
- 2
Then recompile and run.