cp .cshrc .cshrc-original
This copies the file .cshrc to a new file called .cshrc-original. After
giving this command, list your files again, and you should see both
.cshrc and the copy that you made, .cshrc-original. Now, if you really
mess up your .cshrc file, you'll still have the original version sitting
around so that you can easily restore it to its pristine condition.
Before moving on, there's one more file that you should copy in order to
set up our editor so that it can deal with C++ programs
in a nice way. To do this, you should copy the editor's initialization
to your home directory. The file in my directory
is called ".emacs"
(with a period as part of the file name), and you want to download it
from
www.portmain.com/ds/labs/.emacs
to your
own home directory.
Now that you've downloaded this file, all future editor sessions will treat
C++ programs nicely (creating nice indentation and treating the backspace
key correctly).
You can now move to the next step, where you'll modify .cshrc .
Using GNU emacs
In order to modify your .cshrc file--or to modify any other file--you'll
need to use one of the text editors. Some of your have probably used a
Unix text editor before, such as vi, or pico, or even emacs. In the past,
we have let students use whichever editor they wanted. But this
semester I'd like everyone to use an editor that has a good degree of
integration with a compiler and other tools. The editor is called GNU
emacs (yes, the G in GNU is pronounced!), and you have already copied
the .emacs initialization file to your home directory.
In order to start emacs and edit the .cshrc file, give this command:
emacs .cshrc
The editor will open, and display the current contents of your .cshrc
file. Spend one minute using the terminal's arrow keys to browse through
the file. You can also use the commands to page down (Ctrl-V) or page
up (press ESCAPE and then V). Each line of the file tells the
operating system something about how you like to have things set up. For
example, one of the lines in my own .cshrc file is this:
alias copy /bin/cp
This line tells the operating system that whenever I type the command
copy
, I really want to use the usual cp
command (which is located in a
spot called /bin/cp). Notice that the location of files and commands uses
a forward slash "/", rather than the backward slash "\" that you might be
used to from a PC.
If you like, you may add the alias line shown above to the end of your
.cshrc file. Just move the cursor to the end of the last line, press
return and type the alias lines. Then save the file before exiting.
Subdirectories
You could keep all your files in one directory--your home directory, which
is the location where the machine goes when you log in. That approach is
sometimes called the Michael Main approach to organizing your files. But
in truth, even Michael Main now uses about a dozen directories to organize
his different files. For your work in 2270, please create a subdirectory
called 2270. In this subdirectory you can create further subdirectories
to store each week's work. So, to make a new subdirectory called 2270,
give this command:
mkdir 2270
Next, you can move down to this directory, where you will start today's
real work. The command to change to a subdirectory (the 2270
subdirectory) is:
cd 2270
Sometimes, when you are moving through directories, you forget exactly
where you are. The Unix command pwd will print the working directory. If
you give this command now, then the current directory will be printed,
which should end with "/2270".
Actually, rather than putting your work right here in the 2270 directory,
why don't you make one deeper subdirectory called labs, and then change
to that subdirectory. Down in this deeper subdirectory, the pwd command
should print something that ends "/2270/labs".
Downloading a Program
The next step will ensure that you know how to compile, link, and run a
C++ program. Even if you already know this, go ahead and go through the
steps--it won't take long and you'll probably pick up at least one tip.
Start by downloading a short program into your current directory
(/2270/labs). The program, called heatwave.cxx
can be downloaded from
www.portmain.com/ds/labs/heatwave.cxx
By the way, we'll always use the filename extension .cxx to
identify a C++ program, although others use .cpp or other extensions.
Compiling a Program
You'll be using the GNU C++ compiler. This compiler is called g++, and you
can compile heatwave.cxx with the simple command g++ heatwave.cxx—but we
suggest that you use a more complex C++ compile command:
g++ -Wall heatwave.cxx -o heatwave
The switch -Wall instructs the compiler to list all warning messages.
These warning messages usually indicate programming errors, and we'll
forbid warning messages in your programs. At the end of the compilation
line, the arguments "-o heatwave" indicate that the compiler should put
its result (the "object code") in a file called heatwave. If you don't
specify a location for the object code, then the compiler places its
result in a file named a.out, which is not a particularly useful name.
Anyway, compile the heatwave program now (as shown above), and then list
your files again. But this time use the long form of the list command:
ls -l
This provides more information about each file. The information on the far
left is particularly useful. For example, next to the object code file
heatwave are some symbols such as -rwxr-xr-x. Here's how to decrypt these
symbols: The first minus sign means that this is a file (rather than a
directory). The next three symbols (rwx) mean that you can read, write,
and execute the file. The next three symbols (r-x) mean that other people
in your own group can read and execute the file, but not write it. Your
"group" probably consists of all other undergraduate students. The last
three symbols indicate what other people can do, even if they are not in
your group.
Running a Program
Anyway, the important thing now is that heatwave is an executable file,
meaning that you can run it by typing its name followed by a
period and a slash, like this:
./heatwave
.
Do this now. Type
./heatwave, press return, and then interact with the program (which
computes how long a tree will heat a house).
Emacs Indenting
When emacs knows that a file is a C++ program, it can help
you to make sensible indentation. To demonstrate this ability,
use emacs to open up heatwave.cxx, go down
into the body of heatwave.cxx, and mess up the indentation on a few lines.
Get rid of some spaces or add some extra spaces at the start of a line.
Just mess it up some way.
Then press Esc-p. The letter p stands for "pretty-print," and the
command will nicely indent your entire program.
This command is one of the things that I set up in your .emacs file
(normally Esc-p just moves the cursor to the previous line.)
Here are four other indentation items that I have set up in the
.emacs file:
- The TAB key: When you press TAB, emacs will attempt to
correctly indent the current line.
- The RETURN key: When you press the RETURN key, emacs will
indent the current line, insert a blank line, and move the
cursor to the right spot to start typing. If the first
character of the newline is a bracket, then its indentation
will be corrected after you type the bracket.
If you find that you need to turn off the annoying behavior
of the RETURN key, then go into your .emacs file and put
three semi-colons at the front of the lines that look like this:
(local-set-key [13] 'tab-return-tab)
- The Pretty-Printer: When you press Esc-p in a C or C++ program,
emacs will nicely indent the entire file.
Now that you know about pretty-printing, you can exit
the editor and move to the next step.
Compiling from within Emacs
At the moment you are looking at heatwave.cxx from within emacs. You
can actually compile heatwave.cxx without stopping the editor. To compile
from within the editor, press the escape key once, press x, type the word
"compile", and press return. The "mini-window" at the bottom of the screen
will change to this message:
Compile command: make -k
Use the backspace key to erase "make -k", and instead type your usual
compilation command, so that the mini-window looks like this:
Compile command: g++ -Wall heatwave.cxx -o heatwave
Then press return. When you press return, the screen will split into
two pieces. The top piece is still your heatwave program. The bottom
piece is a new window that will contain any error messages from the
compilation. During the compilation, there will also be a message
near the bottom of the screen indicating "Compilation: run Compiling".
When the compilation finishes, the message changes to "Compilation:
exit[0]". If there were no compilation errors, you can get rid of
the compile window by giving the "one window" command (Ctrl-X followed
by typing the digit 1).
Finding Compilation Errors
Compiling from within emacs is a big help when there are compilation
errors. For example, let's put a compilation error into the
heatwave.cxx file. Go down to the input line "cin >> height;"
and delete
the semicolon at the end of the line. This is certainly an error!
Move the cursor away from this error before you proceed to the next
paragraph.
Now, type the compile command again (ESCAPE x compile RETURN). Notice that
when you press return, the miniwindow still remembers your compilation
command, so just press return once more to get the compilation underway.
When the new compilation starts, emacs will ask whether you want to
save the changes to heatwave.cxx before compiling. You should answer
y (otherwise the compilation would compile the old version of
heatwave.cxx, which did not have an error).
The compilation will find your syntax error pretty quickly, and display
some error message "on line 42".
With the error message in the compilation window, you can ask emacs to
move the cursor to the location of the error. The emacs "find error"
command is CTRL-x followed by the "backquote" key. (The backquote key is
the single quote mark that seems to go backward.) Give this command now.
Emacs will move the error message to the top of the compilation window,
and move the cursor in the program to line 42. Line 42 is actually one
line after the real error. (Remember that usually you must move backward
from the error line to find the location of the actual syntax error.)
Go ahead and fix this syntax error now, and quit emacs (saving the file).
More About Emacs
As the semester progresses, you'll learn a lot more about emacs. For
example, you can change the definitions of various keys to match
your own preferences for editing operations. You can invoke a debugger
from within emacs. For now however, a
summary of basic editing commands
should be sufficient.
Later you might find it useful to also browse the
emacs online manual.
Separate Compilation and Linking
Small programs can be written in a single file, such as heatwave.cxx.
Larger programs should be broken into manageable pieces. Each piece can
be developed, compiled, and tested on its own--all in a manageable size.
Normally, such a program will have one file that is the main program, and
one or more "modules" that provide something for the main program to use.
Each module comes in two parts: (1) a header file that provides a
prototype for each of the module's functions, and (2) an implementation
file that provides the actual implementations of the module's functions.
You'll see this separation over-and-over again in our class. As a first
example, you can grab such a module from the class's program directory.
You'll need to download the header file
(www.cs.colorado.edu/~main/chapter2/throttle.h)
and implementation file
(www.cs.colorado.edu/~main/chapter2/throttle.cxx)
to your working directory.
Separate Compilation of the throttle Module
The throttle module provides some functions from Chapter 2 of the textbook.
You can browse through the header file to see exactly what's
present. After browsing for a while, exit your editor and give this
compile command:
g++ -Wall -c throttle.cxx
The -c switch tells g++ to compile only, and not to create an executable
file. In fact, you can't create an executable file because throttle.cxx
does not have a main function. When a module is compiled like this, with
no main function, you need the -c switch. Also, there is no need to use
the -o switch, since you are not creating an executable file.
With the -c switch turned on, throttle.cxx is compiled, and the resulting
code is placed in a nonexecutable file named throttle.o. After your
compilation, do a long listing to make sure that throttle.o is present.
A Program That Uses the throttle Module
Once the throttle module has been compiled, any program can use the items
it provides. I've written a program that uses the throttle module. Copy
it to your directory now. It's in the file
www.cs.colorado.edu/~main/chapter2/demo2.cxx
.
Download the file and use the editor to browse through the file.
You might not understand all of the program, but notice the include
directive:
#include "throttle.h"
This include directive is all that's
needed in the code of the program that uses the throttle module.
Notice that the throttle include directive uses quotation marks rather
than angle brackets. The angle brackets (such as #include <iostream>)
are used only for one of the standard header files. The quotation marks
are for one of your own header files. (The compiler looks for these two
different kinds of header files in different places).
Anyway, you can now quit the editor and compile the demo2 program, with
the compilation command:
g++ -Wall -c demo2.cxx
This compiles demo2.cxx, but it does not yet create an executable file.
Instead, it puts the compiled code in a file named demo2.o. In the next
step, you'll put all the pieces together, finally creating an executable
file.
Linking Several Pieces Together
Your total executable demo2 program will include three pieces: (1) The
compiled code for the throttle module, (2) the compiled code from
demo2.cxx, and (3) some compiled code for mathematical functions that
are part of the C++ math library. (You might have noticed the use of the
math library in demo2.cxx by the include directive #include <cmath>.)
We can put all three pieces together with one last compilation command:
g++ demo2.o throttle.o -lm -o demo2
We did not use the -Wall option because we are not compiling any new
code--just putting together previously compiled pieces. The -lm picks up
the library of math functions. And at the end, the -o option tells the
compiler where to place the executable file that it creates. After this
compilation you'll have an executable file named demo2, and you can
run it. Give it a try now by typing the command
demo2
Printing a File
Please ask your TA for current directions on how to print files
from the lab.
Remote Access
Part of the reason that we are asking you to use the emacs editor
is that compilation and debugging can be done through the emacs
interface even when you are logged on from a remote site such as
your home PC. In order to log on from a remote site, you will
need a program that allows "secure shell" (ssh) access. I use the
ssh program that CU distributes for free to students at
www.colorado.edu/its/docs/authenticate/win_ssh.html. When you
run this program, you can click the quick connect button and use
these settings:
- Host name: csel.cs.colorado.edu
- User name is your identikey
- Port Number: 22
- Authentication method: Password
The ssh package also includes a program called sftp that allows
you to easily transfer files back and forth from your PC to the
csel machine.