Hi everyone. Welcome to CS 54. This lab is designed to orient you with the systems we will be using here at UMR as well as how to handle common problems you may run into along the way. This is a very important lab, so keep up and don't hesitate to ask even the slightest question!
We will be working in a UNIX environment using a program called PuTTY. The UNIX filesystem, like the Windows filesystem consists of a hierarchy of directories, files, and links. The UNIX filesystem hierarchy is quite complex and you will have the opportunity to further explore it as you go along. Here are the parts of the hierarchy you should know for now:
| Path | Comment |
| / | "Slash" - the mother of the entire hierarchy. Everything can be traced back here. |
| /nethome/users/username | username's DFS drive. This is the same drive you see from Windows. This is where we'll be working and will be called "home." |
There's lots more to explore, but we can discover that as we go along.
In order to work with the compiler for the rest of the semester, we will be using a program called PuTTY to connect to the UNIX machines. The name of the UNIX servers are dellX.cs.umr.edu, where X is an integer from 7 to 22. Choose an integer share it with your neighbor; make sure you both have different numbers. This way, no one machine gets too overloaded. For instance, suppose you chose 8. Type in dell8.cs.umr.edu as the hostname and click "Open":
![[PuTTY Configuration]](putty_config.png)
If this question pops up, say yes:
![[Key Verification]](putty_key.png)
After entering your username and password, your screen should look something like this:
![[PuTTY Screen]](putty_screen.png)
If the last line of the screen says bash-3.00$ and there are no glaring error messages, then you're in good shape. You are ready for the next part of the lab.
We will be demonstrating and practicing all these commands in class, but here's a quick reference of each command we will use:
| Command | Comment |
| mkdir dir | Makes a directory called dir. |
| pwd | Print Working Directory. |
| cd dir | Change to directory dir. |
| cd .. | Change to directory one directory above. |
| cd ../.. | Change to directory two directories above. |
| cd ~ | Change to your home directory. |
| cd | Change to your home directory. Equivalent to the previous entry. |
| ls | List files in the current directory. |
| ls -l | List files in the current directory (Long Format). |
| cp source dest | Copy file source to destination file dest. The original file is preserved. |
| mv source dest | Copy file source to destination file dest. The original file is not preserved. |
| rm file | Delete file. Once a file is deleted, there is no way to get it back. |
| rmdir dir | Delete directory dir. Once a directory is deleted, there is no way to get it back. |
| exit | End your session and exit the UNIX system. |
In order to create files in UNIX, you can use a program called a text editor. There are a myriad of text editors available for UNIX, Mac, Windows, et al. You can use whatever you are most comfortable with as long as you can get your work done. In this Lab, vim will be the "official" editor we use. To start the vim editor, type vim file, where file is the name of the file you wish to edit. For instance, if I want to edit the file test.txt, I would type vim text.txt and press <ENTER>. You can now start using vim to create or edit your file.
vim is a powerful editor used heavily in the real world. vim has two modes: command mode and insert mode. Command mode is a mode where text is edited by using standard vim commands. Insert mode is a mode where text is edited by directly typing text in (the mode most familiar to Windows users). vim starts in command mode by default. To switch to insert mode, press i to insert before the cursor or a to append after the cursor. When in insert mode, you can switch to command mode by pressing <ESCAPE>. vim has lots of commands you can use, but here's the first ones you should know:
| Command | Comment |
| :w | Save current file. |
| :wq | Save current file & quit. |
| :q | Quit, if already saved. |
| :q! | Quit, forgetting changes you have made. |
| :e file | Open file for editing. |
| ZZ | Synonym for wq. |
One of the most powerful things about vim is the ability to construct your own command and then have vim do your bidding. The basic building blocks are:
| Command | Comment |
| n | A positive integer. Repeat this command n times. If this building block is omitted, the command is run only once. |
| ^ | Start from the beginning of the line and go until the cursor. |
| $ | Start from the cursor and go until the end of the line. |
| w | Start from the cursor and go until the end of the word. |
Now that you have your building blocks, you can now use the commands themselves. Note that if you use either $ or ^, you can't use another building block.
| n? | Command | ^, $, or w? | Comment |
| Yes | d | Yes | Cut text. |
| Yes | y | Yes | Copy text. |
| Yes | dd | No | Cut a line of text. |
| Yes | yy | No | Copy a line of text. |
| Yes | x | No | Cut character of text. |
| No | P | No | Paste text from the d, dd, y, and yy commands before the cursor. |
| No | O | No | Start a new line before the cursor. |
| No | p | No | Paste text from the d, dd, y, and yy commands after the cursor. |
| No | o | No | Start a new line after the cursor. |
| No | u | No | Undo. |
Suppose I wanted to delete three words, I would type 3dw. I could paste these later by pressing p. As another example, if I want to copy 5 lines, I would type 5yy. Try these on your own. Think up of as many combinations as you can and test them out in the vim editor. There's much more to the vim editor. Entire reference books, pocket guides, and even coffee mugs are available to help you learn vim commands. The preceeeding commands should be enough to get you started and the best way to get comfortable with vim is through practice.
We will now compile our first program. First, change to the lab00 directory by typing cd cs54/lab00. Second, use vim to create a new file called lab00.cpp by typing vim lab00.cpp. Then, put this text in the new file (WARNING: Do NOT copy & paste this code. Type it in!):
// Programmer: <Your Name>
// Class and Section: CS 54, Section <n>
// Date: January 9, 2007
// Purpose: Test the compiler & our vim skills
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world!" << endl;
return 0;
}
Now we want to compile our C++ code into an actual program. The command to do this is:
g++ -Wall -W -s -pedantic-errors file.cpp -o executable
Wow! Only that, eh? Let's do this by parts:
| Part | Comment |
| g++ | The GNU C++ Compiler. The actual program that converts our C++ code to something executable. |
| -Wall -W -s -pedantic-errors | The compiler flags. These flags tell the compiler how to behave. |
| file.cpp | The file you created that you want the compiler to compile. |
| -o executable | Tells the compiler to save the executable file in a file called executable |
So, we will end up typing:
g++ -Wall -W -s -pedantic-errors lab00.cpp -o lab00
This will take lab00.cpp, compile it, and save the executable in a file named lab00.
If there are no errors, we can run the executable by typing:
./lab00
Our "hello, world" message will be printed out on the screen and we will be done...almost!
Now go to the line that ends in the text "endl". Delete the semi colon at the end of that line. Try to compile as we did above. See what happens? We get an error message from the compiler:
lab00.cpp: In function `int main()':
lab00.cpp:12: error: expected `;' before "return"
See how imperfect the compiler is? It indicates an error on line 12 (The line that has return 0;) but the error is actually on the line previous to line 12, which is line 11. This is a useful debugging technique when the compiler gives you an error. If you can't find the error the compiler thinks the error is on, try the line(s) before it instead. Compiler aren't perfect. Neither are we. Working through mistakes is what debugging is all about.