ldc-developers/ldc — pull request #793 by JinShil (2014-11-15 07:59:42+00:00)
Title: fix issue #780 - Pass linker switches to gcc instead of to ld

This provides a fix for https://github.com/ldc-developers/ldc/issues/780

`dmd` uses `gcc` for its linker, not `ld`. Currently `dmd` appends `-Xlinker` to linker switches (i.e. switches that begin with `-L`) so they bypass `gcc` and are passed directly to `ld`.

In GCC, some options are passed transparently to `ld` (e.g. -lmylib), some are recognized only by `gcc` but not by `ld`, and some seem to have a different affect if passed to `gcc` than if passed to `ld`. (See https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html for a description of GCC's linker options).

By passing `dmd`'s linker options directly to `ld`, it prevents users from leveraging options that are only recognized by `gcc` but affect the linking process, or options that have different effect in `gcc` than in `ld`. Such switches include `-static`, `-nodefaultlibs` and `-nostdlib`. (There may be others).

This pull request modifies the way `dmd` passes linker options to `gcc`. It removes the `-Xlinker` prefix for any `dmd` switch prefixed with `-L`. If users still wish to bypass `gcc` and pass switches directly to `ld`, they can use `-L-Xlinker -L-ldswitch` in their `dmd` command. This commit modifies dmd.conf to pass `--export-dynamic` directly to `ld`, since `--export-dynamic` is not recognized by `gcc`.

This change has the potential to break users' build scripts if they are depending on `ld`-specific options to be passed directly to `ld` using the `-L` prefix. But, I think this is the best way forward, as it allows users to control all aspects of the linking process. That is users can choose which options go to `gcc` and which options bypass `gcc` and go directly to `ld`. Currently, users don't have this capability.

A pull request for this issue was submitted to DMD as well: https://github.com/D-Programming-Language/dmd/pull/4140. GDC, being GCC, does not suffer from this problem.