Kenwood HP 9000 Personal Computer User Manual


 
36 Chapter 2
What Happens When You Compile and Link a Program
Compiling Programs on HP-UX: An Example
Compiling Programs on HP-UX:
An Example
To create an executable program, you compile a source file containing a
main program. For example, to compile an ANSI C program named
sumnum.c, shown below, use this command (-Aa says to compile in ANSI
mode):
$ cc -Aa sumnum.c
The compiler displays status, warning, and error messages to standard
error output (stderr). If no errors occur, the compiler creates an
executable file named a.out in the current working directory. If your
PATH environment variable includes the current working directory, you
can run a.out as follows:
$ a.out
Enter a number: 4
Sum1to4:10
The process is essentially the same for all HP-UX compilers. For
instance, to compile and run a similar FORTRAN program named
sumnum.f:
$ f77 sumnum.f Compile and link sumnum.f.
... The compiler displays any messages here.
$ a.out Run the program.
... Output from the program is displayed here.
Program source can also be divided among separate files. For example,
sumnum.c could be divided into two files: main.c, containing the main
program, and func.c, containing the function sum_n. The command for
compiling the two together is:
$ cc -Aa main.c func.c
main.c:
func.c:
Notice that cc displays the name of each source file it compiles. This way,
if errors occur, you know where they occur.
#include <stdio.h> /* contains standard I/O defs */
int sum_n( int n ) /* sum numbers from n to 1 */
{
int sum = 0; /* running total; initially 0 */
for (; n >= 1; n--) /* sum from n to 1 */