This course is primarily based on Linux, one of the many flavours of Unix. Programming in this course happens in principle in C++, but I am also accepting (and supporting) C.
This section sketches the programming process under Linux. Read it carefully, you will need all of this for your programming assignments.
There is a huge amount on information on the Web about C++ programming (and programming in general). Here are some of these documents:
Note that the makefiles presented in class are good for GCC. In Windows, the name of the compiler should be changed, as well as the compiler options (in particular, options are introduced in Windows by
-). In addition, the
makecommand supplied with Microsoft Visual C++ appears to be actually called
nmake. Consult the documentation that comes with your system for details.
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/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):
|XEmacs function call||
During the labs, you will work under Linux. 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. The guide assumes that you write C++
code; if your code is actually C, the guide still applies with the
following changes: (a) source files have extension
.cc, and (b) you should use
gcc instead of
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
[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
and then you change directory to
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
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
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
the section on manual pages.
To list a directory, type
This will display the name of the files that reside in the current directory. If you want a more detailed listing, type instead
On most Linux systems, you can also type
If you want more information about
ls, see the section on
Assuming that the name of the executable is
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:
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
mv foo.cc ~/courses/csc414
foo.cc to the directory
To copy a file file to the directory or file dest do:
Again, both file and dest can be either relative or absolute.
To delete a file file (given with relative or absolute path) do:
See the manual pages of
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 can find the current directory by typing in a terminal:
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 can change to any directory by using the
cd command. You
can give an absolute or relative path to the new
directory. The following is an example of an absolute path:
will change the working directory to
bin of the subdirectory
local of the
usr of the root directory''). You could also
specify the new working directory relative to the current working
directory by omitting the first
/. For example, the command:
changes the current directory to the subdirectory
bin of the
local of the subdirectory
usr of the
current working directory.
To make your life easier, there are some special directory names:
||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:
Similarly, the documentation for
g++, the GNU C++ compiler is
obtained by typing in a terminal
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
the manual pages will be searched for an entry called
starting with Section 1, and the first occurrence will be displayed.
In this particular case, there exists an entry for
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
-S switch. For example,
man -S2 time
will display the documentation for
time found in Section 2
Manual pages are searchable. To find pages containing the string
If you want to look for complete words only, do:
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 CSC 414. Well, you actually can.``Remote login'' (i.e., having access to a machine from a remote location) is a built-in facility in Unix.
where username is your J118 user name. Once presented with a
prompt on the remote machine you can launch any application you like
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
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
scpand works almost like
cpexcept that either the source or the destination may be on a remote machine and specified under the following form:
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.,
and remote-directory the thing you want to copy (or into
which you want to copy, as the case may be). Use the
switch to recursively copy entire directories.
A graphical SCP client for Windows is WinSCP.