本文主要是介绍【vcpkg】boost编译链接总结(探究vcpkg编译boost偷偷做了哪些修改),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 导读
- 开发环境
- 编译过程
- 安装vs2019
- 安装vcpkg
- 安装指定版本的boost 1.77
- boost库实战
- 步骤
- 编写测试代码-引入lib示例
- 问题:链接错误
- 解决思路和方案
- 文章小结
- vcpkg install boost:x86-windows-static-rel-v142
- 其他命令
- 参考资料
导读
开发环境
版本号 | 描述 | |
---|---|---|
操作系统 | Win11-21H2 | 22000.588 |
VS2019 | ||
工作目录 | J:_ALL\CODE\vcpkg | |
POCO | 1.77.0#2 |
编译过程
安装vs2019
略
安装vcpkg
打开powershell。
切换到目录cd J:\_ALL\CODE\vcpkg
中。
下载代码
可以使用git命令获取官方代码仓库
git clone https://github.com/microsoft/vcpkg
觉得速度慢的,可以使用镜像下载,网站hub.fastgit.xyz
的速度还是不错的。
git clone https://hub.fastgit.xyz/microsoft/vcpkg.git
运行
bootstrap-vcpkg.bat
,初始化vcpkg。
安装指定版本的boost 1.77
# 查看boost版本
.\vcpkg x-history boost# 使用git回退到指定版本
git reset --hard 1f5e8e4e9b5101a04634e83ae20c147042bfafd2# 编译boost(32位、静态库)
.\vcpkg.exe install boost:x86-windows-static
编译结果将保存到x86-windows-static
目录中,如下图所示:
我们发现编译出来的lib标记为了vc140,拿regex为例,我们生成的文件为boost_regex-vc140-mt-gd.lib
,而通过boost生成的文件名为libboost_regex-vc142-mt-sgd-x32-1_77.lib
。虽然名字不一样,但是内容是一样的(https://github.com/iso8859/vcpkg-boost 项目给了解决名字不一样的方案)。
boost库实战
步骤
- 生成一个MFC工程,指定“在静态库中使用 MFC”。
- 设置引入目录
/I"J:\_ALL\CODE\vcpkg\vcpkg\installed\x86-windows-static\include"
:
- 引入头文件
#include <boost/regex.hpp>
- 编写测试代码一
int 正则测试()
{const char* url = "https://steamcommunity.com/profiles/76561199166454776/friends";boost::regex reg("https://steamcommunity\\.com/profiles/\\d+/friends.*");bool r = boost::regex_match(url, reg);printf("%d", r);r = boost::regex_match("https://steamcommunity.com/xxxprofiles/76561199166454776/friends/add", reg);printf("%d", r);boost::regex reg2(".*visa\\.cn.*");r = boost::regex_match("https://help.visa.cn/zh-cn/wizard/HelpWithGame/?appid=1097150", reg2);printf("[reg2] %d", r);return 0;
}
ps: 目前代码中不需要引入库文件就可以运行,boost大量使用模板,只需要头文件就可以生成使用了。
编写测试代码-引入lib示例
上面的《测试代码》中,只需要头文件就可以实现编译了,下面我们通过编码转换
示例,引出vcpkg编译问题。
示例代码二:
{std::string sSource = "BIG-5字符串";std::wstring ws = boost::locale::conv::to_utf<wchar_t>(sSource, "BIG5");std::string ss = boost::locale::conv::to_utf<char>(sSource, "BIG5");}
问题:链接错误
众所周知,boost可以通过头文件将对应的lib库引入到项目中,其原理参考文章《Boost库解密——自动链接库(auto_link) https://blog.csdn.net/godmaycry/article/details/78673001》,但是,当我们引入示例代码二
的时候,出现了下面链接错误:
也就是说,未找到链接的库,那么vcpkg为什么没有自动引入链接库呢?
解决思路和方案
首先,我们需要明白,VS链接必然要引入lib库,如果不在VS配置中设置,就需要在代码中引入类似
#pragma comment(lib, "***.lib")
这样的 代码,显然boost使用的是后者。
然后,我们将vcpkg生成的头文件和通过boost官方教程提供的b2.exe方案生成的头文件进行比较,我们会发现
config\user.hpp
文件中,增加了宏定义BOOST_ALL_NO_LIB
。
该宏的解释如下图所示,即,不自动引入lib库:
最后,既然vcpkg不自动引入库,那么我们自己引入不就可以了。拿debug环境来说,我们在代码中增加
#pragma comment(lib, "boost_locale-vc140-mt-gd.lib")
,库目录设置到项目中/LIBPATH:"J:\_ALL\CODE\vcpkg\vcpkg\installed\x86-windows-static\debug\lib"
。再次编译就可以编译通过了。
ps: 附上引入头文件和库目录的代码,以后就可以直接通过vcpkg生成的库进行编码了,注意根据
_DEBUG
区分引入不同的库。
#include <boost/filesystem.hpp>
#include <boost/locale.hpp>
#include <boost/regex.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread.hpp>
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/foreach.hpp>
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/path.hpp"
#include <boost/regex.hpp>
#include <boost/date_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/asio/streambuf.hpp>
#include <boost/asio/ip/host_name.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/random/random_device.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/serialization/singleton.hpp>
#include <boost/property_tree/json_parser.hpp>#ifdef _DEBUG
#pragma comment(lib, "boost_atomic-vc140-mt-gd.lib")
#pragma comment(lib, "boost_chrono-vc140-mt-gd.lib")
#pragma comment(lib, "boost_container-vc140-mt-gd.lib")
#pragma comment(lib, "boost_context-vc140-mt-gd.lib")
#pragma comment(lib, "boost_contract-vc140-mt-gd.lib")
#pragma comment(lib, "boost_coroutine-vc140-mt-gd.lib")
#pragma comment(lib, "boost_date_time-vc140-mt-gd.lib")
#pragma comment(lib, "boost_exception-vc140-mt-gd.lib")
#pragma comment(lib, "boost_fiber-vc140-mt-gd.lib")
#pragma comment(lib, "boost_filesystem-vc140-mt-gd.lib")
#pragma comment(lib, "boost_graph-vc140-mt-gd.lib")
#pragma comment(lib, "boost_iostreams-vc140-mt-gd.lib")
#pragma comment(lib, "boost_json-vc140-mt-gd.lib")
#pragma comment(lib, "boost_locale-vc140-mt-gd.lib")
#pragma comment(lib, "boost_log-vc140-mt-gd.lib")
#pragma comment(lib, "boost_log_setup-vc140-mt-gd.lib")
#pragma comment(lib, "boost_math_c99-vc140-mt-gd.lib")
#pragma comment(lib, "boost_math_c99f-vc140-mt-gd.lib")
#pragma comment(lib, "boost_math_c99l-vc140-mt-gd.lib")
#pragma comment(lib, "boost_math_tr1-vc140-mt-gd.lib")
#pragma comment(lib, "boost_math_tr1f-vc140-mt-gd.lib")
#pragma comment(lib, "boost_math_tr1l-vc140-mt-gd.lib")
#pragma comment(lib, "boost_nowide-vc140-mt-gd.lib")
#pragma comment(lib, "boost_program_options-vc140-mt-gd.lib")
#pragma comment(lib, "boost_python310-vc140-mt-gd.lib")
#pragma comment(lib, "boost_random-vc140-mt-gd.lib")
#pragma comment(lib, "boost_regex-vc140-mt-gd.lib")
#pragma comment(lib, "boost_serialization-vc140-mt-gd.lib")
#pragma comment(lib, "boost_stacktrace_noop-vc140-mt-gd.lib")
#pragma comment(lib, "boost_stacktrace_windbg-vc140-mt-gd.lib")
#pragma comment(lib, "boost_stacktrace_windbg_cached-vc140-mt-gd.lib")
#pragma comment(lib, "boost_system-vc140-mt-gd.lib")
#pragma comment(lib, "boost_thread-vc140-mt-gd.lib")
#pragma comment(lib, "boost_timer-vc140-mt-gd.lib")
#pragma comment(lib, "boost_type_erasure-vc140-mt-gd.lib")
#pragma comment(lib, "boost_unit_test_framework-vc140-mt-gd.lib")
#pragma comment(lib, "boost_wave-vc140-mt-gd.lib")
#pragma comment(lib, "boost_wserialization-vc140-mt-gd.lib")
#else
#pragma comment(lib, "boost_atomic-vc140-mt.lib")
#pragma comment(lib, "boost_chrono-vc140-mt.lib")
#pragma comment(lib, "boost_container-vc140-mt.lib")
#pragma comment(lib, "boost_context-vc140-mt.lib")
#pragma comment(lib, "boost_contract-vc140-mt.lib")
#pragma comment(lib, "boost_coroutine-vc140-mt.lib")
#pragma comment(lib, "boost_date_time-vc140-mt.lib")
#pragma comment(lib, "boost_exception-vc140-mt.lib")
#pragma comment(lib, "boost_fiber-vc140-mt.lib")
#pragma comment(lib, "boost_filesystem-vc140-mt.lib")
#pragma comment(lib, "boost_graph-vc140-mt.lib")
#pragma comment(lib, "boost_iostreams-vc140-mt.lib")
#pragma comment(lib, "boost_json-vc140-mt.lib")
#pragma comment(lib, "boost_locale-vc140-mt.lib")
#pragma comment(lib, "boost_log-vc140-mt.lib")
#pragma comment(lib, "boost_log_setup-vc140-mt.lib")
#pragma comment(lib, "boost_math_c99-vc140-mt.lib")
#pragma comment(lib, "boost_math_c99f-vc140-mt.lib")
#pragma comment(lib, "boost_math_c99l-vc140-mt.lib")
#pragma comment(lib, "boost_math_tr1-vc140-mt.lib")
#pragma comment(lib, "boost_math_tr1f-vc140-mt.lib")
#pragma comment(lib, "boost_math_tr1l-vc140-mt.lib")
#pragma comment(lib, "boost_nowide-vc140-mt.lib")
#pragma comment(lib, "boost_program_options-vc140-mt.lib")
#pragma comment(lib, "boost_python310-vc140-mt.lib")
#pragma comment(lib, "boost_random-vc140-mt.lib")
#pragma comment(lib, "boost_regex-vc140-mt.lib")
#pragma comment(lib, "boost_serialization-vc140-mt.lib")
#pragma comment(lib, "boost_stacktrace_noop-vc140-mt.lib")
#pragma comment(lib, "boost_stacktrace_windbg-vc140-mt.lib")
#pragma comment(lib, "boost_stacktrace_windbg_cached-vc140-mt.lib")
#pragma comment(lib, "boost_system-vc140-mt.lib")
#pragma comment(lib, "boost_thread-vc140-mt.lib")
#pragma comment(lib, "boost_timer-vc140-mt.lib")
#pragma comment(lib, "boost_type_erasure-vc140-mt.lib")
#pragma comment(lib, "boost_unit_test_framework-vc140-mt.lib")
#pragma comment(lib, "boost_wave-vc140-mt.lib")
#pragma comment(lib, "boost_wserialization-vc140-mt.lib")
#endif
ps2: 引入链接库方案二,通过宏
MY_BOOST_LIB_TYPE
区分debug和release下不同的库,从而减少代码量。
#ifdef _DEBUG
#define MY_BOOST_LIB_TYPE "-mt-gd"
#else
#define MY_BOOST_LIB_TYPE "-mt"
#endif#pragma comment(lib, "boost_atomic-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_chrono-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_container-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_context-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_contract-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_coroutine-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_date_time-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_exception-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_fiber-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_filesystem-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_graph-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_iostreams-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_json-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_locale-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_log-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_log_setup-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_math_c99-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_math_c99f-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_math_c99l-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_math_tr1-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_math_tr1f-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_math_tr1l-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_nowide-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_program_options-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_python310-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_random-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_regex-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_serialization-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_stacktrace_noop-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_stacktrace_windbg-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_stacktrace_windbg_cached-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_system-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_thread-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_timer-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_type_erasure-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_unit_test_framework-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_wave-vc140" MY_BOOST_LIB_TYPE ".lib")
#pragma comment(lib, "boost_wserialization-vc140" MY_BOOST_LIB_TYPE ".lib")
文章小结
vcpkg install boost:x86-windows-static-rel-v142
不支持指定vs版本,默认使用最高版本,其实在目录J:\_ALL\CODE\vcpkg\vcpkg\triplets\community
中拷贝一份文件x86-windows-static.cmake
为x86-windows-static-rel-v142.cmake
,增加如下内容即可:
set(VCPKG_VISUAL_STUDIO_PATH "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Professional")
set(VCPKG_PLATFORM_TOOLSET v142)
其他命令
export命令
导出指定安装包,可以指定安装包的导出方式(如raw、nuget、zip、7zip等等),例如以zip压缩包导出gdal安装包可以运行以下命令:
vcpkg export gdal --zip
upgrade命令
如果希望直接重建所有可升级的安装包,可以用以下方式运行命令vcpkg upgrade --no-dry-run
。
integrate命令
将vcpkg构建的安装包与Vistual Stuido集成
参考资料
- [C++ Boost库使用 系列教程] https://tangxing.blog.csdn.net/article/details/116393985
- https://gitee.com/ineo6/hosts
- 2022目前三种有效加速国内Github https://juejin.cn/post/7068996624910909471
- vcpkg快速入门手册_孤独浮云-程序员信息网_vcpkg https://www.i4k.xyz/article/gudufuyun/113787953#31_Boost_484
- https://github.com/iso8859/vcpkg-boost
- qq群:夜猫逐梦技术交流裙/953949723
这篇关于【vcpkg】boost编译链接总结(探究vcpkg编译boost偷偷做了哪些修改)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!