This presentation describes writing, compiling, and running C++
programs while logged into a UMD Computer Science Department server.
You can log into the server from your own machine, or from a machine
in a UMD lab (such as MWAH 177).
Once logged in, you will use a
text editor to write your program
and the
g++ tool to compile and link your program.
All students enrolled in Computer Science courses have access to and
file storage on a server maintained by the department called
"
janus":
- janus runs Linux, a Unix-like operating
system.
- Since the internet domain name for UMD is "d.umn.edu," the
full name of janus is "janus.d.umn.edu".
- To log in to janus from your own machine, you may need to
install the Secure Shell communication software.
-
Note: When logging in, the entry of your username and password
are case sensitive. E.g. if your username is jsmit123
then be sure to enter "jsmit123" and not
"JSMIT123". Same for your password.
Lab machines have communication software already installed that
supports logging into servers that are connected to the internet. To
use:
- Access the UMD network by providing your username and password
(these are the same credentials you use to access your UMD
email)
- Launch the desktop program Apps4U to get a list of
installed applications
- Find and launch the application called PuTTY
- Connect to the host janus.d.umn.edu — you may need
to click through some warnings having to do with host registration
Secure Shell is a suite of programs allowing computers to
communicate with one another securely over the internet.
The
ssh client program allows a user of computer
A to log
into and use computer
B so long as the user is authorized to use
computer
B.
- If your computer's operating system is Linux or Unix, it is
likely that ssh is already installed. If not you can
download and install it from here:
https://www.openssh.com/
Alternatively, if you are familiar with package managers you can
use apt-get to install the package openssh-client.
To launch, open a terminal window and type
"ssh janus.d.umn.edu".
- If your computer's operating system is Mac OS X, which is based
on Unix, just open the Terminal application and type
"ssh janus.d.umn.edu".
- If your computer's operating system is Microsoft Windows,
see Windows in the menu at left.
For Windows machines, there is an implementation of the
Secure
Shell suite called
PuTTY. Here is the URL from which you can
download and install:
http://www.chiark.greenend.org.uk/~sgtatham/putty/
After installing, run the
PuTTY program.
See the menu at left for the process of connecting and authenticating.
Start the application and enter "janus.d.umn.edu" for the host.
Leave the rest of the settings as is and click
Open.
You will be presented with a terminal window through which you will
enter your UMD username and password:
After successfully authenticating, you will see a system message and
prompt. Now you are ready to execute Unix commands.
Once logged in to
janus, you will interact with a
command
line interpreter (CLI). Below are some typical commands. Note
that "directory" is another word for "folder".
- pwd — print the name of your current working directory. When you
first log in you will be in your
home directory, whose name will include your username.
- mkdir dirname — creates a new directory with
the given name. This becomes a subdirectory of the current working
directory. If the current working directory's name is cur,
the new subdirectory is called cur/dirname.
- rmdir dirname — removes the named directory
if it is empty.
- cd dirname — changes to the directory with
the given name. This becomes your current working directory.
- ls — list the files in your current working
directory. These can include subdirectories and regular files.
- rm filename — removes the named file.
- zip dirname.zip dirname — compresses a named
directory into a "zip" file. (If there are subdirectories use the
"-r" option.)
- unzip dirname.zip — uncompresses a "zip" file
into a directory.
- exit or lo — logs out of the Unix session.
Regular files (files that are not also directories) include the files
containing your C++ programs. To create and develop these files, you
will use a
text editor.
Here are three text editors available on
janus:
- nano — easiest to learn. Here is a
Beginner's Guide
- vi or vim — powerful but requires
distinguishing insert mode from command mode. Here
is A Tutorial.
- emacs — a full programming environment with a steeper
learning curve. Here is
a Guided Tour.
Of course, there is much more documentation for each of these editors
online.
Whichever you choose:
- Launch the editor by typing its name
into the command line
- Type the program into the editing area, or, if you are
completing a program you have downloaded for a lab or assignment,
transfer the file from your machine to janus
(see Transferring Files in the menu to the left).
- When you are finished, be sure to save your changes. If it is a
new file, you will have to give it a name.
- Exit the editor. Now you can compile and run the program.
If you have a program file (or files) on your own machine that you need to
transfer to
janus, there are two options:
- Copy and paste text between windows
- Use a file transfer client
For a single file or a small number of files, the procedure below will
suffice.
Suppose the file on your machine to be transferred is "
main.cpp":
- Open main.cpp in a simple file viewer on your machine
- Select the contents of main.cpp using the Select All
feature of your machine's window manager (also ctl-A), then
copy the contents into your window manager's clipboard using
the Copy feature (also ctl-C)
- Launch the PuTTY, SSH, or Terminal
communication application (depending on your platform)
- Connect to janus.d.umn.edu
- Launch your editor of choice on janus
- Paste the contents of your clipboard into the editor
using the Paste feature of your window manager (also
ctl-V, but not for emacs)
- Save the contents of your editor as main.cpp
The copy and paste method of getting files to
janus is not
feasible when there are many files, or when a file is not a standard
text file, as with "zipped" files (folders or directories that have
been compressed into one non-text file).
In that case, you can use a communications program, called a
file
transfer client, to transfer the file(s).
- The OpenSSH suite, used by Unix, Linux, and
Mac OS X, includes the scp
and sftp file transfer programs.
-
The PuTTY installation for Windows includes the PSFTP
file transfer program. See the menu at left.
Running the
PSFTP program produces a terminal window in
which you can type various commands. In the screenshot below, the user
has typed "
help" to see a summary of the available commands.
In the example below, the user uses the following commands to transfer
the file
exercises.cpp from his Windows desktop (local host) to a
directory on
janus (remote host):
- open: to connect to the remote host and login
- cd: to change to the cs1581 directory
on the remote host
- lcd: to change to the Desktop folder on the local
host
- !dir: to list the files on the local host
- put: to send the file exercises.cpp from the local
host to the remote host
- quit: to end the session
A free and open-source file transfer client is available
from the
FileZilla project at
https://filezilla-project.org/.
For details, see the menu at left. The
FileZilla
site has download links and documentation on how to use the
client.
Below is a generic screenshot of FileZilla's main window. (The
screenshot is from Windows, but the presentations for Linux and Mac OS
are similar.)
For your usage, enter:
- janus.d.umn.edu into the Host
field
- your username and password into the Username
and Password fields, and
- 22 into the Port
field
Your machine's file system will be shown under
Local site,
and your home directory's file system on
janus will be shown
under
Remote site.
You can then move files between the file systems.
In this section we show how to invoke the C++ compiler, interpret
compiler errors, and compile programs involving multiple files.
Suppose your current directory contains a C++ source file
main.cpp, whose listing can be seen on the right:
43% ls
main.cpp
A program on
janus called
g++ invokes a C++ compiler and
linker on source files. By default,
g++ produces an
object
file called
a.out:
44% g++ main.cpp
45% ls
a.out main.cpp
a.out is an executable file that can be run directly:
46% a.out [program invocation]
What is your name? [program output]
Boo [program input]
Boo who? [program output]
Note: You may need to execute
a.out with the command:
46% ./a.out
The reason for this involves your
path environment variable,
which is beyond the scope of this presentation.
If a C++ file does not have the correct syntax,
g++ will report
an error.
To the right is
main.cpp with an error. Can you spot it?
When there is a syntax error,
g++ will provide the line number
(
17
in this case) and the character position within the line (
30) where
the error occurs:
47% g++ main.cpp
main.cpp: In function ‘int main(int, char**)’:
main.cpp:17:30: error: ‘end1’ was not declared in this scope
cout << name << " who?" << end1;
^~~~
main.cpp:17:30: note: suggested alternative: ‘enum’
cout << name << " who?" << end1;
^~~~
enum
Note that the compiler "suggestions" are not always informative.
main.cpp
interact.H
interact.cpp
Often, a C++ program will be distributed across multiple files to
facilitate
separate compilation.
For example, the
main.cpp file could be organized into the three
files
main.cpp,
interact.H, and
interact.cpp,
whose listings are shown to the right.
48% ls
interact.cpp interact.H main.cpp
Since
main.cpp calls a function defined in
interact.cpp,
both need to be compiled:
49% g++ main.cpp interact.cpp
50% ls
a.out interact.cpp interact.H main.cpp
The same effect can be achieved by:
49% g++ *.cpp
which uses the file name "wildcard"
*.
In either case,
a.out is the final executable program that
results from "linking" the programs specified by
main.cpp
and
interact.cpp.