经过重重困难,终于配置好了各种环境,. 网上说的配置boost的方法略不靠谱, 东凑西凑, 加上官网说明文档,才摸索出来.
分享一下心得给跟我一样的菜鸟们, 不要路太弯.
(1) 首先, 官网下载boost压缩包: 假设为 boost_1_61_0.tar.bz2
(2) 解压到出来, 进入压缩包所在的目录:
- tar -zxvf boost_1_61_0.tar.bz2
(3) 进入boost_1_61_0目录, 里面有一个 bootstrap.sh, 执行命令
你也可以添加prefix参数, 自定义生成的头文件和二进制库文件目录,如:
- 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中.
执行命令:
等吧, 等它执行完毕, 去看看上面说的目录里面是不是有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等部分函数已经是可以用的.编译和运行都没有问题. 我以为大功告成了, 其实并没有
写入以下测试代码:
- #include
- #include
-
- void handler1(const boost::system::error_code &ec)
- {
- std::cout << "1 second, this is function " <<__FUNCTION__ << std::endl;
- }
-
- void handler2(const boost::system::error_code &ec)
- {
- std::cout << "5 second, this is function " <<__FUNCTION__ << std::endl;
- }
- int main()
- {
- boost::asio::io_service io_service;
- boost::asio::deadline_timer timer1(io_service, boost::posix_time::seconds(2));
- timer1.async_wait(handler1);
- boost::asio::deadline_timer timer2(io_service, boost::posix_time::seconds(5));
- timer2.async_wait(handler2);
- io_service.run();
- }
点击那个锤子,编译
发现报错:
- 22:42:14 **** Incremental Build of configuration Debug for project hello ****
- make all
- Building target: hello
- Invoking: GCC C++ Linker
- g++ -o "hello" ./src/main.o
- makefile:45: recipe for target 'hello' failed
- ./src/main.o: In function `__static_initialization_and_destruction_0(int, int)':
- /usr/local/include/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'
- /usr/local/include/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
- /usr/local/include/boost/system/error_code.hpp:223: undefined reference to `boost::system::system_category()'
- ./src/main.o: In function `boost::system::error_code::error_code()':
- /usr/local/include/boost/system/error_code.hpp:322: undefined reference to `boost::system::system_category()'
- ./src/main.o: In function `boost::asio::error::get_system_category()':
- /usr/local/include/boost/asio/error.hpp:230: undefined reference to `boost::system::system_category()'
- collect2: error: ld returned 1 exit status
- make: *** [hello] Error 1
-
- 22:42:15 Build Finished (took 1s.78ms)
原来是链接(Link)的时候出问题了. asio调用了boost_system库, 这货的确在/usr/loca/lib里面, 但是在链接的时候,它找不到的.需要手动指定. 如果咱们手动用g++命令添加库是可以编译的:
- g++ main.cpp -o hello.o -lboost_system
那么我们在Eclipse里面也要配置上这个库
对项目点右键->properties->C/C++ Build->Settings, 点击选择 GCC C++ Linker-> Libraries,如图, 然后在右边的Libraries(-l)中添加库文件boost_system
点击Ok

好, 现在再点那个锤子编译, 应该没有任何问题了.
- 22:51:03 **** Incremental Build of configuration Debug for project hello ****
- make all
- Building target: hello
- Invoking: GCC C++ Linker
- g++ -o "hello" ./src/main.o -lboost_system
- Finished building target: hello
-
-
- 22:51:03 Build Finished (took 433ms)
但是! 当你点击那个虫子或者Run运行的时候, 特么又不能运行了.(如果你运行成功了, 接下来就不用看了)
提示什么什么找不到 libboost_system.so.1.61.0库的, 无法定位到库文件夹. 坑特么的爹.
咱还得配置在Ubuntu正常环境下运行boost程序的库,这样才能保证你写的程序在电脑任意地方都能运行
里面文件老多了.咱要找的是这个: ld.so.conf, 查看内容
发现里面只有一句: include /etc/ld.so.conf.d/*.conf (至少我的系统是这样的)
这里导入了/etc/ld.so.conf.d目录下所有的 .conf后缀名的文件
咱们进入/etc/ld.so.conf.d文件夹

这些文件都是一些C++库路径, 其中libc.conf 里面的内容是 : /usr/local/lib
妈蛋, 这不是有boost库所在的目录嘛? 为啥运行程序还是没找到?(没有的人,就自己加吧)
咱们需要执行以下命令:

好了, 再次运行你的程序吧,应该没问题了.
好累... 菜鸟的心啊..