The goals of this lab are to:
Secondary storage (a disk drive) is organized into directories and files. Files are often text (for example, letters, class rosters, etc.). Another kind of file is an executable file. An executable file is a ready-to-run program. They are also stored as files somewhere in the secondary memory of the computer, cs. Directories are special files that are used to organize other files that are related. Note that directories can themselves contain other directories (sub-directories).
On cs you have what is called a personal directory. This name is used because only you (via your username and password) have access to the contents contained within it and its subdirectories. Another name used for this important directory is your home directory. When you first login, it is this home directory that you are placed in.
In addition to your personal directory, you have a class directory for this course. The class directory should be accessible to both you and your instructor, but not accessible to any other student in the class. All work associated with this class should be placed in your class directory. We will look at this 1425 class directory in more detail later in the lab.
Each project or programming assignment should be placed in its own project directory in your class directory. Your instructor will indicate exactly which name to use for each class project.
Enough words! Let's take these concepts and apply them by getting online.
You should have learned how to login to cs in a previous lab. Of course, we can do nothing on cs until you login. So, go ahead now and login.
Every file in the Unix file system has a simple file name like ".login" or "hello.C" and also a full path name like "/u/css/xb20145/.login" or "/u/msd/aam/1425/hello.C" The full path name of a file ends with the simple name of the file; preceding the simple name of the file is the full path name of the directory the file resides in.
While you are logged in, the system keeps track of a current directory or working directory for you. Files that you refer to in commands are assumed to be files in your current directory unless you supply additional directory information (for example, you could type the full path name). Clearly we will do less typing if our current directory is the directory that contains the files we're working with.
When you first login, your current directory is your home directory. To see the name of your current working directory, use the pwd command. That is, at the shell prompt ("%") type:
pwdDo that now.
This command asks Unix to print your working directory. On cs, you should see something like:
/u/css/your_login_name
Recall from above that all your work for this class should be placed in your 1425 (class) directory not your personal directory. In order to work in the 1425 directory we must first change to the directory. To do this, enter the following:
cd 1425
Next, verify that your current directory is your class directory by typing
pwdYou should get exactly one line of output containing the name of your current directory (and nothing else), such as:
/u/css/your_login_name/1425If you see anything else, look closely at what you've typed (maybe you misspelled something). If you're having any trouble, talk to your instructor.
Next, go ahead and see what (if anything) is in your 1425 (class) directory by entering,
ls
Before creating a project directory and running your first C++ program, let's see how to return to your personal (or home) directory. It is easy; just enter
cd(That's a cd command with no arguments.) Try the pwd command while there to verify that you are, in fact, back in your personal (and private) workspace. No matter which directory on the system is your current directory, entering the cd command with no arguments will return you to your home directory.
You could also have used the following command to get from your 1425 directory to your home directory:
cd ..A cd followed by two dots will move you up one level in the directory tree. Since your 1425 directory is located inside your home directory, "cd dot-dot" will take you that one level up.
You should now be in your home directory. Assuming everything is as expected, return now to your 1425 directory by re-entering the same cd command you used before, "cd 1425". Before going on to the next section, make sure to verify that you have changed back to your class directory (use the pwd command again).
Before compiling and running (executing) our first program, we must first create a directory to contain all the files that will be used in the project. We will name the directory "lab1".
After double-checking that you are in your class directory, enter the following command to create the new "lab1" directory,
mkdir lab1
Remember, Unix command names and file names are case sensitive. Verify that the directory has been created using a modified ls command. This time type the following (after the ls hit the space bar and then type a minus followed by a lowercase L):
ls -lThe flag "-l" tells UNIX to show you the long version of your file listing. Verify that "lab1" is listed as one of your files. Note the "d" at the front of the line for that file. The letter d tells you that the file is really a directory.
Before we create the C++ program source file, we must first change to the newly created project directory by entering,
cd lab1
If you are unsure whether you are in the "lab1" directory or not, use the pwd command once again.
pico hello.C
We want to put the following program into the file called "hello.C." You can either type it or use the directions below the program to copy and paste it from Netscape to cs.
// Program: hello.C // Date: <today's date> // Programmer: <your name> #include <iostream.h> int main(void) { cout << "Hello World!\n"; }You need to make this program your own by adding your name and today's date to the file. To exit the file, you will hit control-x, as you can see from the menu.
Use the ls command to verify that the file "hello.C" is present. Then enter
cat hello.C
The cat program displays the contents of a file on the screen. Verify that the contents of the hello.C file is your C++ program and nothing else.
We are now about to compile our program.
As we have discussed, all computer programs must be translated into machine language prior to their execution. It is a three-step process,
In order to translate and link your source file, enter
g++ hello.CTyping this form of the g++ command translates the program to machine code and leaves the executable program in a file named "a.out" in the current directory. If all went well, you should get another shell prompt ("%") right below the g++ command you typed. Any other output contains error messages whose causes must be remedied before you go on (you'll need to edit hello.C and then type another g++ command). If you're unsure what's wrong, ask.
At this point you should use the ls command to see what has happened in the hello directory. Notice that a new file, "a.out", has been created. This is the executable file which we will call to execute our C++ program.
The name "a.out" is a default name for any executable file produced by the g++ command. We would prefer to call our executable files by a name that is the same as the C++ source code file, but without the ".C" extension. Let's delete the a.out file and produce an executable file called "hello". Enter
rm a.outNow we'll compile our C++ program again, but this time we'll specify a better name for our executable file. Enter,
g++ hello.C -o hello
We told g++ to name the output "hello". List your files with the ls command and note the difference. The "a.out" file is gone and you now have a file named "hello". You are now ready to run your very first C++ program.
A DRUM ROLL, PLEASE! To execute your program just say its name. Enter
hello
Congratulations! Welcome to C++ programming!