42

I have been looking through some configuration files and I've seen both being used (albeit on different architectures). If you're using GCC on a Linux box, is there a difference between the two syntaxes for passing options to the linker?

Reading the GCC manual they are explained it almost identically as far as I could tell.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bpw1621
  • 2,962
  • 2
  • 32
  • 35

1 Answers1

49

From man gcc:

-Xlinker option

Pass option as an option to the linker. You can use this to supply system-specific linker options which GCC does not know how to recognize.

If you want to pass an option that takes a separate argument, you must use -Xlinker twice, once for the option and once for the argument. For example, to pass -assert definitions, you must write -Xlinker -assert -Xlinker definitions. It does not work to write -Xlinker "-assert definitions", because this passes the entire string as a single argument, which is not what the linker expects.

When using the GNU linker, it is usually more convenient to pass arguments to linker options using the option=value syntax than as separate arguments. For example, you can specify -Xlinker -Map=output.map rather than -Xlinker -Map -Xlinker output.map. Other linkers may not support this syntax for command-line options.

-Wl,option

Pass option as an option to the linker. If option contains commas, it is split into multiple options at the commas. You can use this syntax to pass an argument to the option.

For example, -Wl,-Map,output.map passes -Map output.map to the linker. When using the GNU linker, you can also get the same effect with -Wl,-Map=output.map.

As you can se, the only difference is that -Wl allows you to specify multiple arguments by means of a comma, like -Wl,-rpath,/my/libs, which you cannot do with -Xlinker; on the other hand, -Xlinker is maybe a bit more self-descriptive. Take your pick. Also check other compilers (nvcc comes to mind, and clang) to see if any of them agree on the syntax, and then use that for portability if that's important to you.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 3
    You can get the same behavior, as noted in the manual, from multiple -Xlinker options. I had read the manual page and from that I believed there was no difference. What I was wondering was whether there was any reason you would see both in code in the wild (e.g., portability or one vice the other, etc.) other than personal preference. – bpw1621 Aug 28 '11 at 13:42
  • 3
    Well, as I said, other compilers may also support one of the options but not the other. For instance, `nvcc` only accepts `-Xlinker`. But that's no longer a pure "gcc" question :-) – Kerrek SB Aug 28 '11 at 14:11