windows下boost库的安装和使用
什么是boost库
简单来说,Boost库是为C++语言标准库提供扩展的一些C++程序库的总称,由Boost社区组织开发、维护。Boost库可以与C++标准库完美共同工作,并且为其提供扩展功能。
C++11, C++14很多特性都是从boost库来的,很多 boost 库采用 STL 的风格,需要 STL 的基础。建议先用好 STL 再去看 boost。
boost 更准确的说,并不是一个库,而是一个库集合。不用每个都去看。应该先快速翻阅一些简介,只需要了解一下有什么库,每个库大概是做什么东西的。之后碰到问题再细看具体的库用法。
更详细的介绍可以看以下链接:
Boost库 - 百度百科
学习C++有没有必要学习boost库? - 知乎
boost库的下载和安装
下载
网上有的教程挥手一指就让到boost官网下载,本来我想着从官网下载挺好,然而官网点击下载之后,居然又给我跳到什么JFrog网站,又要安装JFrog相关的配套环境才能从JFrog上下载我需要的boost库,这啥呀想想就头大,这么能绕,不干。
我比较推荐以下这两个链接,直接下载压缩包,之后再按照相关教程配置就可以了,方便多了。
我用的是第二个。
安装
我下载的是 boost_1_77_0 。
解压文件,然后打开到boost库的根目录下:
双击bootstrap.bat文件,生成b2.exe,然后打开b2.exe,在cmd中输入以下命令:
bjam --toolset=msvc --build-type=complete stage ,然后回车
等待程序编译完成,大约要十几分钟到两个小时左右,会在boost根目录下生成bin.v2和stage两个文件夹,其中bin.v2下是生成的中间文件,大小在2.2G左右,可以直接删除。stage下才是生成的dll和lib文件。
VS项目属性配置(VS2019)
项目->属性
在弹出的属性对话框中:
配置属性->VC++目录:"包含目录": boost的根目录,例: D:\Visual Stdio 2013\lipeng\boost\boost_1_58_0
"库目录": stage下的链接库目录,例:D:\Visual Stdio 2013\lipeng\boost\boost_1_58_0\stage\lib
配置属性->链接器->常规:"附加库目录":同上面的"库目录",例:D:\Visual Stdio 2013\lipeng\boost\boost_1_58_0\stage\lib
至此环境就配置好了,下面测试一下:
点击查看代码
#include <cstdlib>
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <boost/timer.hpp>
#include <boost/progress.hpp>
#include <libs/date_time/src/gregorian/greg_names.hpp>
#include <libs/date_time/src/gregorian/greg_month.cpp>
#include <libs/date_time/src/gregorian/gregorian_types.cpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost;
int main()
{
boost::timer t;
boost::progress_display pd(100);
for (int i = 0; i < 100; ++i) //进度条
{
++pd;
}
boost::gregorian::date dt(2009, 12, 8); //date_time 库
assert(dt.year() == 2009);
assert(dt.day() == 8);
boost::gregorian::date::ymd_type ymd = dt.year_month_day();
std::cout<<"\n"<<ymd.year<<"/"<<ymd.month<<"/"<<ymd.day<<" the day is "
<<dt.day_of_year() <<" days of this year"<< std::endl;
std::cout << boost::gregorian::to_iso_extended_string(dt) << std::endl; //转换为其他格式
std::cout << boost::gregorian::to_iso_string(dt) << std::endl;
std::cout << boost::gregorian::to_simple_string(dt) << std::endl<<std::endl;
//对数组排序操作
std::vector<int> test_vc(100);
std::vector<int>::iterator beg_it = test_vc.begin();
std::vector<int>::iterator end_it = test_vc.end();
std::srand(std::time(NULL));
std::for_each(beg_it, end_it, [](int& n){n = rand(); });
std::copy(beg_it, end_it, std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl << std::endl;
std::sort(beg_it, end_it, std::greater<int>());
std::copy(beg_it, end_it, std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl<<std::endl;
boost::posix_time::ptime pt(boost::gregorian::date(2005, 2, 6));
std::cout << t.elapsed() << "s" << std::endl; //程序运行时间
system("pause");
return 0;
}
注:在原帖中的代码出现了小问题,在我的vs版本编译时报错:C2084 函数“const char *boost::date_time::nth_as_str(int)”已有主体 ,错误来源于date_generators.cpp文件。
解决方法:直接删去如下语句即可
#include <libs/date_time/src/gregorian/date_generators.cpp>
猜测是此boost库中的内容已经被添加到新的C++标准库中,所以此文件中函数定义与C++原本的标准库中函数定义冲突,所以直接删去后程序可以正常运行。
程序正确运行:
参考文章:windows下boost库的基本使用方法 ,有删改。
本文来自博客园,作者:lylygoing,转载请注明原文链接:https://www.cnblogs.com/lylygoing/p/BoostDownload.html
posted on 2021-11-08 18:29 lylygoing 阅读(10536) 评论(5) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!