Jump to content
  • Advertisement
Sign in to follow this  
superpig

Weak linking

This topic is 4834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible to specify a symbol as 'weakly linked,' i.e. that if a second definition of the symbol is found at link time, the weak one can be discarded? I'd like to provide default implementation of some functions, but also allow people to override them by specifying their own. Their ones, not being marked weak, would knock out my ones. I've checked the #pragma and __declspec() docs and haven't seen anything.

Share this post


Link to post
Share on other sites
Advertisement
I find that quite surprising. Surely this is performed at link-time and so would be dependent on the linker you use rather than the executable format?

Share this post


Link to post
Share on other sites
You could always use #define and #ifdef's. Not totally the same, but perhaps it might be close enough.

Share this post


Link to post
Share on other sites
The problem is that those are only applied at compiletime. I'm looking at this as something like a static library - precompiled code that the linker uses when the client has not specified their own implementation.

Share this post


Link to post
Share on other sites
You could perhaps fudge something together, something like:

#ifndef USER_FUNCTIONS
# pragma comment(lib,"MyLib.lib")
#endif


Although that's still fairly ugly, and means the user has to define USER_FUNCTIONS if they provide their own version of the functions.

Share this post


Link to post
Share on other sites
Microsoft uses #pragma anyone all over the place in its ATL library. Its supposed to specify that "hey, I"m defining alot of these and their all the same, linker grab the one you want"

I'm sorry its not a pragma its __declspec(selectany)

Cheers
Chris

Share this post


Link to post
Share on other sites
If you want to kludge it in a very messy fashion, the MSVC++ linker has an option /force:multiple which forces a linker to ignore multiple definitions of symbols and just choose a definition to goes for. If you could determine how to make it choose certain symbols (I'd guess it depends on the order you link things) you could get it to do what you want. Though of course this is a very bad way to do it but if there's no other options you may want to try it out.

Share this post


Link to post
Share on other sites
Quote:
Original post by chollida1
Microsoft uses #pragma anyone all over the place in its ATL library. Its supposed to specify that "hey, I"m defining alot of these and their all the same, linker grab the one you want"


That's not what weak linking is. With weak linking, the (for example) function is present in the library but if the user also defines the function in his own code, then that version is used instead.
A typical example would be in the lex & yacc libraries, which do provide their own (weak) main() functions, letting you directly test your parser, while not preventing you from providing your own main() function once you integrate it in your full application.

Superpig - To the best of my knowledge, it is the linker which decides on the final executable format. [grin] You could have a look at the win32 ports of GCC and check if they do provide the weak attribute (or if the GCC documentation gives any indication as to when it is available).

Share this post


Link to post
Share on other sites
Well from the GCC manual page here

Quote:
weak
The weak attribute causes the declaration to be emitted as a weak symbol rather than a global. This is primarily useful in defining library functions which can be overridden in user code, though it can also be used with non-function declarations. Weak symbols are supported for ELF targets, and also for a.out targets when using the GNU assembler and linker.


So it looks like you can't do it with PE files.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!