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.cxxIf 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.outNOTE: 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.
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.cxxIf 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.outNOTE: 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.
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.
math 26:aCC -o myprog myprog.cxxor
[jsmith@pascal ~/ma10]$ g++ -o myprog myprog.cxxAfter 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
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.
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:historythe "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.
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.