Unix 6 -- Running Programs and Other Information

Math 10 -- D. C. Smolarski, S.J.
Santa Clara University, Department of Mathematics and Computer Science

[Return to Math 10 Homepage | Return to Math 61 Homepage]
[Return to Unix 5 Page | Go to Unix 7 Page ]

Contents


GNU C++ or "G++"

The C++ compiler on the Linux server pascal is distributed by GNU and is named g++. On-line help is available via man g++.

C++ source code should be contained in files with the extension c++ or cxx or cpp (other extensions are also permitted -- see man pages).

On Unix systems one invokes a compiler on the source file by indicating the compiler name followed by the source code filename, e.g.

     [jsmith@pascal ~/ma10]$ g++ myprog.cxx
If there are no errors the executable file is put in a file named a.out which is executable merely by indicate the filename, e.g.,
     [jsmith@pascal ~/ma10]$ a.out
NOTE: If this command does not work, it could be that the system is not recognizing the current user directory, and you must use the command ./a.out. This can be avoided by correct the path in the .cshrc file.

HP C++

The C++ compiler on the HP server math is distributed by HP, and the command name is aCC. On-line help is available via man aCC or via aCC +help (if using an X-Server interface), or at this HP Document link.

C++ source code should be contained in files with the extension c++ or cxx or cpp (other extensions are also permitted -- see man pages -- but it is good to use extensions that distinguish C++ code from "pure" C code).

On Unix systems one invokes a compiler on the source file by indicating the compiler name followed by the source code filename, e.g.

     math 23:aCC myprog.cxx
If there are no errors the executable file is put in a file named a.out which is executable merely by indicate the filename, e.g.,
     math 24:a.out
NOTE: If this command does not work, it could be that the system is not recognizing the current user directory, and you must use the command ./a.out. This can be avoided by correct the path in the .cshrc file.

File Redirection

"File Redirection" refers to using disk data files for input or output rather than the keyboard and screen ("standard IO") and usually refers to giving commands at runtime to use files rather than having to recode the program itself to refer to such data files. Unix makes file redirection very simple. This also enables a user, for example, to use several different input files without recompiling the program, and to see the output on the screen before saving it in a file without having to change the program or recompile the code.

To redirect input and output, one may use < (for input) and > (for output), e.g.,

     math 25:a.out < infile.inp > outfile.out

WARNING! One should NEVER redirect output to a file unless one first views the output on the screen! If a program gets caught in an infinite loop, the output file will grow until it uses up all available disk space and you will be unable to create or edit any file unless you delete that file. ALWAYS run a program without output file redirection first, and only when you are convinced the output is correct, then redirect the output to a file to be able to print it.

Renamed Executable File

One can either rename the a.out file for future use or use a "-o switch" in the compilation to give the compiled file a different name, e.g.
     math 26:aCC -o myprog myprog.cxx
or
     [jsmith@pascal ~/ma10]$ g++ -o myprog myprog.cxx
After this, myprog (with no extension) will be an executable version of myprog.cxx. Thus to execute the program in this approach, one types
     math 27:myprog

Tips on Debugging

Multiple errors are often difficult to debug because so many appear on the screen in the process of compilation. Thus, it is best to copy the list of errors and to print out the source code (C++) file and identify errors away from a computer.

To copy the errors produced by compilation, invoke the Unix command script at the system prompt. This commands records all subsequent information appearing on the screen in a file, by default named typescript. Then, invoke the g++ compiler on the desired source file of C++ code, and then invoke the Unix command exit to stop the scripting. Finally, invoke the command lp typescript to print out the file of errors. In other words, do the following commands:

    math 21: script
    math 22: aCC mp3.cxx
    math 23: exit
    math 24: lp typescript

To print a C++ file WITH line numbers use the pr command and the -n switch (for line numbers) and "pipe" the output to the printer via the following:

     math 21: pr -n mp3.cxx | lp

One can then compare the list of errors (with line number references) with the actual source code (now with line numbers) and make corrections before going back to the computer.

Other Unix Features

The C-shell (or Turbo C-shell) on math supports filename completion in commands.

This means that one may type, in a Unix command, a few letters of a filename or directory and then press the escape key to complete te rest of the name. If there is a possible conflict, the system will complete as much of the name as possible (until options begin), and the user should add a character or two to eliminate ambiguity, and then press escape again.

math also supports command history. When a user types

     math 28:history
the "history" or last 20 (or so) commands will appear on the screen. (The exact number can be determined in your .cshrc file.) Any of these commands can be reexecuted by entering the ! and the line number corresponding to the command.

Alternatively, a previous command can be reexecuted by entering the ! and the first few letters of the command (as few as needed to avoid ambiguity).

The double exclamation point !! reexecutes the previous command.

NOTE: The POSIX and sh shells also support history recall and command completion, but the commands are slightly different. Those who wish to use a different shell should check the on-line manual files for further information by invoking man sh.


[Return to Math 10 Homepage | Return to Math 61 Homepage]
[Return to Unix 5 Page | Go to Unix 7 Page ]

This page is maintained by Dennis C. Smolarski, S.J. dsmolarski@math.scu.edu
© Copyright 1998-2001 Dennis C. Smolarski, SJ, All rights reserved.
Last changed: 16 September 2001.