参考网址:

Ubuntu下安装boost库_上善若水-CSDN博客

在Ubuntu上安装boost库_一点一滴-CSDN博客

Ubuntu20.04安装boost库_vanyongqi-CSDN博客_ubuntu 安装boost

安装

apt安装

1
sudo apt-get install libboost-dev

build安装

  • 下载boost库:Boost C++ Libraries
  • 解压: tar -xzvf boost_1_xx_xx.tar.gz
  • 进入解压目录,执行脚本,sudo ./bootstrap.sh
  • 安装, sudo ./b2 install
  • 测试环境变量
1
2
3
4
5
6
sudo gedit /etc/profile

#在文件末尾添加
export CPLUS_INCLUDE_PATH=/usr/local/include/boost:$CPLUS_INCLUDE_PATH
export LIBRARY_PATH=/usr/local/lib:$LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

测试

1
sudo nano test.cpp
1
2
3
4
5
6
7
8
9
10
#include<iostream>
#include<boost/bind.hpp>
using namespace std;
using namespace boost;
int fun(int x,int y){return x+y;}
int main(){
int m=1;int n=2;
cout<<boost::bind(fun,_1,_2)(m,n)<<endl;
return 0;
}
1
g++ test.cpp -o test

cmake使用boost库

1
2
3
4
5
6
7
// CMakeLists.txt
cmake_minimum_required(VERSION 2.8.3)
project(boost_test)
find_package(Boost REQUIRED COMPONENTS system thread)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(boost_test boost_test.cpp)
target_link_libraries(boost_test ${Boost_LIBRARIES})
1
2
3
4
5
6
7
8
9
10
11
// boost_test.cpp
#include<iostream>
#include<boost/bind.hpp>
using namespace std;
using namespace boost;
int fun(int x,int y){return x+y;}
int main(){
int m=1;int n=2;
cout<<boost::bind(fun,_1,_2)(m,n)<<endl;
return 0;
}