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

## 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 on other sites
On ELF systems, yes. On PE systems, not that I am aware of.

##### Share on other sites
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 on other sites
You could always use #define and #ifdef's. Not totally the same, but perhaps it might be close enough.

##### 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 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 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 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 on other sites
Quote:
 Original post by chollida1Microsoft 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 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 on other sites
MinGW gave me this: warning: weak declaration of 'foo' not supported.

##### Share on other sites
Quote:
 Original post by MonderSo it looks like you can't do it with PE files.

At least with GCC. Though it's reasonable to assume that, if it was feasible on for PE, they'd have it figured out by now.

##### Share on other sites
This all just seems so weird to me. I mean, this attribute only needs to be present in .obj files (or .lib files) which are different from build system to build system. Once it's been properly linked (i.e. all externals resolved, not linking as in gluing units together into a static library) then the attribute wouldn't be present, and it doesn't become PE or ELF or anything up to that point because it's in the build system's intermediate format still.

• 12
• 10
• 11
• 18
• 13