问题
How can I tell the linker of g++
to allow multiple definitions of symbols (choose the first appearance)?
-z multidefs Allows multiple symbol definitions. By default, multiple symbol definitions occurring between relocatable objects (.o files) will result in a fatal error condition. This option suppresses the error condition and allows the first symbol definition to be taken. This option is valid only when the -b svr4 option is specified.
The -zmuldefs
option is not recognized by g++
, nor -z OPTION
. What's the correct parameter? Is it possible?
回答1:
There is no such thing as the "linker of g++", it uses your system's own linker.
To pass options through from GCC to the linker you need to use GCC's -Wl,
or -Xlinker
options:
-Xlinker option
Passoption
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 theoption=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
Passoption
as an option to the linker. Ifoption
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
.
So you would want to use
-Xlinker -z -Xlinker multidefs
or
-Wl,-z,multidefs
but the docs you quoted say you must also use -b svr4
to use that option, e.g.
-Wl,-b,svr4,-z,multidefs
Edit: From your comments I see you're on Mac OS X, which uses the darwin linker, and its man page shows the corresponding option is obsolete:
-m
Don't treat multiple definitions as an error. This is no longer supported. This option is obsolete.
回答2:
GCC uses ld
as linker in linux, which is part of binutils
created by GNU.
It seems the newer version of ld
do not support -z muldefs
option. You can try to use --allow-multiple-definition
as an alternative.
来源:https://stackoverflow.com/questions/17443976/how-to-allow-z-multidefs-with-g47