On Friday, 1 August 2014 at 16:04:21 UTC, Daniel Murphy wrote:
"Johannes Pfau" wrote in message news:lrgar7$1vrr$1...@digitalmars.com...

Do you know if linkers actually guarantee that behaviour? AFAICS dmd doesn't do anything special, it always emits weak symbols and just calls gcc to link. The linker usually uses the first symbol it sees, but I
have not seen any guarantees for that.

Yes, they do. Or at least, it's the classic linker behaviour and I expect they all implement it. It's relied upon for nofloat and a few other things. It's one of the reasons linkers are hard to parallelize.

Though your linker specification is not the same as Johannes said.

Also what happens if your main application doesn't use the template, but two libraries use the same template. Then which instances are
actually used? I'd guess those from the 'first' library?

Well, the first one that it encounters after getting the undefined reference.

That's just how ld ended up working, and it's known to be pretty stupid in comparison with other linkers. And it's used rather stupidly, e.g. gcc just repeats libs several times to ensure ld sees all symbols it needs.

eg
module application
{
undef x
...
}


lib1:
module a
{
undef tmpl
def a
}
module tmpl
{
def tmpl
}

lib2:
module x
{
undef tmpl
def x
}
module tmpl
{
def tmpl
}

Here nothing that the application references will cause the tmpl in lib1 to get pulled in, but when it processes lib2 it will pull in x, and then tmpl.

lib2:
module x
{
undef tmpl
def x
}

phobos:
module tmpl
{
def tmpl
}

Then tmpl is picked from phobos instead of your code. Template emission optimization is a simple tool: if you realistically link against a module, which already has an instantiated template, template emission can be skipped and the linker will reasonably link whatever it finds.

Reply via email to