Chapter 2 41
What Happens When You Compile and Link a Program
Compiler-Linker Interaction
Compiler-Linker Interaction
As described in “Looking “inside” a Compiler”, the compilers
automatically call ld to create an executable file. To see how the
compilers call ld, run the compiler with the -v (verbose) option. For
example, compiling a C program in 32-bit mode produces the output
below:
$ cc -Aa -v main.c func.c -lm
cc: CCOPTS is not set.
main.c:
/opt/langtools/lbin/cpp.ansi main.c /var/tmp/ctmAAAa10102 \\
-D__hp9000s700 -D__hp9000s800 -D__hppa -D__hpux \\
-D__unix -D_PA_RISC1_1
cc: Entering Preprocessor.
/opt/ansic/lbin/ccom /var/tmp/ctmAAAa10102 main.o -O0 -Aa
func.c:
/opt/langtools/lbin/cpp.ansi func.c /var/tmp/ctmAAAa10102 \\
-D__hp9000s700 -D__hp9000s800 -D__hppa -D__hpux \\
-D__unix -D_PA_RISC1_1
cc: Entering Preprocessor.
/opt/ansic/lbin/ccom /var/tmp/ctmAAAa10102 func.o -O0 -Aa
cc: LPATH is /usr/lib/pa1.1:/usr/lib:/opt/langtools/lib:
/usr/ccs/bin/ld /opt/langtools/lib/crt0.o -u main main.o
func.o -lm -lc
cc: Entering Link editor.
The next-to-last line in the above example is the command line the
compiler used to invoke the 32-bit mode linker, /usr/ccs/bin/ld. In
this command, ld combines a startup file (crt0.o) and the two object
files created by the compiler (main.o and func.o). Also, ld searches the
libm and libc libraries.
In 64-bit mode, the startup functions are handled by the dynamic loader,
dld.sl. In most cases, the ld command line does not include crt0.o.
NOTE If you are linking any C++ object files to create an executable or a shared
library, you must use the CC command to link. This ensures that
c++patch executes and chains together your nonlocal static constructors
and destructors. If you use ld, the library or executable may not work
correctly and you may not get any error messages. For more information
see the HP C++ Programmer's Guide.