views:

452

answers:

4

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?

A: 

One way of doing this would be to implement it manually via LoadLibrary and GetProcAddress.

Mark Wilkins
nasty and lots of boilerplate... sigh c++ :P
Matt Joiner
Indeed. It makes for some seriously ugly code.
Mark Wilkins
The weak function will be in a static library. Would those functions work without DLLs?
caspin
@Matt Joiner: What do these functions have to do with C++?
Void
@Caspin: If I understand what you are asking, then yes, I believe it would work. Unless I am forgetting something fundamental, there is no problem loading a DLL from a static lib.
Mark Wilkins
@Void: Linux, Win32, it makes no difference. Dynamic linking is a royal pain in C++. It's just not "pretty", as Mark Wilkins put it above. Compare to Python's equivalent: `someLib.oldFunc = newFunc`
Matt Joiner
@Matt Joiner: Dynamic linking is certainly easy in Python. I agree with you there. My point was that the issues you are referring to are not limited to C++. They exist in other languages, like C, as well.
Void
A: 

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.html it is essentially to define functions that, if their symbols exist, are used, otherwise, are not, so...

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. If your static library is linked in, as part of the linking in process, tweak the defines to define USE_MY_FUNCTION.

Not quite a direct equivalent and very ugly but it's the best I can think of.

Ninefingers
+2  A: 

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++.

AndreyT
+1  A: 

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.

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

Michael Burr
A brief test with VS 2010 RC indicates that the behavior I described is still there.
Michael Burr