-

Bourne Shell: Chapter 5a

-

Please contact or Mt Xia for assistance with all your shell programming needs.


  • 5. COMPILING PROGRAMS IN UNIX. Go to the bottom, first,previous,next, last section, table of contents.

    5. COMPILING PROGRAMS IN UNIX

    This chapter will examine compiling source code programs in three high level languages "C", FORTRAN, and COBOL. The second part of the chapter will look at the archive and library maintainer. The archive allows you to create a library of object modules. These files are used by the link editor.

    5.1 "C": Sample Program with a Main and Two Functions in One

    File Based on the command line options, cc compiles, assembles, and load C language source code programs. It can also assemble and load assembly language source programs or merely load object programs.
      Command Format:  cc [options] file-list                      
                                                                   
      (See Appendix E for a complete list of options)              
    
    When using the cc utility, the following conventions are observed: 1. A filename with the extension of .c indicates a C language source program. 2. A filename with an extension of .s indicates an assembly language source program. 3. A filename with an extension of .o indicates an object program. The cc utility will take its input from the file or files you specify on the command line. Unless you use the -o option, it will store the executable program in a file called a.out. Sample C Language Source Code Program: $cat hello.c main () { printf ("Hello from main!\n\n"); printf ("Calling function1!\n\n"); funct1(); printf ("\t Back from function1!\n\n"); printf ("Calling function2!\n\n"); funct2(); printf ("\t Back from funct2!\n\n"); printf ("That's all!\n\n"); } funct1() { printf ("\t\t Hello from function1!\n\n); } funct2() { printf ("\t\t Hello from function2!\n\n); }

    5.2 "C": Compiling a Program

    To compile the previous example program into an executable module, enter the following command at the command line. Sample Session: $cc hello.c $ Without any options, cc accepts C source code and assembly language programs that follow the conventions outlined above. It will compile, assemble, and load these programs to produce an executable called a.out. The cc utility puts the object code in files with the same base filename (everything before the period) as the source but with a filename extension of .o . The a.out stands for assembly output. This is the default. Sample Session: $cc hello.c $a.out Hello from main! Calling function1! Hello from function1! Back from function1! Calling function2! Hello from function2! Back from function2! That's all! $ NOTE: The a.out file that was created by the cc utility has the following permissions: user - read, write, and execute group - read and execute other - read and execute It is not necessary for you to change the permissions using the chmod command because the cc utility set the execute permissions for you.

    5.3 "C": Renaming the Executable Module

    You can rename the executable module using the mv command. The file permissions will be the same as before the file is renamed. Sample Session: $mv a.out hello $hello Hello from main! Calling function1! Hello from function1! Back from function1! Calling function2! Hello from function2! Back from function2! That's all! $

    5.4 "C": Giving a Name to the Output File

    It is possible to have the output sent to a file you specify instead of a.out by using the following command:
     
      Command Format:  cc -o output source                         
                                                                   
      output - the name of the executable file                     
                                                                   
      source - the name of the C source code file                  
    
    The -o option tells cc to tell the link editor to use the specified name for the output instead of the default a.out. NOTE: It is not necessary for the -o option to appear after the cc command. The filename that appears after the -o is the name of the output file. For example, cc source - o output is the same as cc -o output source. Sample Session: $cc -o hello hello.c $hello Hello from main! Calling function1! Hello from function1! Back from function1! Calling function2! Hello from function2! Back from function2! That's all! $

    5.5 "C": Producing an Assembly Listing

    This option causes cc to compile C programs and leave the corresponding assembly language source programs in a file with filename extensions of .s.
      Command Format:  cc -S hello.c                               
                                                                   
      -S = Compile only                                            
    
    Sample Session: $cc -S hello.c $ls -C example.f hello hex.c octal.c hello.c hello.s multiply.c $

    5.6 "C": Main and Two Functions in Three Separate Source Files

    This is the same C program that we have seen before, except it is now in three files rather than one as before. The three files are main.c, funct1.c, and funct2.c. $cat main.c main () { printf ("Hello from main!\n\n"); printf ("Calling function1!\n\n"); funct1(); printf ("\t Back from function1!\n\n"); printf ("Calling function2!\n\n"); funct2(); printf ("\t Back from funct2!\n\n"); printf ("That's all!\n\n"); } $cat funct1.c funct1() { printf ("\t\t Hello from function1!\n\n); } $cat funct2.c funct2() { printf ("\t\t Hello from function2!\n\n); }

    5.7 "C": Compiling but Not Producing an Executable Module

    Using the previous program, the following command will compile but not produce an executable module.
     
         Command Format:     cc -c main.c funct1.c funct2.c        
                                                                   
         -c = Compile, but do not load object files.  This option  
              causes cc to compile and/or assemble source code     
              programs and leave the corresponding object programs 
              in files with filename extensions of .o.             
    
    Sample Session: $cc -c main.c funct1.c funct2.c main.c: funct1.c: funct2.c: $ls a.out a.out not found $ls -C *.o funct1.o funct2.o main.o $ The -c options causes the compilation system to suppress the link edit phase. This produces an object file or files, in this example (main.o funct1.o funct2.o), that can be link edited at a later time with the cc command with no options.

    5.8 FORTRAN: Sample Program a Main and Two Subroutines

    There are several conventions for use with the FORTRAN compiler. They are: 1. The name of the file containing the FORTRAN source code must end with .f. 2. The compiler is invoked with f77. 3. Several options are available with the compiler. (-c, -o, -p, -S) 4. Preconnections are made for stdin (unit5) and stdout (unit6). This is the FORTRAN source code example to be used in the following discussions of the FORTRAN compiler. Sample Session: $cat hello.f program calling write(6,100) 100 format (' Hello from main!',/) write(6,110) 110 format(' Calling subroutine1!',/) call sub1 write(6,120) 120 format(t15' Back from subroutine1!',/) write(6,130) 130 format(' Calling subroutine2!',/) call sub2 write(6,140) 140 format(t15' Back from subroutine2!',/) write(6,150) 150 format(' That's all, folks!') end subroutine sub1 write(6,200) 200 format(t20,' Hello from subroutine1!',/) end subroutine sub2 write(6,210) 210 format(t20,' Hello from subroutine2!',/) end

    5.9 FORTRAN: Compiling a Program

    The FORTRAN compiler is invoked with the following command:
     
      Command Format:  f77                                         
    
    To compile the above program into an executable program, use the following command at the command line. Sample Session: $f77 hello.f $ Without any options, f77 accepts FORTRAN source code and assembly language programs that follow the conventions outlined above. It will compile, assemble, and load these programs to produce an executable called a.out. The f77 utility outputs the object code into files with the same base filename (everything before the period) as the source but with a filename extension of .o. The a.out stands for assembly output. This is the default. Sample Session: $f77 hello.f $a.out Hello from main! Calling function1! Hello from function1! Back from function1! Calling function2! Hello from function2! Back from function2! That's all! $ NOTE: The a.out file that was created by the f77 utility has the following permissions: user - read, write, and execute group - read and execute other - read and execute It is not necessary for you to change the permissions using the chmod command because the f77 utility set the execute permissions for you.

    5.10 FORTRAN: Renaming the Executable Module

    You can rename the executable module using the mv command. The file permissions will be the same as before the file is renamed. Sample Session: $mv a.out hello $hello Hello from main! Calling function1! Hello from function1! Back from function1! Calling function2! Hello from function2! Back from function2! That's all! $

    Go to the top, first,previous,next, last section, table of contents.