std library hooks

H. S. Teoh hsteoh at quickfur.ath.cx
Sat Apr 14 22:26:55 PDT 2012


On Sun, Apr 15, 2012 at 01:08:35AM -0400, Nick Sabalausky wrote:
> "Walter Bright" <newshound2 at digitalmars.com> wrote in message 
> news:jmdhof$1v3v$1 at digitalmars.com...
> > On 4/14/2012 4:54 PM, Manu wrote:
> >> I just tried it, and I got the error I expected:
> >>
> >> LIBCMTD.lib(atox.obj) : error LNK2005: _atoi already defined in Unit.obj
> >>
> >> In C, I would always explicitly declare weak linkage to do this...
> >> what's the point of the weak attribute if not for this?
> >
> > First you need to find out why you have multiple definitions of the
> > same symbol being linked together. For example, do not specify
> > libcmtd.lib to the linker, then link, then see what your unresolved
> > symbols are.
> 
> This contradicts what you said earlier about symbols in obj files
> always overriding ones in lib files. Either obj symbols override lib
> symbols or they conflict. Manu demonstrated that they conflict, rather
> than override.  How is your suggestion for replacing druntime
> functions supposed to work when the linker is just going to bitch
> about them being "already defined"?
[...]

Symbols in obj files do not override anything unless the library exports
weak symbols. Normal exports from the library will cause a conflict. At
least, that's how ld works; I can't speak for optlink.

(It *is* possible is to statically link your code with the obj file
containing the overriding functions first, into a kind of intermediate
"library" of sorts, strip it to hide the references to the conflicting
symbols, then link the rest of the system libraries.  But this is black
magic linker hacking, and may have unintended side-effects. It isn't
always possible, and you risk ending up with chimerical situations where
some library functions call one version of a function, and other code
calls another version of the same function. Can lead to nasty runtime
bugs caused by having incompatible versions of the same function(s). Not
advisable unless you know exactly what you're doing.)


T

-- 
It is widely believed that reinventing the wheel is a waste of time; but
I disagree: without wheel reinventers, we would be still be stuck with
wooden horse-cart wheels.


More information about the Digitalmars-d mailing list