On 10/01/2016 11:29 PM, [email protected] wrote:
> Hi,
>
> Suppose I would compile a program, which uses shared libraries and I
> specify an additional library, which will be completly unused by the
> code...will the resulting executable differ from an executable which
> is compiled without this library ?
>
It depends. From the "ld" man page:
--as-needed
--no-as-needed
This option affects ELF DT_NEEDED tags for dynamic libraries
mentioned on the command line after the --as-needed option.
Normally the linker will add a DT_NEEDED tag for each dynamic
library mentioned on the command line, regardless of whether the
library is actually needed or not. --as-needed causes a DT_NEEDED
tag to only be emitted for a library that at that point in the link
satisfies a non-weak undefined symbol reference from a regular
object file or, if the library is not found in the DT_NEEDED lists
of other needed libraries, a non-weak undefined symbol reference
from another needed dynamic library. Object files or libraries
appearing on the command line after the library in question do not
affect whether the library is seen as needed. This is similar to
the rules for extraction of object files from archives.
--no-as-needed restores the default behaviour.
Here's a program:
$ cat main.c
int main(int argc, char* argv[]) {
return 0;
}
And here's what happens with various C/LDFLAGS:
$ gcc main.c
$ ldd a.out
linux-vdso.so.1
libc.so.6 => /lib64/libc.so.6
/lib64/ld-linux-x86-64.so.2
$ gcc -lcrypto main.c
$ ldd a.out
linux-vdso.so.1
libcrypto.so.1.0.0 => /usr/lib64/libcrypto.so.1.0.0
libc.so.6 => /lib64/libc.so.6
libdl.so.2 => /lib64/libdl.so.2
libz.so.1 => /lib64/libz.so.1
/lib64/ld-linux-x86-64.so.2
$ gcc -Wl,--as-needed -lcrypto main.c
$ ldd a.out
linux-vdso.so.1
libc.so.6 => /lib64/libc.so.6
/lib64/ld-linux-x86-64.so.2
Note that the binaries in the first and third examples still differ,
although I don't know why off the top of my head.