The PC's in CL122 are all configured to run the Linux Operating System. To log on to one of these computers, you will need to have a Computer Science Department Hercules account. Use your Hercules username and password in the dialog boxes on the Linux startup screen. Your lab instructor can help with account problems.
To program in C++, you will need a Terminal window. The top item in your desktop context menu is a a terminal. On a blank piece of desktop right click and select Open Terminal.
There should also be a Terminal icon on the menubar, and an entry in the RedHat Applications menu at System Tools->Terminal,
Once you have a Terminal window,
you can start entering Linux command right away or, you could log on to
Hercules and use Unix commands.
Either way, your files are stored in your Hercules account space.
To log on to Hercules from any Unix commandline,
enter this command:
In general you will not ssh into Hercules from this lab.
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.
From TeraTerm of Putty on Windows this commands sends the file to whatever printer is designated as your default Windows printer.
|lpr -Pcl122 file||Print the file on the laser printer in CL122.|
|cp file1 file2|
|mv file1 file2|
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.
scriptcommand 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.
script lab1.logScript started, file is lab1.log Script on xxxxxxxx%
g++ -o hello hello.cppOr on Hercules:Script on xxxxxxxx%
CC -o hello hello.cppThis step proves to the marker that your program is bug free
./helloThis 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?
GeorgeHello George! Welcome to CS115. Script on xxxxxxxx%
exitThis step exits the scripting program.exit Script done, file is lab1.log xxxxxxxx%
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.
cp /net/data/ftp/pub/class/115/ftp/cpp/hello.cpp hello.cpp
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:
cp /net/data/ftp/pub/class/115/ftp/config/.emacs .emacs
The path: pub/class/115/ftp/html The file: .emacsPLEASE avoid copying and pasting code. It makes a mess of formatting and may render the code uncompilable.
ssh herculesNote that this command needs to be entered only at the beginning of each session, not before every compilation.
g++ -o hello hello.cppOr on Hercules:xxxxxxxx%
CC -o hello hello.cpp
./helloWelcome to the world of C++ What is your name?
AdaHi Ada! Welcome to CS115. xxxxxxx%
|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.
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.
If you only have one file to compile (for instance, hello.cpp), you can use:
g++ -o hello hello.cppHowever, 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.hNote 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|
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
Tuesday, 29-Sep-2009 20:17:09 CST