GCC style weak linking in Visual Studio?

deft_code picture deft_code · Feb 18, 2010 · Viewed 21.6k times · Source

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.

Does Visual Studio offer a similar feature?

Answer

Ringo picture Ringo · Jul 17, 2012

You can do it, here is an example in 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")