ld -r
Nick Clifton
nickc@redhat.com
Mon May 11 11:35:19 GMT 2020
Hi Paul,
> Why does adding "-r" to "ld" allow the link
> to complete without error, even when there
> are undefined symbols?
Because the "-r" option tells the linker to perform a partial
link, not a full link. It combines together the input files
into a single output file, but this output is not a functioning
binary yet. It still needs a final link, which includes references
to the appropriate libraries and other object files, in order
for an executable to be produced.
> Is there an option I
> can use to get the link to fail as expected?
Just do not use the "-r" option.
> Note that I am using "-r" to produce
> relocatable executables. See below for example.
Unfortunately that is not the purpose of this option.
If you do not need to control the linker directly then using gcc to
perform the compilation and link is a much easier way to go. For
example:
gcc -fpic main.c
Note that you usually need to provide an option to gcc to tell it
that you want to produce a relocatable executable. In the above
example -fpic is used.
If you do need to control the linker directly then normally no
special options are needed to make a relocatable executable.
Provided that you have compiled the code with the -fpic or -fpie
options then a relocatable program should be produced. If you
are trying to build a relocatable library however then you do
need to give the linker the "-shared" command line option.
Cheers
Nick
More information about the Binutils
mailing list