HP-UX HB v13.00 Ch-11 - Software Development

HP-UX Handbook Rev 13.00 Page 15 (of 101)
Chapter 11 Software Development
October 29, 2013
Because the linker creates the program file or the shared library, it also gives it its name. If
nothing else is specified, the name will be a.out. A different name can be selected with the -o
<name> linker option. Every compiler frontend also knows this option, and if it is specified at the
command line, it is passed to the linker.
Linking HelloWorld
It is always recommended to use a compiler frontend to link. We simply pass HelloWorld.o to
cc. We don’t need to add the option -lc, the frontend command will append it as a default
option to the linker command line:
cc -v -o HelloWorld HelloWorld.o
cc: NLSPATH is
/opt/ansic/lib/nls/msg/%L/%N.cat:/opt/ansic/lib/nls/msg/C/%N.cat:
cc: CCOPTS is not set.
cc: INCLUDIR is INCLUDIR=/usr/include
cc: LPATH is /usr/lib:/opt/langtools/lib:
/usr/ccs/bin/ld /opt/langtools/lib/crt0.o -o HelloWorld HelloWorld.o -u
main -lc
cc: Entering Link editor.
cc passes the object and some default parameters to the linker. One of the defaults needed for
linking an executable is the object crt0.o. It contains the code required for the program startup
and initialization, e.g. here is the code that loads dld.sl|dld.so. crt0.o will always be passed to
the linker as the first object.
ld picks up our object, finds the entry point main and the undefined symbol printf. Now it
searches subsequently all other objects and libraries to find the definition of printf. It finally
finds it in the shared library libc.sl (PA) or libc.so (IA) which is added to the link command
via the -lc parameter. A reference to libc.sl | libc.so is stored in the binary. Herewith ld has
found all it needs to create the HelloWorld executable.
Linking In 64-bit Mode
The linker does not have a special option that switches between 32-bit or 64-bit mode. It simply
checks the type of the first object passed to it to decide which executable should be created.
When linking executables, this is always crt0.o. There is a 32-bit and a 64-bit version of it.
Without adding special options to the link command, the frontend will pick the 32-bit crt0.o
which will cause the linker to do a 32-bit link. With +DD64 the frontend picks the 64-bit crt0.o
which will make the linker do a 64-bit link.
Each object and shared library involved in a link phase must be of the same type, either 32-bit or
64-bit. Mixing both does not work and will result in a linker error message.