Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC style weak linking in 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.

Does Visual Studio offer a similar feature?

like image 876
deft_code Avatar asked Feb 18 '10 17:02

deft_code


2 Answers

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") 
like image 157
Ringo Avatar answered Sep 23 '22 15:09

Ringo


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.

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

like image 29
AnT Avatar answered Sep 25 '22 15:09

AnT