According to the GCC 6.1 manual on Link Options:
-Wl, option
Pass option as an option to the linker. If option contains commas, it is split into multiple options at the commas.
-Xlinker option
Pass option as an option to the linker. You can use this to supply system-specific linker options that GCC does not recognize.
Why we need 2 of them?
Similarly, why we need both -Xassembler
and -Wa
for assembler options?
Quote from GCC 6.1 doc on Assembler Options:
-Wa,option
Pass option as an option to the assembler. If option contains commas, it is split into multiple options at the commas.
-Xassembler option
Pass option as an option to the assembler. You can use this to supply system-specific assembler options that GCC does not recognize.
Difference is into the following bold text
-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.
-Xlinker option
Pass option as an option to the linker. You can use this to supply system-specific linker options that GCC does not 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.
I guess 2 similar function still there due to back compatibility.