C++ 链接器如何知道哪个 .lib 包含哪些函数?

How does the C++ linker know which .lib contains which functions?(C++ 链接器如何知道哪个 .lib 包含哪些函数?)

本文介绍了C++ 链接器如何知道哪个 .lib 包含哪些函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如在 Boost 中.我将 MSVC++2010 中的 include 目录设置为 Boost 根目录,并在我的源代码中有一个 #include <boost/regex.hpp>.我将一个 library 目录设置为 booststagelib 但里面有数百个文件——每个 Boost 库有几个文件,这些用于 boost::regex:

<前>libboost_regex-vc100-s-1_46.liblibboost_regex-vc100-mt-gd-1_46.liblibboost_regex-vc100-mt-1_46.liblibboost_regex-vc100-mt-s-1_46.liblibboost_regex-vc100-mt-s.liblibboost_regex-vc100-s.liblibboost_regex-vc100-mt.liblibboost_regex-vc100-mt-gd.lib

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 包含哪些函数?

基础教程推荐