VS2019编译boost库

一、boost库介绍

Boost是一个广泛使用的C++库集合,用于提供各种功能和工具,包括算法、容器、迭代器、函数对象、数值处理和多线程编程等。Boost库的目标是扩展C++语言和标准库,为开发人员提供高质量、可移植和可重用的代码。

二、boot库编译

下载相应版本的boost源码包:https://www.boost.org/users/history/
解压到想放到的路径,例如:D:\Program Files\boost_1_70_0
下载压缩包后直接解压缩就行了,解压缩后的目录如下,找到其中的bootstrap.bat文件

image

使用vs2019的tools工具,打开x86_x64 Cross Tools Command Prompt for VS 2019

image

 切换到刚才boost的解压缩安装目录下(D:\Program Files\boost_1_70_0),然后执行bootstrap.bat

image

 命令行运行bootstrap.bat后,会在根目录下生产bjam.exe,b2.exe(bjam的升级版),project-config.jam,bootstrap.log四个文件。

在命令框中继续输入以下内容,开始编译:

bjam.exe install stage --toolset=msvc-14.2 architecture=x86 address-model=64 --stagedir="D:\Program Files\boost_1_70_0" link=static runtime-link=shared runtime-link=static --build-type=complete --with-system --with-thread --with-date_time --with-filesystem --with-serialization threading=multi debug release

如果要获取动态库:

bjam.exe install stage --toolset=msvc-14.2 --stagedir="D:\Program Files\boost_1_70_0" link=shared runtime-link=shared threading=multi debug release 

如果是要获取静态库:

bjam.exe install stage --toolset=msvc-14.2 --stagedir="D:\Program Files\boost_1_70_0" link=static runtime-link=static threading=multi debug release  

其中,注意修改–toolset=msvc-14.2,将14.2修改成对应的vs版本号,14.2是VS2019的版本号。

编译成功后会在D:\Program Files\boost_1_70_0下生成一个lib目录,目录里是编译好的boost的lib库

image

 

三、vs2019配置boost并使用
新建vs2019项目,并添加boost依赖,右键项目属性,在附加包含目录和附加库目录添加boost依赖。

image

 

image

 代码测试:

#include <iostream>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/filesystem.hpp>

int main() {
    // 1. 使用boost::algorithm::to_upper复制并转换字符串为大写
    std::string str = "Hello, Boost!";
    std::string strUpper = boost::algorithm::to_upper_copy(str);
    std::cout << strUpper << std::endl; // 输出: HELLO, BOOST!

    // 2. 使用boost::lexical_cast将字符串转换为数字
    std::string numStr = "123";
    int num = boost::lexical_cast<int>(numStr);
    std::cout << num << std::endl; // 输出: 123

    return 0;
}

注意:如果在VS调用时,出现错误“LNK1104 无法打开文件“libboost_filesystem-vc142-mt-gd-x32-1_70.lib”

下面是结局的步骤。先说一下报错的原因,这个文件名是libboost_filesystem-vc142-mt-gd-x32-1_70.lib,第一个表示的是vc142表示使用的是vc142进行编辑的,第二个mt表示的是使用threading=muti多线程编译出来的;第三个gd表示的是debug版本,与其对应的是release版本。第四个x64表示的是64位系统,如果是x32表示的就是32位系统,还有1_70表示的就是boost版本是1_70_0。如果无法打开问价"libboost_filesystem-vc142-mt-gd-x32-1_70.lib",将程序改为x64再次编译即可。

所以这个地方注意两点:

(1).下载的boost版本要对应

(2).使用vs编译的版本要对应。

问题:
问题1:提示打不开libboost_filesystem-vc142-mt-gd-x64-1_89.lib ,看下目录下有个boost_filesystem-vc142-mt-gd-x64-1_89.lib把他改成对应的名称就可以了

 

posted @ 2025-11-18 09:00  家煜宝宝  阅读(3)  评论(0)    收藏  举报