Design of large UNIX programs


                   The design of a large program requires a great deal more care than a small program. In order to make the complexity of a large program manageable, the program is broken down into components, each of which is responsible for a small, well-defined part of the overall function of the program. Most languages have procedure and/or function constructions for dividing a program into components
But for larger programs, higher-level components are needed. Consider a calculator program that calculates the values of C-like real expressions involving variables and can assign values to variables. A program designer can come up with a reasonable decomposition of this program into high-level components without having to consider details about how those components are implemented.
First of all, since the expressions involve variables, there is a need to be able to save and retrieve values of variables. The need for this kind of functionality arises often in programs, and there are well-known data structures called tables that provide that kind of functionality.
Second, file input is in terms of characters, but understanding expressions and how they are evaluated is easiest with units called tokens. A token is a unit of text, such as a number or identifier or an operator that may contain more than one character. For a calculator program, it is useful to have a high-level component that breaks up a stream of characters into these tokens and classifies them.
With these two components, the work of the main program is much easier to accomplish, with the responsibility of token classification and variable value storage and retrieval delegated to other components.
Both components involve several functions that work together in a coordinated way. For example, the table component will require a function for assigning a value to a variable and another for retrieving the value of a variable, and it will also need some sort of body of data (perhaps an array) for saving the data. Since these components have distinct responsibilities, it is desirable to be able to put them into separate code files table.C and tokens.C, and compile them and test them separately. The main program is then written in a third file calculator.C, and it is tested and compiled after the others have been thoroughly tested.

Compiling UNIX programs

Separate compilation is an integral part of the standard for the C programming language. When a C source code file is compiled there are two tasks performed by the compiler. First, the file is compiled into a format called an object file. Then the object file is linked with a library of standard C functions to produce an executable file.
An object file contains coding of the source code file into language that the machine understands. It is incomplete in that it may contain calls to functions that are in other files, and it need not contain a main function. When Unix compilers produce an object file, it has the same name as the source code file except that the suffix is changed to .o.
Suppose you want to compile prog.C to produce the executable program prog. Normally, you would give the command
g++ -Wall -o prog prog.C
First the g++ program compiles prog.C and creates the object Separate compilation is an integral part of the standard for the C programming language. When a C source code file is compiled there are two tasks performed by the compiler. First, the file is compiled into a format called an object file. Then the object file is linked with a library of standard C functions to produce an executable file.
An object file contains coding of the source code file into language that the machine understands. It is incomplete in that it may contain calls to functions that are in other files, and it need not contain a main function. When Unix compilers produce an object file, it has the same name as the source code file except that the suffix is changed to .o.
Suppose you want to compile prog.C to produce the file prog.o. Then it links prog.o with the standard C libraries and creates the executable file prog. When called with a single .C argument, the g++ program removes the .o file.
The compilation and linking steps can be executed separately as follows:
g++ -Wall -c prog.C
g++ -o prog prog.o
The first g++ command just compiles prog.C into prog.o, and the second links it to create prog. The g++ program recognizes the .o suffix so that it doesn't try to compile prog.o.
Now suppose you have C code files described above for the calculator program. Then the following commands will create the corresponding object files:
g++ -Wall -c calculator.C
g++ -Wall -c table.C
g++ -Wall -c tokens.C
When the g++ program is called with a list of .o files it will link them all together to form an executable program. So the following command creates the calculator program:
g++ -o calculator calculator.o table.o tokens.o


Contributed by :

Pooja Bangera 
sahyadri college of engineering and management


Comments