I do not care what programming environment you use. However, keep in mind that during the course you will be working under Linux. In addition, assignments have to be handed in such a way that they can be build using GNU make and the GNU C++ compiler (in the GCC package). You should thus have at least a minimal familiarity with these applications.
Programs may be composed and edited using any text editor, such as notepad on Windows. In any case, make sure that you have a good text editor. A programmer's editor is a very different thing from a word processor. Most importantly, it saves your work in plain text files and it doesn't insert extra carriage returns beyond the ones you actually type (so, Microsoft Word does not qualify). A good programmer's text editor will do a lot more than this, including:
There are many text editors that have some or all of these features. The most widely used editors on UNIX systems are vi and Emacs (or a variant called XEmacs). They are also subjects of a long standing holy war. Various ``modes,'' supporting various programming languages, exist for both of them.
You are of course free to program in any editor you like (including ed). For this course though, I recommend either XEmacs or Kate (they are both installed in J118).
My personal preference is XEmacs, but in honesty it does not have such a good C++ support as it does for Java, Prolog, or Lisp. It does though offer auto indentation, syntax highlighting and so on.
The disadvantage of using XEmacs is that XEmacs interface is quite
different from anything you have probably seen. While most commands
you are likely to use are accessible from menus or the toolbar,
keyboard shortcuts do come handy, and XEmacs' are quite different from
a usual Windows environment. Here is a list of the most used commands
and their shortcuts (in Emacs-speak, C
stands for Control, and
M
stands for Meta; the Meta key is usually bound to Alt, but
Esc can be used as well: M-x
stands thus for pressing (and
holding) Meta/Alt and then pressing x, or pressing (and
releasing) Esc and then pressing x):
Command | XEmacs-speak | Shortcut |
---|---|---|
Open | find-file | C-x C-f |
Save | save-buffer | C-x C-s |
Save as | write-file | C-x C-w |
Copy | kill-ring-save | M-w |
Cut | kill-region | C-w |
Paste | yank | C-y |
Quit | kill-emacs | C-x C-c |
XEmacs function call | M-x function-name |
During the labs, you will work under Linux, which is one of the many flavours of Unix. The major difference between Linux and Windows is that one way of interaction with the system happens through a command line interface (CLI), much like the DOS prompt but much more powerful. You have however the usual window system (a bunch of them actually) at your service.
The following is a quick guide to what you have to do to write, compile, and run a program.
Once you log into a machine in J118 you end up with a desktop in
front of you similar to the desktop of a Windows workstation. There
exists on the desktop an icon called ``Terminal.'' Double click on
it, and you will obtain a window featuring a prompt looking
like this: [bruda@j118-18 ~]%
. Unless otherwise noted, the
remainder of these steps will be accomplished by typing commands in
this window which is henceforth called ``the terminal window.''
It is easier to manage things if you create a separate directory for
each of your programs. Let's say that you want to put your program
in a directory called foo
. First, you create foo
:
mkdir foo
and then you change directory to foo
:
cd foo
Also see the note on directories.
First, you open an editor (say, XEmacs) to create your program.
Let's say that your decide to call your program foo.cc
. Type
then:
xemacs foo.cc &
Another window will open. This is an editor, so go away and write your program then click ``Save.''
If you prefer Kate to XEmacs, substitute kate
to
xemacs
in the above command.
Get back to the terminal window, and type
g++ -g -Wall -o foo foo.cc
If no errors appear, you will end up with an executable called
foo
. If the name foo
is not to your liking, change
the above command. For example, if you want to call your executable
big_hairy_program
, then issue the following command instead:
g++ -g -Wall -o big_hairy_program foo.cc
If you wonder about the meaning of -g
and -Wall
, see
the section on manual pages.
To list a directory, type
ls
This will display the name of the files that reside in the current directory. If you want a more detailed listing, type instead
ls -l
On most Linux systems, you can also type
ll
If you want more information about ls
, see the section on
manual pages.
Assuming that the name of the executable is foo
, type:
./foo
Your program will do its stuff (why ``./
''? see the
note on directories).
So you wrote this infinite loop and your program just stays there and runs happily no matter what you do. Most programs can be actually stopped by going to the terminal they run into and typing control-c (i.e., press and hold control and type c).
You also need to change your program from time to time (let's say that it won't compile). To do this, go to the editor window, do whatever changes you wish, and click again ``Save.'' Repeat then from Step 4.
Let's say that you want to create a new program based on an existing one (which you do not want to destroy). In this case, the first thing to do is to copy someplace else the original program. You may also want to move files around.
To move the file or directory source to the directory or file dest do:
mv
source
dest
Both source and dest can be either relative or absolute (see the note on directories for the meaning of absolute and relative). For instance,
mv foo.cc foo.cpp
will move (rename) the file foo.cc
in the current working
directory to foo.cpp
, and
mv foo.cc ~/courses/cs318
will move foo.cc
to the directory ~/courses/cs318
.
To copy a file file to the directory or file dest do:
cp
file
dest
Again, both file and dest can be either relative or absolute.
To delete a file file (given with relative or absolute path) do:
rm
file
See the manual pages of
cp
and mv
for more options.
Caution. If you move or copy a file over another file with the same name, then the latter will be lost. There is no way in Unix to recover overwritten or deleted files.
In Unix, all the files accessible on a given machine reside in a
directory tree whose root is called /
(``the root directory'').
This is similar to Windows but not the same--indeed, on a Windows
machine you have a directory tree for each disk rather than for
the entire machine.
At any time, one directory is the working directory. You could find the current directory by typing in a terminal:
pwd
All of you files reside in a directory called ``home directory,''
which is of course somewhere inside the /
directory. This is
your working directory when you log into a Unix machine.
You could change to any directory by using the cd
command. You
could give an absolute or relative path to the new
directory. The following is an example of an absolute path:
cd /usr/local/bin
will change the working directory to /usr/local/bin
(meaning
``the subdirectory bin
of the subdirectory local
of the
subdirectory usr
of the root directory''). You could also
specify the new working directory relative to the current working
directory by ommitting the first /
. For example, the command:
cd usr/local/bin
changes the current directory to the subdirectory bin
of the
subdirectory local
of the subdirectory usr
of the
current working directory.
To make your life easier, there are some special directory names:
Name | Meaning |
---|---|
~ |
your home directory |
. |
the current directory |
.. |
the parent of the current directory |
Most Unix commands and C functions are documented in manual
pages. To obtain the documentation for a Unix command (say,
ls
) go to a terminal and type:
man ls
Similarly, the documentation for g++
, the GNU C++ compiler is
obtained by typing in a terminal
man g++
The manual pages are organized in sections. You can find manual pages for publicly available commands (Section 1), system services (Section 2), library functions (Section 3), and so on. When you type
man time
the manual pages will be searched for an entry called time
starting with Section 1, and the first occurrence will be displayed.
In this particular case, there exists an entry for time
in
Section 1, so this is what you will end up with. However, there also
exists a manual page for time
in Section 2. How do you reach
it? By passing a section number to the command man
using the -S
switch. For example,
man -S2 time
will display the documentation for time
found in Section 2
(-S2
).
Manual pages are searcheable. To find pages containing the string
time
do:
apropos time
If you want to look for complete words only, do:
whatis time
For more information about what I just wrote, use the...(drum roll) manual pages, i.e., issue the following commands:
man man man apropos man whatis
Here are some tutorials that offer a quick introduction to Unix.
So it's past 11 pm, the lab is closed, but you have to submit this assignment for CS 318. Well, you actually can.``Remote login'' (i.e., having access to a machine from a remote location) is a built-in facility in Unix.
ssh
username@linux.ubishops.ca
where username is your J118 user name Once presented with a
prompt on the remote machine you can launch any application you like
(such as submit
), including graphical applications (whose
windows will show up on your screen--the Unix SSH client does
display forwarding for you automatically).
Under Windows you need an SSH client such as
putty.
Launch then the client, enter linux.ubishops.ca
in the
address/host field (don't remember the exact name of the field,
sorry), and make sure you check the ``SSH'' radio button. Log then
in using the same user name and password you use in J118, and you
will end up with a terminal similar to the one you have used
already. No graphical interface is available in this setting, but
you can open as many terminals as you wish by repeating this
connection procedure. This should at least allow you to use the
submit
program.
scp
and works almost like cp
except that either the source or
the destination may be on a remote machine and specified under the
following form:
@
machine-name:
remote-directory
with username the name of your account on the respective
machine, machine-name the name of the machine you want to
copy from (or to), e.g., linux.ubishops.ca
, and
remote-directory the thing you want to copy (or into which
you want to copy, as the case may be). Use the -r
switch to
recursively copy entire directories.
A graphical SCP client for Windows is WinSCP.