Stanford CS 107 - The Unix Programming Environment

Unformatted text preview:

CS107 Handout 08 Spring 2008 April 8, 2008 The UNIX Programming Environment This handout was compiled by many other past CS107 and CS193D lecturers and TAs. Introduction In writing C and C++ programs to run under Unix, there are several concepts and tools that turn out to be quite useful. The most obvious difference, if you are coming from a PC or Macintosh programming background, is that the tools are separate entities, not components in a tightly coupled environment like Metrowerks CodeWarrior or Microsoft Visual C++. The appendix at the end of the handout gives a summary of some basic UNIX and EMACS commands. The most important tools in this domain are the editor, the compiler, the linker, the make utility, and the debugger. There are a variety of choices as far as "which compiler" or "which editor", but the choice is usually one of personal preference. The choice of editor, however, is almost a religious issue. EMACS integrates well with the other tools, has a nice graphical interface, and is almost an operating system unto itself, so we will encourage its use. Caveat This handout is not meant to give a comprehensive discussion of any of the tools discussed, rather it should be used as an introduction to getting started with the tools. If you find that you need more information, all of the programs have extensive man pages and xinfo entries, and gdb has some on-line help for all of its commands. These man pages list numerous bits of information, some of which will appear to be quite attractive, but if you do not know or understand what an option does, especially for the compiler, please do not use it just because it sounds nice. Also, O'Reilly & Associates publishes a pretty good set of references for these basic tools, the titles of which resemble "UNIX in a Nutshell". These are not required or endorsed (for the record), but may be useful if you get lost. The Editing Process The first frontier a person is confronted with when programming is the editing environment. As mentioned, the choice of editor in UNIX was and still is, for various valid and not so valid reasons, pretty much set in stone as the 11th commandment: Whosoever writes programs with EMACSi shall live without bugs, or else... EMACS has pretty much all the features you will possible need in an editor, and then some; the only problem is finding out how to invoke them as needed. This section is only meant as a quick reference to get you started. To start editing a new or existing file using EMACS, simply type this to the UNIX prompt: emacs filename where filename is the file to be edited. Once EMACS has started up, text entry works much the same way as in any other text editors, i.e. the default mode is INSERT mode where text being i In the 4th Century AD, Rome was plagued by the worshipers of the Church of Emacs. They never bathed. They would gather in large, foul-smelling groups and harass small children.... - Cornell Quote2 typed is inserted at the current cursor position. All the fancy editing commands, such as find-and-replace, are invoked through typing special key sequences (there really isn’t a great mouse-driven UNIX editor for writing code). Two important key sequences to remember are: ^x (holding down the "ctrl" key while typing 'x') and [esc]-x (simply pressing the "esc" key followed by typing 'x'), both of which are used to start command sequences. Note that in most user manuals for EMACS, the “esc” key is actually refered to as the “Meta” key (let’s not bother explaining that one). Therefore, you will often see commands prefaced with the key sequence M-x, as opposed to the [esc]-x that we will use in this handout. Since the two are pretty much synonymous, we’ll stick with explicitly using the “esc” key. Now let’s see some examples of these mysterious “command sequences”. For instance, to save the file being edited the sequence is ^x^s. To exit (and be prompted to save) EMACS, the sequence is ^x^c. To open another file within EMACS, the sequence is ^x^f ('f' for "find file"). This sequence can be used to open an existing file as well as a new file. If you have multiple files open, EMACS stores them in different "buffers." To switch from one buffer to another (very handy when you are editing a .c source file and need to refer to the prototypes and definitions in the .h header file), you use the key sequence ^x followed by typing 'b' (without holding down "ctrl" when typing 'b'). You can then enter the name of the file to switch to the corresponding buffer (a default name is provided for fast switching). The arrow keys usually work as the cursor movement keys, but in the event that they don't, ^f is for moving forward (right), ^b is for moving backward (left), ^p is for moving to the previous line (up), and ^n is for moving to the next line (down). [esc]-< ([esc] followed by '<') moves to the beginning of the file, and [esc]-> moves to the end of the file. Finally, ^v does a "page down" and [esc]-v does a "page up." Note that the [esc] key is simply pressed and not held, whereas the [ctrl] key is held in all cases. For a listing of some other functions, please consult the appendix at the end of this handout. Quite frequently you'll make typing mistakes. The backspace key usually deletes the character before the text cursor, but sometimes it is strangely bound to invoke help! In case that fails, try the delete key (the one on the mini-keypad that also contains page up, page down, etc). You can also delete a whole line at a time by using ^k. Another important editting feature is copy/cut and paste. To copy or cut, you first have to select a region of text. To tell emacs where this region begins, use [esc]-@ or ^[space] (control and space simultaneously). Then move the text cursor to the end of the region. Alternatively, if you're running emacs under X Windows, you can use your mouse by pressing down on the left button at the start of the region and "drag" your mouse cursor to the end. To copy the region, use [esc]-w, while to cut the region, use ^w. To paste, use ^y. For a listing of some other functions, please consult the


View Full Document

Stanford CS 107 - The Unix Programming Environment

Download The Unix Programming Environment
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view The Unix Programming Environment and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view The Unix Programming Environment 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?