Linux下(Ubuntu)如何在eclipse中使用boost库

经过重重困难,终于配置好了各种环境,. 网上说的配置boost的方法略不靠谱, 东凑西凑, 加上官网说明文档,才摸索出来.

分享一下心得给跟我一样的菜鸟们, 不要路太弯.

(1)  首先, 官网下载boost压缩包: 假设为 boost_1_61_0.tar.bz2


(2) 解压到出来, 进入压缩包所在的目录:

[html] view plain copy
  1. tar -zxvf boost_1_61_0.tar.bz2  

(3) 进入boost_1_61_0目录, 里面有一个 bootstrap.sh, 执行命令
[html] view plain copy
  1. sudo ./bootstrap.sh  
你也可以添加prefix参数, 自定义生成的头文件和二进制库文件目录,如:
[html] view plain copy
  1. sudo ./booststrap.sh --prefix /usr/local/lib/boost  
则生成的头文件在/usr/local/lib/boost/include中,  二进制库文件在/usr/local/lib/boost/lib中

但是不建议自己定义目录, 不然后面用的时候还得指定库目录, 麻烦

 sudo ./boostrap.sh 默认头文件在/usr/local/include中; 二进制库文件在/usr/local/lib中.

执行命令:

[html] view plain copy
  1. sudo ./b2 install  

等吧, 等它执行完毕, 去看看上面说的目录里面是不是有boost的东西. 如查看/usr/local/include/boost中有没有boost相关头文件

(4) 在eclipse c++中使用它

如果没安装eclipse,可以参看

http://blog.csdn.net/moollun/article/details/51812071


新建项目-  C++项目  ,  选择 Linux GCC的空项目

点击finish

默认情况下, 项目已经自动包含了/usr/local/include库路径

新建src文件夹,然后在里面新建main.cpp

这时,咱们调用boost::bind等部分函数已经是可以用的.编译和运行都没有问题. 我以为大功告成了, 其实并没有

写入以下测试代码:

  1. #include <boost/asio.hpp>  
  2. #include <iostream>  
  3.   
  4. void handler1(const boost::system::error_code &ec)  
  5. {  
  6.   std::cout << "1 second, this is function " <<__FUNCTION__ << std::endl;  
  7. }  
  8.   
  9. void handler2(const boost::system::error_code &ec)  
  10. {  
  11.   std::cout << "5 second, this is function " <<__FUNCTION__ << std::endl;  
  12. }  
  13. int main()  
  14. {  
  15.   boost::asio::io_service io_service;  
  16.   boost::asio::deadline_timer timer1(io_service, boost::posix_time::seconds(2));  
  17.   timer1.async_wait(handler1);  
  18.   boost::asio::deadline_timer timer2(io_service, boost::posix_time::seconds(5));  
  19.   timer2.async_wait(handler2);  
  20.   io_service.run();  
  21. }  

点击那个锤子,编译

发现报错:

[html] view plain copy
  1. 22:42:14 **** Incremental Build of configuration Debug for project hello ****  
  2. make all   
  3. Building target: hello  
  4. Invoking: GCC C++ Linker  
  5. g++  -o "hello"  ./src/main.o     
  6. makefile:45: recipe for target 'hello' failed  
  7. ./src/main.o: In function `__static_initialization_and_destruction_0(int, int)':  
  8. /usr/local/include/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'  
  9. /usr/local/include/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'  
  10. /usr/local/include/boost/system/error_code.hpp:223: undefined reference to `boost::system::system_category()'  
  11. ./src/main.o: In function `boost::system::error_code::error_code()':  
  12. /usr/local/include/boost/system/error_code.hpp:322: undefined reference to `boost::system::system_category()'  
  13. ./src/main.o: In function `boost::asio::error::get_system_category()':  
  14. /usr/local/include/boost/asio/error.hpp:230: undefined reference to `boost::system::system_category()'  
  15. collect2: error: ld returned 1 exit status  
  16. make: *** [hello] Error 1  
  17.   
  18. 22:42:15 Build Finished (took 1s.78ms)  

原来是链接(Link)的时候出问题了. asio调用了boost_system库, 这货的确在/usr/loca/lib里面, 但是在链接的时候,它找不到的.需要手动指定. 如果咱们手动用g++命令添加库是可以编译的:
[html] view plain copy
  1. g++ main.cpp -o hello.o -lboost_system  

那么我们在Eclipse里面也要配置上这个库

对项目点右键->properties->C/C++ Build->Settings,  点击选择 GCC C++ Linker-> Libraries,如图, 然后在右边的Libraries(-l)中添加库文件boost_system

点击Ok




好, 现在再点那个锤子编译, 应该没有任何问题了.

[html] view plain copy
  1. 22:51:03 **** Incremental Build of configuration Debug for project hello ****  
  2. make all   
  3. Building target: hello  
  4. Invoking: GCC C++ Linker  
  5. g++  -o "hello"  ./src/main.o   -lboost_system  
  6. Finished building target: hello  
  7.    
  8.   
  9. 22:51:03 Build Finished (took 433ms)  

但是! 当你点击那个虫子或者Run运行的时候, 特么又不能运行了.(如果你运行成功了, 接下来就不用看了)

提示什么什么找不到 libboost_system.so.1.61.0库的, 无法定位到库文件夹. 坑特么的爹.

咱还得配置在Ubuntu正常环境下运行boost程序的库,这样才能保证你写的程序在电脑任意地方都能运行

[html] view plain copy
  1. cd /etc  
里面文件老多了.咱要找的是这个: ld.so.conf, 查看内容
[html] view plain copy
  1. cat ld.so.conf  

发现里面只有一句: include /etc/ld.so.conf.d/*.conf (至少我的系统是这样的)

这里导入了/etc/ld.so.conf.d目录下所有的 .conf后缀名的文件

咱们进入/etc/ld.so.conf.d文件夹

[html] view plain copy
  1. cd  /etc/ld.so.conf.d  


这些文件都是一些C++库路径, 其中libc.conf 里面的内容是 : /usr/local/lib

妈蛋, 这不是有boost库所在的目录嘛? 为啥运行程序还是没找到?(没有的人,就自己加吧)

咱们需要执行以下命令:

[html] view plain copy
  1. sudo ldconfig  


好了, 再次运行你的程序吧,应该没问题了.


好累... 菜鸟的心啊..
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值