CS115 Lab: Introduction to Linux, Editors, and C++ Compilation


Highlights of this lab:

The purpose of this lab is to help you develop a working knowledge of the Linux operating system and an editor. Linux is the operating system that you will use to compile and run your C++ programs.

Lab Exercise:

Click the little computer above for a detailed description.

Getting started -- logging on to the Linux Systems in the lab.

Be aware that although you will be using a Linux system in the lab, the files you will work with are actually stored in your account space on the CS Department computer, Hercules.

Accessing Linux Computers From Other Locations

If you are using a PC running the Windows Operating System in one of the other labs on campus (e.g. the University Library, 4th floor Education Building labs) use your UofR Hyperion user name and password to log in.

The university provides a program for you to use called  PuTTY Click on this link: UR Configured Software Suite to download the software. See this page to configure PuTTY for Hercules. On Windows PCs on campus (and many labs on campus), you can may find a PuTTY icon on the desktop or in Start->Programs->putty->putty . After you start PuTTY you would then click hercules.cs.uregina.ca from the Saved Sessions list, click Load then click Open.  Enter your Hercules username and password when prompted to do so.

Normally you won't do your work on Hercules. Once you are logged into Hercules type cs_clients CL122. Those numbers are the computers' names. They are also printed on the top of the computer. You can write down the name of your favorite Linux PC and ssh to it from Hercules. This is strongly recommended. If for some reason you can't use a Linux computer this lesson has instructions for compiling on Hercules.

Some people used to use the terminal emulation program called TeraTerm. Beginning September 1st, 2009, this program is not compatible with CS Department servers.

Unix/Linux Commands

Review

You should be able to fill in the descriptions for this basic set of Unix/Linux commands. We give you the descriptions of the first ones because they are implementation specific.
Command Description
TeraPrint file From TeraTerm of Putty on Windows this commands sends the file to whatever printer is designated as your default Windows printer.
  1. e.g. In CL135.4 this will print your file in CL135.4 IF you have enabled printing from the PC.
  2. Note: If you issue this command from your home PC, it will send the file to your home printer.
lpr -Pcl122 file Print the file on the laser printer in CL122.
ls  
more file  
man command  
passwd  
pwd  
cp file1 file2  
mv file1 file2  
rm file  
mkdir path  
rmdir path  
cd path  

If you are not able to complete the table above, here are a couple of references for you.
CS Lab Summary
A very helpful UNIX Tutorial.

Script Command

Some course instructors require you to submit your class assignments electronically using email. Others require you to submit assignments (and runs) in paper format. You will be advised at the beginning of the semester which method is required.

You may be asked to use the script command for putting together an assignment that needs to be handed in for class. The script command allows you to save the contents of a program run into a file, which you can then print out. Script works the same on Linux as it does on Hercules. The following example illustrates the process.

xxxxxxxx[1]% script lab1.log
Script started, file is lab1.log
Script on xxxxxxxx[1]% g++ -o hello hello.cpp

Or on Hercules: CC -o hello hello.cpp This step proves to the marker that your program is bug free
Script on xxxxxxxx[2]% ./hello
This step runs your program (the ./ says to look in your current directory for the file)
Welcome to the world of C++ What is your name? George Hello George! Welcome to CS115. Script on xxxxxxxx[3]% exit
This step exits the scripting program.
exit Script done, file is lab1.log xxxxxxxx[2]%

Transferring files.

In some of your labs, you will be asked to transfer some documents, for example, sample C++ applications from the CS Department's FTP Server, to your Hercules account space.

Transferring files from the FTP Server to the Linux systems in CL122 uses the same procedures as transferring files from the FTP Server to Hercules. i.e. If you are logged on in another lab on campus, or logged on from your home PC, you can still transfer files to your Hercules account space.

There are three methods you can use to copy these files to your Hercules account.

  1. The Unix cp command.
    You need to know the path i.e. the location of the file to be copied from the CS Department's server to use this command. We will always give you the complete path. For example the path to the file hello.cpp is: /net/data/ftp/pub/class/115/ftp/cpp so to copy the file, you would use this cp command:
     cp  /net/data/ftp/pub/class/115/ftp/cpp/hello.cpp  hello.cpp
    
  2. Use the FTP program to perform the copy. Follow the instructions in this How to use FTP link to download the sample program: hello.cpp.

  3. Use the mouse to copy and paste the contents of the file.
    This is not usually recommended. Sometimes editors or the copy buffer introduce errors into code that make it impossible to compile. For very small examples you may find it more convenient.

You should be aware that there is a command-line program that you use to transfer programs between two Unix systems. There is also a program called WinSCP that can be used to transfer a program between Unix systems and PCs. WinSCP replaces the graphical FTP program in the Computer Science Department effective December 2004. The link just given explains how to use the command line FTP, as well as how to obtain WinSCP (free) and use it.

File transfer sections in the notes will look like this:

Text Editors

There are a variety of text editors that you can use on Unix systems. The pico editor is a very simple and straightforward editor to use. More powerful editors include the vi and emacs editors. Each editor has its supporters for various reasons. The Professor's choice of the editor for the current semester will be given to you by your lab instructor.

Here is a summary introduction to each of these editors:

How to Compile and Run C++ Programs.

Now let's look at how to compile a C++ program in the Linux environment. In order to clearly understand the compilation process you must be aware of two things:
  1. Each Linux system in the lab is an individual processor with its own C++ compiler, called the GNU C++ compiler.
    You call up this compiler with the g++ command.

  2. The Hercules computer (where your files are stored even when you are using the Linux systems) has its own compiler.
    You you call up this compiler with the CC command.

    If you want to use the Hercules compiler when you are on a Linux system in a CS lab, you first need to enter the command: ssh hercules Note that this command needs to be entered only at the beginning of each session, not before every compilation.
Let's suppose you had successfully transferred the file called hello.cpp from the CS Department's FTP server. The commands to compile and run this program are as follows:
xxxxxxxx[115]%  g++  -o hello hello.cpp 
Or on Hercules: CC -o hello hello.cpp
xxxxxxxx[116]% ./hello Welcome to the world of C++ What is your name? Ada Hi Ada! Welcome to CS115. xxxxxxx[117]%

Compile Command

g++ hello.cpp -o hello     Or only on Hercules:     CC hello.cpp -o hello

If we break this down, we can see what each part of the command does.

g++ This is the command that says "Compile my C++ program by using GNU C++ compiler."
CC This is the command that says "Compile my C++ program by using Sun's C++ compiler on the Hercules computer."
Important Note!: A file that is compiled with the CC command cannot be run when logged onto a Linux system unless you have first entered the ssh hercules command to connect to Hercules.
hello.cpp This is the name of the C++ program.
Notice that you need to use the extension .cpp
-o hello This is called the "minus-oh" option.
It tells the linker to create an executable file as specified by the name following -o. It is a convention to name your executable file the same name as the source file without the .cpp extension.
In this example: source file is hello.cpp and executable will be hello.
What happens if you don't use the minus-oh option?
Then your executable file will, by default, be called a.out which is ok, but every time you compile another C++ file, you would overwrite your a.out file.

There are many different C++ compilers. There's at least one for every operating system, and usually more. They all have little quirks that make them slightly different. When you switch compiler you will need to pay attention to the error messages it produces and test your software to make sure it still works as expected. If you can't run a program that you previously compiled check to make sure you are running it on the same type of computer it was compiled on. Programs compiled on Hercules won't run on Linux machines. Programs compiled on Linux machines won't run on Hercules. You will need to recompile or switch machines.

Run Command

./hello

Is that all??   Yes, actually, that is all there is to it. Just type in ./ (so that Unix/Linux knows to look in your current directory for the file) and then the name of the executable file. Following the example just given, the name of the executable file is hello. If we had not specified the -o hello option, then the executable would have been named a.out and you would have had to type in ./a.out to run the program.

You might not be used to typing in the ./ prefix to specify the current directory. That is because here in the CS Department your current directory has been added to your path via your login script. However in any system, if you don't specify the ./ and have an executable file with the same name as a system program (and there are thousands) the system will run its program rather than yours. Try this out if you like - compile hello.cpp and name the executable "switch" and then try running it. Bottom line: Always specify the ./ prefix when running your executable because your current directory is not always in your user path in all Unix/Linux systems.


Separate Compilation

If you only have one file to compile (for instance, hello.cpp), you can use:

g++ -o hello hello.cpp
However, in this lab, you will be more often working with multiple files

Let's work with an example. You can get three files by entering the commands:

cp /net/data/ftp/pub/class/170/ftp/cpp/SeparateCompile/main.cpp main.cpp
cp /net/data/ftp/pub/class/170/ftp/cpp/SeparateCompile/myFunction.cpp myFunction.cpp
cp /net/data/ftp/pub/class/170/ftp/cpp/SeparateCompile/myFunction.h myFunction.h
Note that both main.cpp and myFunction.cpp each have a reference to the .h file myFunction.h. You will have to compile each of these files separately to produce .o files and then link them all together. The following commands show how to do this. Notice the -c option you need to specify when you want to compile only, not to compile and link.

Compile only: g++ -c main.cpp
Compile only: g++ -c myFunction.cpp
Link: g++  main.o   myFunction.o  -o main
If you wanted to compile a whole set of C++ programs at the same time, you could enter: g++ -c *.cpp Be careful with this though, for these reasons:

To link a whole set of object files you could have just entered: g++   *.o   -o main Again, you should be careful that you have all your files in one directory.

The following diagram illustrates how the previous example appears conceptually:

Looking Ahead!

If you want to save your compilation errors to a file, you can use the following command:
g++ -o hello hello.cpp >& errors.txt
OR
if errors.txt already exists and you want to overwrite the file, use:
g++ -o hello hello.cpp >& ! errors.txt

Lab Exercise:


This page last modified:
Tuesday, 29-Sep-2009 20:17:09 CST

CS Dept Home Page
CS Dept Class Files
CS115 Lab Files

Copyright: Department of Computer Science, University of Regina.