Kenwood HP 9000 Personal Computer User Manual


 
Chapter 3 55
Linker Tasks
Using the Compiler to Link
Renaming the Output File with -o
The -o name option causes ld to name the output file name instead of
a.out. For example, to compile a C program prog.c and name the
resulting file sum_num:
$ cc -Aa -o sum_num prog.c Compile using -o option.
$ sum_num Run the program.
Enter a number to sum: 5
The sum of 1 to 5: 15
Specifying Libraries with -l
Sometimes programs call routines not contained in the default libraries.
In such cases you must explicitly specify the necessary libraries on the
compile line with the -l option. The compilers pass -l options directly to
the linker before the default libraries.
For example, if a C program calls library routines in the curses library
(libcurses), you must specify -lcurses on the cc command line:
$ cc -Aa -v cursesprog.c -lcurses
...
/usr/ccs/bin/ld /opt/langtools/lib/crt0.o -u main \
cursesprog.o -lcurses -lc
cc: Entering Link editor.
Linking with the crt0.o Startup File in 32-bit mode
Notice also, in the above example, that the compiler linked
cursesprog.o with the file /opt/langtools/lib/crt0.o. This file
contains object code that performs tasks which must be executed when a
program starts running — for example, retrieving any arguments
specified on the command line when the program is invoked. For details
on this file, see crt0(3) and “The crt0.o Startup File” on page 43.
Suppressing the Link-Edit Phase with -c
The -c compiler option suppresses the link-edit phase. That is, the
compiler generates only the .o files and not the a.out file. This is useful
when compiling source files that contain only subprograms and data.
These may be linked later with other object files, or placed in an archive
or shared library. The resulting object files can then be specified on the
compiler command line, just like source files. For example: