简体   繁体   English

GCC样式在Visual Studio中的弱链接?

[英]GCC style weak linking in Visual Studio?

GCC has the ability to make a symbol link weakly via __attribute__((weak)) . GCC可以通过__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风格的弱符号可以让我做到这一点,但是我不知道它是否可以在Visual Studio中完成。

Does Visual Studio offer a similar feature? Visual Studio是否提供类似的功能?

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")

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. MSVC ++具有__declspec(selectany) ,它涵盖了弱符号的部分功能:它允许您使用外部链接定义多个相同的符号,从而指示编译器选择多个可用符号中的任何一个。 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 ++不会涵盖弱符号功能的另一部分:在库中提供“可替换”定义的可能性。

This, BTW, makes one wonder how the support for standard replaceable ::operator new and ::operator delete functions works in MSVC++. 顺便说一句,BTW使一个奇怪的问题是对标准可替换::operator new::operator delete函数的支持在MSVC ++中如何工作。

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. MSVC过去的行为是,如果在.obj文件和.lib中定义了符号,则它将在.obj文件中使用该符号而不会发出警告。 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. 我记得它还会处理在多个库中定义符号的情况,它将使用列表中第一个命名的库中的符号。

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文件中的符号)。

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:选项转移到链接器。

There isn't an MS-VC equivalent to this attribute. 没有等效于此属性的MS-VC。 See http://connect.microsoft.com/VisualStudio/feedback/details/505028/add-weak-function-references-for-visual-cc . 请参阅http://connect.microsoft.com/VisualStudio/feedback/details/505028/add-weak-function-references-for-visual-cc I'm going to suggest something horrible: reading the purpose of it here: http://www.kolpackov.net/pipermail/notes/2004-March/000006.html it is essentially to define functions that, if their symbols exist, are used, otherwise, are not, so... 我将提出一些可怕的建议:在此处阅读其目的: 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 #ifdef statements. 然后在由#ifdef语句包围的应用程序逻辑中进行适当的调用。 If your static library is linked in, as part of the linking in process, tweak the defines to define USE_MY_FUNCTION. 如果您的静态库已链接,则在进行链接的过程中,请调整定义以定义USE_MY_FUNCTION。

Not quite a direct equivalent and very ugly but it's the best I can think of. 它不是直接等效的,而且非常丑陋,但这是我能想到的最好的。

一种实现方法是通过LoadLibraryGetProcAddress手动实现它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM