![]() |
![]() | |||||
|
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
|
UNIX System Program Development The process of developing a program1 to satisify a particular need requires access to a comprehensive program development environment. The UNIX system provides an exceptional programming environment. Because the operating system was written in C by highly talented programmers who had their own needs in mind, UNIX provides an ideal environment for program creation using C/C++. Operating system services are readily accessible to the C/C++ programmer in the form of function libraries and system calls2. In addition, there are a variety of tools for making the development and maintenance of programs easier. C/C++ and UNIX This course teaches C/C++ under the UNIX operating system. C/C++ programs will look similar under any other system (such as VMS or DOS), some other features will differ from system to system. In particular the method of compiling a program to produce a file of runnable code will be different on each system. As mentioned above, the UNIX operating system is written in C. In fact the C language was invented specifically to implement UNIX in a manner which made it machine independent. All of the UNIX commands which you type, plus the other system facilities such as password checking, lineprinter queues or magnetic tape controllers are written in C. In the course of the development of UNIX, hundreds of functions were written to provide access to various facets of the system. These functions are available to the C/C++ programmer in libraries. By writing in C/C++ and using the UNIX system libraries, very powerful programs can be created. These libraries are very difficult to access using any other language, C/C++ is therefore the natural language for writing UNIX system programs. What is a Compiler? A compiler is a special program that receives statements written in a particular programming language and translates them into the machine language or "object code" that a computer's processor understands. This machine language object code is an executable program file. Typically, a programmer writes program statements in a high-level programming language such as C or C++ one line at a time using a text editor. These statements are then saved to a file. This file contains what are called source statements or collectively the "source code." The programmer then runs the appropriate programming language compiler, specifying the name of the text file that contains the source statements. ![]()
![]() A preprocessor is a program invoked by various compilers to process code before compilation. For example, the C preprocessor, cpp, handles textual macro substitution, conditional compilation and inclusion of other files. A preprocessor may be used to transform a program into a simpler language, e.g., to transform C++ into C. A compiler works with what are sometimes called third-generation)4, fourth-generation, and fifth-generation languages. An assembler)5 works on programs written using a processor's assembly language. A link editor (or linker) is a computer program which accepts the object code files of one or more separately compiled program modules)6, and links them together into a complete executable)7 program file, resolving references from one module to another. Compiling a C Program Once you understand the purpose and functioning of a compiler, the next step is to use a specific compiler with a program written is C language. The following three commands can be used to compile a C program. In this example, the cc utility is used for a C program named prog.c:
et791:~$ cc prog.c The cc utility calls the C preprocessor, the C compiler, the assembler, and the link editor. The link editor creates an executable file named (by default) a.out. The second command renames a.out to prog. If you fail to rename the a.out file, the next use of the cc utility will overwrite the executable file. The last command is used to make the object code file (now named prog) executable so that you can run it and test it for logic and/or runtime errors. The -o argument (or parameter) can be used to speed up this process. The following two commands achieve the same results as the three above:
et791:~$ cc -o prog prog.c With this approach, there is no need to rename the a.out executable file. GNU C and C++ Compiler For a C++ program, we will be using the GNU8 C++ compiler which is called gcc or g++. Actually, the C and C++ compilers are integrated under the gcc and g++ utilities. Both utilities process C or C++ input source code files through one or more of the four stages mentioned above: preprocessing, compilation, assembly, and linking. Source code filename extensions indicate the specific language:
For example, to compile the C++ program grades.cpp we could use the g++ command as shown below: et791:~$ g++ grades.cpp However, as with the cc utility, a slightly more complex form of the command is both faster and more useful: et791:~$ g++ -Wall -g grades.cpp -o grades Each of the arguments in this command are explained as follows:
Running a Program It is important to remember that the compiler creates a file which is executable, meaning that you can run it by simply typing its name at the UNIX prompt. et791:~$ program_name If you type the name of an executable file and it does not run (i.e., you get an error message) try typing a "./" (dot slash) followed by the program_name, and then press the [Enter] key. et791:~$ ./program_name One of these two methods should work for you. Once the program is finished, you will see the UNIX prompt. Note: The "./" is needed to run the program because of a "path" issue. Finding and Correcting Errors The process of finding and correcting program errors is known as debugging. Debugging is the process of attempting to determine and correct the cause of the symptoms of program errors identified by compilation, testing or by frustrated users. Syntax errors. It is quite possible (in fact almost certain) for the compiler to detect some types of problems or errors in your program the first time you compile it. The types of errors that the compiler can find are called syntax errors. These are errors that simply mean the program you have given the compiler is not completely correct C code. For example, in arithmetic expressions, you must have a right parenthesis " ) " for ever left one " ( ". If you do not, the compiler will detect this mistake and issue a message indicating that there is a syntax error present. It cannot correct errors, because it does not know where you meant the other one to go. The output you get will usually look something like:
foo.c:82: syntax error The compiler will try to produce useful and informative messages that tell you when you've failed to use the C language correctly. However, in reality, they are often somewhat baffling. Don't hesitate to ask what an error means; we have more practice interpreting these messages and can often translate them for you. Also don't get too discouraged if you type in a 100 line program, try to compile it and two or three screenfulls of error messages fly by. This is part and parcel of the process of programming. Tracking down these problems is often perversely pleasurable and it will hopefully give you a greater appreciation of what the people out there building VCRs and airline reservation systems are going through. The syntax errors that the compiler can detect must all be corrected before it will produce a executable file. So continue to correct and recompile your program until the compiler runs without issuing any error messagess. Logic errors. Here you may discover another type of error. We usually call these logic errors to distinguish them from the syntax errors that the compiler finds. These are generally errors that are mistakes in how you've designed your program or in how you've translated your design to the C programming language. The compiler cannot find these types of errors for you since it cannot read your mind and doesn't know what you want the program to do. As a result these errors are generally more difficult to track down and fix. The process of doing so is what we call debugging. Runtime errors. One type of logic error that is often encountered is the infinite loop. An infinite loop is when a program continues running (forever) when you really wanted it to stop at some point. In order to deal with this error, you need some way of interrupting the program and stopping it from running. To kill a running program, type [Ctrl]+c to send it the interrupt signal. This approach will work for most of the programs that you will be developing in this course, ![]() Assignment Complete the following before our next meeting:
#include <iostream.h>
void main(void)
{
cout << "Hello World!" << endl;
}
Footnotes:
| |||||
| |
| Added to the Web: October 4, 1999. Last updated: October 4, 1999. Web page design by Dan Solarek. |
![]() http://cset.sp.utoledo.edu/cset4250/ |