【boost】安装boost

安装boost

先进入解压缩后的目录:

cd boost_1_58_0

boost 包含众多独立的库,使用 --show-libraries 查看将会编译安装的库文件列表::

./bootstrap.sh --show-libraries

编译:

./bootstrap.sh --with-libraries=all --with-toolset=gcc

–with-libraries指定编译哪些boost库,all的话就是全部编译,只想编译部分库的话就把库的名称写上,之间用 , 号分隔即可,可指定的库有以下几种:

库名 说明
atomic  
chrono  
context  
coroutine  
date_time  
exception  
filesystem  
graph 图组件件和算法
graph_parallel  
iostreams  
locale  
log  
math  
mpi 用模板实现的元编程框架
program_options  
python 把C++类和函数映射到Python之中
random  
regex 正则表达式库
serialization  
signals  
system  
test  
thread 可移植的C++多线程库
timer  
wave  

如你希望构建某些特定模块(如 systemfilesystem):

./bootstrap.sh --with-libraries=system,filesystem
sudo ./b2 install --prefix=/opt/boost_1_84

 

命令执行完成后看到显示如下即为成功:

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

编译:

./b2 toolset=gcc

最后执行以下命令开始安装boost:

./b2 install --prefix=/usr

–prefix=/usr用来指定boost的安装目录,不加此参数的话默认的头文件在/usr/local/include/boost目录下,库文件在/usr/local/lib/目录下。这里把安装目录指定为–prefix=/usr则boost会直接安装到系统头文件目录和库文件目录下,可以省略配置环境变量。

 

卸载:默认的头文件在/usr/local/include/boost目录下,库文件在/usr/local/lib/目录下.

类型 路径 说明
头文件 /usr/local/include/boost Boost 头文件目录
静态库 .a /usr/local/lib/libboost_*.a Boost 静态库文件
动态库 .so /usr/local/lib/libboost_*.so* Boost 动态库和符号链接文件

因此直接删除文件即可:

sudo rm -rf /usr/local/include/boost
sudo rm -rf /usr/local/lib/libboost*

 

动态库安装后无法运行怎么办?

虽然 .so 安装到了 /usr/local/lib/,但有些系统(如 Ubuntu)默认不会在此目录查找动态库,你运行程序可能会遇到:

error while loading shared libraries: libboost_*.so: cannot open shared object file: No such file or directory

✅ 解决方法:

方法 1:运行时设置 LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
./your_program

方法 2:系统级添加库路径:

echo "/usr/local/lib" | sudo tee /etc/ld.so.conf.d/boost.conf
sudo ldconfig

 

例子:

#include <boost/thread/thread.hpp> //包含boost头文件
#include <cstdlib>
#include <iostream>
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 << "func2:" << 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;
}

输出:

[root@openeuler test]# g++ test01.cc  -g -lboost_thread
[root@openeuler test]# ./a.out 
func1:0
func2:0
func1:1
func2:1
func1:2

 

参考资料

1. Ubuntu/Linux系统编译安装boost库

posted @ 2021-11-09 21:54  苏格拉底的落泪  阅读(38)  评论(0)    收藏  举报