IC210 Lab 1


Part 0: Lab Ground Rules
Part 1: The Computing Environment

Login: Ubuntu

Your Unix Account

Part 2: Your First Program
We will be using the Unix environment to create, compile and execute C++ programs. First, you must choose a text editor to create your first C++ program:

Our C++ program (source code) will be entered as a file. This is the actual file that you will type using the editor. Other files will be used along with your source code to enable the execution of your program. For example, another type of file that may make up your project is a "header" file, or "include" file. An "include" file allows you to use additional source code that you or others have written. They comprise a library of helpful tools -- for example, they can provide input/output capabilities, standard math functions (like cosine, or finding the square-root), among others. In fact, they are often called standard library files. You reference a "header" file by "including" it into your source file. You will include a library file today that provides basic input and output services for your program (details to follow).

The complete collection of files needed to run our program is termed a project. So, a project is the collection of files that make up the program (or application) that you are developing.

Step-by-step instructions to creating the "HELLO WORLD" program

STEP
INFORMATION
0 Directory setup:
  • Open a terminal by selecting the Terminal application (to do so, look at the "Launch Bar" on the left side of your screen. Click on the top bottom (text saying "Dash Home" appears when you hover over this), wait for a box to appear, and type "Terminal". Or, if the Terminal icon already appears lower down in the Launch Bar, just click on that). This will open a window where you can type in Linux commands.
    USEFUL SHORTCUT: Pressing Ctrl+Alt+T will also open a terminal.
  • Make a new directory (what Windows calls a "folder") to hold your work. You use the "mkdir" command for this. In the terminal that you just opened, type exactly this:
    mkdir ic210
  • "Permissions" control who is allowed to view, modify, and use your files. Later you'll learn the details of this, but for now we just want to ensure that the permissions of your directory allow only you to use your files. The "chmod" (change file mode) command is used for this. First, you will change the permissions mode as follows: for users that belong to the same group(g) as this file or directory, you want to subtract permissions (a minus sign) for read(r), write(w), and execute(x). So enter this:
    chmod g-rwx ic210
    Next, you will do the same thing, but also for all "other"(o) users (all users that don't either own the file or belong to the same group). Enter this:
    chmod o-rwx ic210
  • If during this or any other step you get unexpected error messages, talk to your instructor.
  • Now we want to change the current directory to the new ic210 directory. The "cd" command changes the directory, so enter this:
    cd ic210
  • Make a new directory to hold your files for lab01:
    mkdir lab01
  • Change to this new directory:
    cd lab01
  • Let's verify that we are where we expect to be. Use the "pwd" command ("print what directory"). Enter this:
    pwd
    You should see a full listing of the "path" to your current working directory, which should look approximately like /home/mXXXXXX/ic210/lab01
  • NOTE: For all labs and other work with your Unix account, keep all of your files inside your ic210 folder. Inside the ic210 directory, make a new directory for each lab, homework, project, etc. Do this regardless of whether you are using a lab machine or your own Bancroft machine.
1 Open your preferred text editor (Click on the "Dash Home" button at top left again, wait for the box, then type "gedit", "emacs", OR "vi" and hit Enter). gedit is the simplest, so start with that for now unless you have used something else before. We assume gedit below.
2 The editor window is where you type in your source code.

The editor uses something called syntax coloring to make it easier for you to read the programs you have written. Syntax coloring highlights the different program elements such as comments, keywords, numbers, and variables. This allows you to easily identify elements of your source code and find common syntax mistakes quickly. For example, gedit highlights comments in blue. If you see your source code is blue then you likely forgot to close a comment block. Additionally, the editor attempts to help in other ways such as by providing automatic indenting, aligning braces, and so forth.

blah
3 Give your file a name by saving it. We could just starting typing in the default, unnamed file that appears, but eventually the file needs a name. In addition, the syntax highlighting discusssed above won't work until we give our file a name ending with .cpp (before that, the editor doesn't know what kind of file we are working with!). So let's first save our file, even though right now it is empty:
  • Find the menu for gedit: click on the gedit window to make it active. Next, look in the very top left of your screen, where it will say something like "Text Editor" or "Untitled Document - gedit" (what you see changes depending on if the editor window is maximized or not). Hover the mouse over that text, and the menu appears.
  • Select File->Save As from the menu.
  • Navigate to the ic210/lab01 folder, using one of the directory options that appear at left. You may need to first find the mXXXXXX directory, then ic210, then find lab01).
  • Name your file main.cpp and save it.
3B Enable essential gedit features. There are some important gedit preferences to set. Go to the menu bar and select Edit->Preferences. In the window that appears, do these steps:
  • Under the "View" tab, select "Display line numbers".
  • Under the "Editor" tab, change the "Tab width" to "4".
  • Under the "Editor" tab, select "Insert spaces instead of tabs". (Why? Proper idententation is essential for good programming, and your grades. Inserting spaces instead of tabs ensures that your program still looks right if you paste your code in email or open it in a different editor).
  • Under the "Editor" tab, select "Enable automatic indentation". (This won't do everything, but makes it easier to properly indent your program.)
  • NOTE: you should only have to do this once in the lab, and gedit will remember the preferences -- but don't forget to do this again from your room, so that these preferences will also apply when you do homework etc.
4 Create your main and add your include statements. If you do not understand what you are typing DO NOT BE CONCERNED. Your instructor will soon explain required and essential elements of C++ programs. Type the code exactly as shown below.
#include <iostream>

int main()
{
  std::cout << "Hello World!" << std::endl;
  return 0;
}
When finished, save your file by clicking the Save icon, or press Ctrl-S.
5 Compile. If you want the programs you write to actually do something then you must compile them (turn source code into machine language). Converting high-level C++ source code into machine language is very complex, but compiler vendors solve this problem for you. To compile your program, first make sure that you have a terminal open from before, whose current directory is where you saved main.cpp. Once you are there, compile your program by typing into the terminal g++ -Wall -Wextra main.cpp -o m012345.

If your program compiled successfully, you should not see any output from g++ (you will just see the prompt, waiting for the next command) and there should be a new file in your working directory called m012345. (If you omit the "-o m012345", which we will often do, then a file with default name of "a.out" is created.) The new file is the "executable file" for your program.

If you do see any text output from g++, then get assistance from the instructor. If there are errors, they will be listed in the output. Tips for deciphering error messages are given at the end of this tutorial.

Note: technically, the single command you issued actually "compiled" and "linked" your program. We'll talk much later about this, but in short "linking" (amongst other things) takes your code and combines it with other code provided elsewhere to do standard things like input and output (this is referenced by the #include <iostream> line in your code). Often, we'll informally call this whole process "compiling."

6 Run! Once you successfully compiled and linked your program (no errors), then you are ready to execute your program. In your current working directory, you will see a new file called m012345 (or whatever you have named it). That is your program. To execute a file, type the file's name preceded by a "dot slash" (literally, "./") and hit the enter key.
screenshot
Note: the "dot slash" is telling the computer to look for the program (m012345) in the current directory (indicated by the "dot"). In some cases you can get away with not including this, because your computer is set up to automatically look for commands in the current directory (amongst other places), but including the "dot slash" is safer because it will always work.
7 Program Output: Your program will output to the same terminal from where it was executed, as seen above.

Now, let's view the executable file you just executed. Type ls -l to list details of the contents of your current working directory. There is a file named main.cpp – it is your source code file. Note the size of the file (95 byes in our example). The file called m012345 is your program's executable file, which is combined with all of the other necessary machine code to run your program. Note the size (7782 bytes in our example)!! The increase in size is due to the code that was “included” or linked into your program.

screenshot
Part 3: Let's do it all over again!
You just accomplished quite a bit; you typed in a C++ program, compiled it and executed it. It is important that you understand all the steps involved, and that you are comfortable using a text editor and the terminal.

Repeat Part 2 of this lab all over again, with these changes:

// (Your name and alpha)
// This program adds two numbers

#include<iostream>

int main()
{
  int number1, number2, sum;
  number1 = 12;
  number2 = 13;
  sum = number1 + number2;
  std::cout << "The sum of these two integers is " << sum << std::endl;

  return 0;
}

Show your instructor when you finish this part.

Part 4: The "error" of your ways
When you make certain kinds of common mistakes (syntax errors), the compiler won't be able to understand your program and will issue error statements in the terminal window. Each error message includes a number, which represents the line number of the error. Go to that line number to see where the problem is. Every syntax error must be corrected before a program will compile. Sometimes it is clear what's wrong, and other times you may have a hard time figuring out how to correct a syntax error. You will improve as you see enough errors to associate the messages with the syntax error. Here are a few simple rules and hints:

Experience and practice help a lot here. So for each of the below, purposefully make the syntax error in your current program and try to compile to see what the error message is. Correct the error before introducing the next error into your program (e.g., only add ONE ERROR at a time). Go slowly and pay attention, this will save you time later!

Show your instructor when you finish this part.

Part 5: More on Linux

You'll spend the rest of your time learning more about Linux and the more powerful editors emacs/vi. Complete as many of the following steps as possible:

Part 6: Finishing Up

You should now be comfortable with being able to generate source code, compile it, execute the program, and correct basic syntax errors -- all in Linux. Let's clean up so you can get to your next class.

  • Click the Power button in the top right corner and…
  • Choose the Log Out option. You should *not* shutdown or restart the machine.

LT J Schultz. Last modified by Assoc Prof. McDowell.

Valid XHTML 1.0 Transitional