Visual Studio 中的 GCC 风格弱链接?

2021-12-18 00:00:00 visual-studio gcc c c++ weak-linking

GCC 有能力通过 __attribute__((weak)) 弱化符号链接.我想在用户可以在其应用程序中覆盖的静态库中使用弱符号.GCC 风格的弱符号可以让我这样做,但我不知道是否可以用 Visual Studio 来完成.

GCC has the ability to make a symbol link weakly via __attribute__((weak)). I want to use the a weak symbol in a static library that users can override in their application. A GCC style weak symbol would let me do that, but I don't know if it can be done with visual studio.

Visual Studio 是否提供类似的功能?

Does Visual Studio offer a similar feature?

推荐答案

MSVC++ 有 __declspec(selectany) 它涵盖了弱符号的部分功能:它允许你定义多个相同的符号与外部链接,指示编译器选择几个可用的任何一个.但是,我认为 MSVC++ 没有任何东西可以涵盖弱符号功能的另一部分:在库中提供可替换"定义的可能性.

MSVC++ has __declspec(selectany) which covers part of the functionality of weak symbols: it allows you to define multiple identical symbols with external linkage, directing the compiler to choose any one of several available. However, I don't think MSVC++ has anything that would cover the other part of weak symbol functionality: the possibility to provide "replaceable" definitions in a library.

顺便说一句,这让人想知道对标准可替换 ::operator new::operator delete 函数的支持在 MSVC++ 中是如何工作的.

This, BTW, makes one wonder how the support for standard replaceable ::operator new and ::operator delete functions works in MSVC++.

相关文章