On Tue, Apr 10, 2012 at 7:46 PM, Jonathan M Davis <jmdavisp...@gmx.com>wrote:

> This stackoverflow question raises an interesting issue with linking order
> and
> dmd:
>
>
> http://stackoverflow.com/questions/10095150/std-net-curl-linker-errors-in-linux
>
> I don't know if it's this way on Linux machines in general, but the OP had
> to
> link his program manually to be able to use std.curl. dmd appears to put
> the -
> L argument before any of its own linker arguments, and in this case, the
> linking argument for curl needs to go on the end.
>
> So, my question is whether dmd should be changed to put any -L arguments
> passed to it after the arguments that it passes to the linker itself. I'm
> far
> from an expert on this and am quite surprised that the order of arguments
> to
> the linker matters, but since it does, it seems to me that we should find
> the
> optimal order for dmd to use if we can, since it's not terribly user
> friendly
> to force people to call gcc or ld directly rather than using dmd to link
> just
> because they want to pass an argument to the linker. That's what the -L
> flag is
> supposed to be for.
>
> - Jonathan M Davis
>

That was my Stack Overflow question.  I was getting fairly frustrated and
only figured out how to fix it through a lucky guess. Other new users to D
on Linux might not be so lucky and give up.  I'm certainly no expert on
linking but the fix for this sounds as simple as just tacking the command
line arguments on the end rather than at the beginning.  This really only
needs to happen with libraries phobos uses (like libcurl) though.

Looking in link.c in dmd I see the comment:

    /* Standard libraries must go after user specified libraries
     * passed with -l.
     */

Which, if the git blame is correct, was written by Brad Roberts so maybe he
knows why this is the case.  Further down it makes reference to the fact
that librt must be linked after phobos2 on Ubuntu 11.10 so the link order
issue is acknowledged with respect to that one library.

It appears to be simple enough to change the order to fix the issue
I experienced (even *I* could make such a pull request) but without knowing
the reasoning behind that source comment I don't feel comfortable messing
with it.

Even better from a user standpoint would be linking libcurl in by default
without requiring the user to specify it themselves.  There is, of course,
the issue that D should not require libcurl to be on a system.  It seems
unfortunate that ld doesn't seem to have a "link this library if it can be
found, otherwise continue without error" option (though there are, no
doubt, issues with this that I'm just not knowledgeable enough to know
about).

Regards,
Brad Anderson

Reply via email to