linux下安装boost

原文:https://blog.csdn.net/a379039233/article/details/80493167

linux下安装boost

1、下载解压boost包
2、运行boost目录下启动安装脚本:
./bootstrap.sh --with-libraries=all --with-toolset=gcc
说明:--with-libraries指定需要编译的库,all表示编译所有boost库;--with-toolset指定编译器
没有报错则安装正常
3、编译
./b2 toolset=gcc
4、安装
./b2 install --prefix=/usr
说明:--prefix指定boost安装路径,/usr表示boost安装到系统头文件和库路径,可以省略环境变量

5、最后运行ldconfig命令

6、测试

  1. #include <boost/thread/thread.hpp>

  2. #include <boost/date_time/posix_time/posix_time.hpp>

  3. #include <iostream>

  4. #include <cstdlib>

  5.  
  6. using namespace std;

  7. //using namespace boost; //为什么此处声明了会导致system编译报错

  8.  
  9. bool isRuning = true;

  10. int cnt = 0;

  11. boost::mutex mtx_cnt;

  12.  
  13.  
  14. void thread_proc1()

  15. {

  16. while( isRuning )

  17. {

  18. mtx_cnt.lock();

  19. std::cout << "thread1 cnt: " << ++cnt << std::endl;

  20. mtx_cnt.unlock();

  21. boost::this_thread::sleep( boost::posix_time::milliseconds( 500 ) );

  22. //sleep( 2 );

  23. }

  24. std::cout << "thread1 end" << std::endl;

  25. }

  26.  
  27.  
  28. void thread_proc2()

  29. {

  30. while( isRuning )

  31. {

  32. mtx_cnt.lock();

  33. std::cout << "thread2 cnt: " << ++cnt << std::endl;

  34. mtx_cnt.unlock();

  35. boost::this_thread::sleep( boost::posix_time::milliseconds( 500 ) );

  36. //sleep( 2 );

  37. }

  38. std::cout << "thread2 end" << std::endl;

  39. }

  40. int main( int argc, char* argv[] )

  41. {

  42. std::cout << "start" << std::endl;

  43. boost::thread thread1( &thread_proc1 );

  44. boost::thread thread2( &thread_proc2 );

  45.  
  46. system( "read" );

  47.  
  48. isRuning = false;

  49. thread1.join();

  50. thread2.join();

  51.  
  52. std::cout << "exit" << std::endl;

  53. return 0;

  54. }

编译:g++ main.cpp -o main -lboost_thread

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值