Ubuntu安装boost和boost threadpool

转自:http://blog.csdn.net/steve1018/article/details/7989491

最近在写一个c++的linux app,要用到线程池,想着很多线程的很多的代码,就像借用一下。我选择了基于boost的boost threadpool。

安装boost:我选择的是boost1.51版本。

1.下载boost源码,http://sourceforge.net/projects/boost/files/boost/1.51.0/

2.我下载的zip,下载完毕,解压zip,获得boost源码。

3.源码的根目录:

  ./bootstrap.sh    (默认安装到/usr/local/include和/usr/local/lib)

   sudo ./b2 install

4.如果安装后,你看到有fail的情况,请安装二个软件包:

  "安装过程中提示: patchlevel.h:没有那个文件或目录",解决:sudo apt-get install python-dev

  "bzlib.h:没有那个文件或目录",解决:sudo apt-get install libbz2-dev

5.下载boost threadpool:

    http://prdownloads.sourceforge.net/threadpool/threadpool-0_2_5-src.zip?download

6.threadpool解压后,搞个源码测试一下:

  源码的内容为:

  1. #include <iostream>  
  2. #include "threadpool.hpp"  
  3.   
  4. using namespace std;  
  5. using boost::threadpool::pool;  
  6.   
  7. // Some example tasks  
  8. void first_task()  
  9. {  
  10.     cout << "first task is running\n" ;  
  11. }  
  12.   
  13. void second_task()  
  14. {  
  15.     cout << "second task is running\n" ;  
  16. }  
  17.   
  18. void task_with_parameter(int value)  
  19. {  
  20.     cout << "task_with_parameter(" << value << ")\n";  
  21. }  
  22.   
  23. int main(int argc,char *argv[])  
  24. {  
  25.     // Create fifo thread pool container with two threads.  
  26.     pool tp(2);  
  27.   
  28.     // Add some tasks to the pool.  
  29.     tp.schedule(&first_task);  
  30.     tp.schedule(&second_task);   
  31.     tp.schedule(boost::bind(task_with_parameter, 4));  
  32.   
  33.     // Wait until all tasks are finished.  
  34.     tp.wait();  
  35.   
  36.     // Now all tasks are finished!      
  37.     return(0);  
  38. }  
保存为test.cc。

7.将threadpool-0_2_5-src/threadpool/boost下的一个文件夹和头文件拷贝到test.cc目录。

8.然后就可以编译:

   g++ test.cc -lboost_thread

   发现TIME_UTC没有定义。这个问题来源于,boot1.51这个版本,将"TIME_UTC"改为了"TIME_UTC_",一个脑惨的改动。去threadpool里面报错的文件,把这个宏定义改成后者就好了。

9.编译通过,生成a.out可执行文件。

10.运行a.out,会报错,error while loading shared libraries: libboost_thread.so.1.51.0: cannot open shared object file: No such file or directory

11.加上一句:

sudo ldconfig /usr/local/lib

12.再执行程序,OK。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值