Boost程序库入门学习_boost库学习

下载地址:
https://sourceforge.net/projects/boost/files/boost/1.63.0/

安装:

tar -xzvf boost_1_63_0.tar.gz
cd boost_1_63_0
//编译前配置工作
./bootstrap.sh --prefix=/usr/local
//命令b2开始真正的编译并安装boost
sudo ./b2 install --with=all

我们可以看到:头文件安装到了/usr/local/include,库文件安装到/usr/local/lib中。

Boost库大多数组件不需要编译链接,在自己的源码中直接包含头文件即可。如果要使用boost::tribool,只需要在C++源文件中添加如下include语句:

#include <boost/logic/tribool.hpp>
using namespace boost; //命名空间

文件后缀.hpp = .h + .cpp,把C++类的声明和实现放在了一个文件中。

剩下的少量库(如thread,date_time)必须编译成静态库或者动态库,并在构建时指定链接选项。

补充

这里补充一下Windows环境下如何安装,我们假定源码文件夹放在桌面,文件夹名字为code,我们需要在visual studio的工程中设置编译器搜索路径为:
在这里插入图片描述

二、Boost开发环境验证
1) 不使用编译库

编写一个简单的Boost应用程序来验证开发环境。

#include <boost/version.hpp>
#include <boost/config.hpp>
#include <iostream>
using namespace std;

int main()
{

    cout<< BOOST_VERSION<<endl; //版本号
    cout<< BOOST_LIB_VERSION<<endl;

    cout<< BOOST_PLATFORM<<endl; //操作系统
    cout<< BOOST_COMPILER<<endl;
    cout<< BOOST_STDLIB<<endl;

}

编译

g++ test.cpp -o test
./test

输出

106300
1_63
linux
GNU C++ version 5.4.0 20160609
GNU libstdc++ version 20160609

2) 使用编译库

下面代码是线程的helloworld和timer类的使用例子。

#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/timer.hpp>

using namespace std;
using namespace boost;

void hello()
{
    cout<<"hello boost"<<endl;
}

int main()
{
    timer t;
    boost::thread thrd(&hello);
    thrd.join();

    cout<<t.elapsed_max()/3600<<endl;//最大时间,单位:小时
    cout<<t.elapsed_min()<<endl; //最小统计时间,单位:秒
    cout<<t.elapsed()<<endl;//从建立对象开始,时间流失统计


    return 0;
}

由于使用了thread库,我们需要在链接时指定路径,然后测试:

g++ hello.cpp -o hello -I/usr/local/include  -L/usr/local/lib -lboosstem -lboost_thread

其中编译指令选项参考之前文章编译与链接复习,这里需要说明的是,运行后可能出现错误:
在这里插入图片描述

我们首先查看可执行文件的所链接的文件,然后查找/usr路径下是否有该文件,若有说明链接设置不当,这是因为程序运行时,会在.usr/lib和/lib等目录中查找需要的动态库文件。若找到则载入动态库文件,否则提示类似上述错误。

//方法一:
LD_LIBRARY_PATH="/usr/local/lib" ./hello

//方法二:
 export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
 sudo ldconfig

之后,运行没有错误,输出:

hello boost
2.56205e+09
1e-06
0.000381

补充(2018年11月6日):
编译含有第三方库的代码时,有时候直接将boost的环境变量添加到系统中:

//makefile的修改
$(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJCOMP) $(LIBS) -lboost_system -lboost_thread
举例:
g++ -O2 -std=c++14 -o client client.cpp structHeader.cpp -lboost_system -lpthread

//出错原因:这里没有指明路径,则需要添加环境变量
./server: error while loading shared libraries: libboost_system.so.1.63.0: 
cannot open shared object file: No such file or directory

修改或设置则可以用export指令:
export PATH=${PATH}:/usr/local/lib:/usr/local/include
echo "$PATH"
输出环境变量的值;


3) progress_timer学习

progress_timer也是一个计时器,继承自timer,会在析构时自动输出时间,省去了timer手动调用elapsed()的工作。

#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/timer.hpp>
#include <boost/progress.hpp>
using namespace std;
using namespace boost;

int main()
{  
	//测试progress_timer
    progress_timer pt;

    return 0;
}

这样,程序退出main函数作用域导致pt析构时,会自动输出流逝的时间。

缺点:精度不够,Linux下单位为s。

三、Boost组件progress_display

progress_display是一个独立的类,与timer库中其它的两个组件-timer和progress_timer没有任何关系。

#include <iostream>
#include <boost/progress.hpp>
#include <vector>
#include <fstream>
using namespace std;
using namespace boost;
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值