pthread_stubs.5 (2010 09)
p
pthread_stubs(5) pthread_stubs(5)
Due to the problems mentioned above, -lc
should never be specified in the build command of an execut-
able or shared library. By default, the compiler drivers (cc, aCC, f90) automatically pass
-lc to the
linker at the end of the link line of the executables. To see if a shared library was built with
-lc, look at
the shared library list in the
chatr output (see chatr (1)), or list the dependent libraries with
ldd (see
ldd(1)):
$ cc +z -c lib1.c
$ ld -b -o lib1.sl lib1.o -lc
$ ldd lib1.sl
/usr/lib/libc.2 => /usr/lib/libc.2
/usr/lib/libdld.2 => /usr/lib/libdld.2
/usr/lib/libc.2 => /usr/lib/libc.2
$ cc +DA2.0W +z -c lib1.c
$ ld -b -o lib1.sl lib1.o -lc
$ ldd lib1.sl
libc.2 => /lib/pa20_64/libc.2
libdl.1 => /usr/lib/pa20_64/libdl.1
To see the order in which dependent shared libraries will be loaded at run-time (order is only valid in 64-
bit mode), use the
ldd command on the executable (ldd in 32-bit mode displays the order in which
libraries are loaded in reverse order):
$ cc +DA2.0W thread.c -lpthread
$ ldd a.out
libpthread.1 => /usr/lib/pa20_64/libpthread.1
libc.2 => /usr/lib/pa20_64/libc.2
libdl.1 => /usr/lib/pa20_64/libdl.1
$ cc +DA2.0W thread.c -lc -lpthread
$ ldd a.out
libc.2 => /usr/lib/pa20_64/libc.2
libpthread.1 => /usr/lib/pa20_64/libpthread.1
libdl.1 => /usr/lib/pa20_64/libdl.1
$ cc +DA2.0W thread.c -lpthread -lc
$ ldd a.out
libpthread.1 => /usr/lib/pa20_64/libpthread.1
libc.2 => /usr/lib/pa20_64/libc.2
libdl.1 => /usr/lib/pa20_64/libdl.1
Recommendations:
• Remove -lc from the build command of all shared libraries
• Remove
-lc from the build command of all executables
• Use the
LD_PRELOAD environment variable set to the full pathname for libpthread or libcma,
which will cause the library to be loaded at program startup before other dependent libraries.
LD_PRELOAD functionality is available in PHSS_22478 and later Linker patches. See the dld.sl (5)
man page.
• If you link directly with the ld(1) command instead of with a compiler driver, add
-lc as the last com-
ponent on the link line.
Example 3 (64-bit)
If a 64-bit shared library is built with
-lpthread but the executable is not, libc is loaded before
libpthread (due to breadth-first searching), and the pthread calls are resolved to the pthread stubs in
libc. At run-time, after the a.out is loaded, the dependencies of a.out are loaded in breadth-first
order: libc is loaded as a dependent of a.out before libpthread is loaded as a dependent of
HP-UX 11i Version 3: September 2010 − 5 − Hewlett-Packard Company 5