visual-studio Visual Studio 中的 GCC 风格弱链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2290587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GCC style weak linking in Visual Studio?
提问by deft_code
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.
GCC 有能力通过__attribute__((weak)). 我想在用户可以在其应用程序中覆盖的静态库中使用弱符号。GCC 样式的弱符号可以让我这样做,但我不知道是否可以使用 Visual Studio 来完成。
Does Visual Studio offer a similar feature?
Visual Studio 是否提供类似的功能?
采纳答案by AnT
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.
MSVC++ 具有__declspec(selectany)其中涵盖了弱符号的部分功能:它允许您定义多个具有外部链接的相同符号,指示编译器选择多个可用符号中的任何一个。但是,我认为 MSVC++ 没有任何东西可以涵盖弱符号功能的另一部分:在库中提供“可替换”定义的可能性。
This, BTW, makes one wonder how the support for standard replaceable ::operator newand ::operator deletefunctions works in MSVC++.
顺便说一句,这让人想知道对标准可替换::operator new和::operator delete函数的支持在 MSVC++ 中是如何工作的。
回答by Ringo
You can do it, here is an example in C:
你可以做到,这里有一个 C 语言的例子:
/*
* pWeakValue MUST be an extern const variable, which will be aliased to
* pDefaultWeakValue if no real user definition is present, thanks to the
* alternatename directive.
*/
extern const char * pWeakValue;
extern const char * pDefaultWeakValue = NULL;
#pragma comment(linker, "/alternatename:_pWeakValue=_pDefaultWeakValue")
回答by Michael Burr
MSVC used to behave such that if a symbol is defined in a .obj file and a .lib it would use the one on the .obj file without warning. I recall that it would also handle the situation where the symbol is defined in multiple libs it would use the one in the library named first in the list.
MSVC 过去的行为是,如果在 .obj 文件和 .lib 中定义了符号,它将在没有警告的情况下使用 .obj 文件中的符号。我记得它还可以处理在多个库中定义符号的情况,它将使用列表中第一个命名的库中的那个。
I can't say I've tried this in a while, but I'd be surprised if they changed this behavior (especially that .obj defined symbols override symbols in .lib files).
我不能说我已经尝试过一段时间了,但是如果他们改变了这种行为,我会感到惊讶(尤其是 .obj 定义的符号覆盖了 .lib 文件中的符号)。
回答by denis
The only way i know. Place each symbol in a separate library. User objects with overrides also must be combined to library. Then link all together to an application. User library must be specified as input file, your lib's must be transfered to linker using /DEFAULTLIB:option.
我知道的唯一方法。将每个符号放在单独的库中。具有覆盖的用户对象也必须组合到库中。然后将所有内容链接到一个应用程序。用户库必须指定为输入文件,您的库必须使用/DEFAULTLIB:选项传输到链接器。
回答by denis
There isn't an MS-VC equivalent to this attribute. See http://connect.microsoft.com/VisualStudio/feedback/details/505028/add-weak-function-references-for-visual-c-c. I'm going to suggest something horrible: reading the purpose of it here: http://www.kolpackov.net/pipermail/notes/2004-March/000006.htmlit is essentially to define functions that, if their symbols exist, are used, otherwise, are not, so...
没有与此属性等效的 MS-VC。请参阅http://connect.microsoft.com/VisualStudio/feedback/details/505028/add-weak-function-references-for-visual-cc。我要提出一些可怕的建议:在这里阅读它的目的:http: //www.kolpackov.net/pipermail/notes/2004-March/000006.html它本质上是定义函数,如果它们的符号存在,被使用,否则,不是,所以......
Why not use pre-processor for this purpose, with the huge caveat of "if you need to do this at all"? (I'm not a fan of recommending pre-processor).
为什么不为此目的使用预处理器,并带有“如果您需要这样做”的巨大警告?(我不喜欢推荐预处理器)。
Example:
例子:
#ifdef USE_MY_FUNCTION
extern void function();
#endif
then call appropriately in the application logic, surrounded by #ifdefstatements. If your static library is linked in, as part of the linking in process, tweak the defines to define USE_MY_FUNCTION.
然后在应用程序逻辑中适当地调用,用#ifdef语句包围。如果您的静态库已链接,作为链接过程的一部分,调整定义以定义 USE_MY_FUNCTION。
Not quite a direct equivalent and very ugly but it's the best I can think of.
不完全是一个直接的等价物,而且非常丑陋,但它是我能想到的最好的。
回答by Mark Wilkins
One way of doing this would be to implement it manually via LoadLibraryand GetProcAddress.
一种方法是通过LoadLibrary和GetProcAddress手动实现它。

