How does the C++ linker know which .lib contains which functions?(C++ 链接器如何知道哪个 .lib 包含哪些函数?)
问题描述
例如在 Boost 中.我将 MSVC++2010 中的 include 目录设置为 Boost 根目录,并在我的源代码中有一个 #include <boost/regex.hpp>
.我将一个 library 目录设置为 booststagelib
但里面有数百个文件——每个 Boost 库有几个文件,这些用于 boost::regex:
MSVC 如何知道所有 lib 文件中哪个是正确的?如果它扫描所有这些函数签名是否正确,这是否意味着从两个不同的源(彼此没有链接)编译的 2 个不同的 lib 恰好定义了具有相同名称和参数的函数不能在一个 lib 文件夹中?>
它如何知道在所有这些不同的正则表达式 .lib 中哪个是正确的?然后,每个文件名中带有 1_46
的文件似乎都与相应的文件相同,没有,我可以安全地删除两者之一吗?
boost 库使用一些黑魔法从头文件和编译器选项中选择要链接的库.我真的不知道所有的血腥细节,但您可以查看 boost/config/auto_link.hpp 标头以获取更多信息.
特别是,这似乎是拼图的一个重要部分:
# pragma comment(lib, BOOST_LIB_PREFIX BOOST_STRINGIZE(BOOST_LIB_NAME) "-" BOOST_LIB_TOOLSET BOOST_LIB_THREAD_OPT BOOST_LIB_RT_OPT "-" BOOST_LIB_VERSION ".lib")
For example in Boost. I set an include directory in MSVC++2010 to the Boost root directory and have an #include <boost/regex.hpp>
in my source code. I set a library directory to booststagelib
but there are hundreds of files in there – several for each Boost library and these for boost::regex:
libboost_regex-vc100-s-1_46.lib libboost_regex-vc100-mt-gd-1_46.lib libboost_regex-vc100-mt-1_46.lib libboost_regex-vc100-mt-s-1_46.lib libboost_regex-vc100-mt-s.lib libboost_regex-vc100-s.lib libboost_regex-vc100-mt.lib libboost_regex-vc100-mt-gd.lib
How does MSVC know which of all lib files is the right one? If it scans all of them for the right function signatures, does that mean that 2 different lib's compiled from two different sources (not linked to each other) which happen to define functions with identical names and parameters cannot be in one lib folder?
And how does it know which is right among all those different regex .lib's? And then, each file with 1_46
in its filename seems to be identical to the respective file without, can I safely delete one of the two?
The boost libraries use some dark magic to select the libraries to link from the headers and compiler options. I don't really know all the gory details, but you can take a look at the boost/config/auto_link.hpp header for extra information.
In particular, this seems to be an important piece of the puzzle:
# pragma comment(lib, BOOST_LIB_PREFIX BOOST_STRINGIZE(BOOST_LIB_NAME) "-" BOOST_LIB_TOOLSET BOOST_LIB_THREAD_OPT BOOST_LIB_RT_OPT "-" BOOST_LIB_VERSION ".lib")
这篇关于C++ 链接器如何知道哪个 .lib 包含哪些函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ 链接器如何知道哪个 .lib 包含哪些函数?
基础教程推荐
- Windows Media Foundation 录制音频 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为什么语句不能出现在命名空间范围内? 2021-01-01