CS 112 Lab 0: Getting Started

Objectives:

In this exercise, you will:
  1. Login to a workstation in the Unix Lab.
  2. Use Linux commands to interact with files and folders.
  3. Use the Eclipse IDE to edit, compile, and run a program.
  4. Debug your first C++ program.

Introduction

Welcome to CS 112! In this first lab exercise, we will introduce the Eclipse integrated development environment, and see how we can use it to write a C++ program. The exercise assumes that a C++ compiler (e.g., GNU's gcc/g++), the make utility, Eclipse, and CDT (the C++ plugin for Eclipse) have all been installed on your system.

This lab tells you how to use Eclipse in the CS Dept's Unix Lab (SB 354), which is currently using Ubuntu Linux. If you have not used Linux before, you may want to work through this Introductory Exercise.

Using Linux

I would recommend that you bookmark this Linux Quick Reference, so that you can locate it quickly in the future. Your instructor will conduct a quick introduction to using the Linux command-line. These are useful skills to practice, as they will allow you to login to a remote system (e.g., from home) and work with your files remotely.

Launching Eclipse

There are different ways to launch the Eclipse IDE, but for now, just do this:

A "splash screen" will appear showing Eclipse and the version number.

Specifying a Workspace

Eclipse likes to store all of the program files you generate in a single folder called your workspace. So when you launch Eclipse, it will ask you what folder you want to use for your workspace:

Click the browse button, and a file-dialog box will appear by which you can navigate to the folder you wish to use as your workspace. If you have already created a 112 folder, navigate to that folder, select it, and click Ok. If you have not yet created a 112 folder, use the Create Folder button to create one named 112, select it, and click Ok.

Make certain that you do not use spaces in the names of any files or folders you create. Spaces within the names of files or folders can confuse Eclipse.

Once you click the Ok button, Eclipse will launch. The first time you launch Eclipse, it will display a cover page asking you if you want an introduction to Eclipse. This can be useful, but much of the material is Java-specific, so for now, just close this cover page. This will expose a window like the one below:

Note that Eclipse divides its screen into several regions, including:

The particular arrangement of the screen is determined by what Eclipse calls a perspective. By default, Eclipse launches the Java perspective (for building Java programs), so our next task is to switch from the Java perspective to the C++ pespective.

Switching to the C++ Perspective

There are two ways to change Eclipse's perspective:

Once you have selected the C/C++ perspective, Eclipse will update its appearance something like that shown below:

Note that the C/C++ perspective keeps the same basic areas as the Java perspective, though some of the labels have new names.

Congratulations! Eclipse is now ready for C++ programming!

Interlude

The preceding steps were all "first time using Eclipse" steps -- it should not be necessary to do them again. In fact, if you were to quit Eclipse right now, and then relaunch it, it should come back looking just the way you left it. (Feel free to try this if you like.)

The remaining steps are ones you will want to remember, as you will need to do them for each lab and assignment in CS 112.


Creating a Project

Like many IDEs, Eclipse organizes the files for a given application by storing them all in a project folder. So to start a new application/program, the first thing you need to do is create a new project. One way to do so is to use Eclipse's File menu and select File > New > C++ Project.

When you do so, Eclipse displays a C++ Project dialog in which you can enter a name for your project folder. Type lab0 for its name (remember no spaces), and then click the Finish button. If Eclipse asks you if you want to switch to the C++ Perspective, click "Yes". Eclipse then generates a new folder in your workspace named lab0, in which it will store the files we create in this exercise. This folder then appears in the Project Explorer area on the IDE's left side.

Creating a Program

C++ programs are stored in one or more source files, so to create a program in Eclipse, we must create at least one source file. To do so, we again use the File menu and choose File > New > Source File. This will display a New Source File dialog, in which you can enter the name of the file. Enter the name main.cpp and click the Finish button. You should then see main.cpp appear in your IDE's Project area, and your editing area should now contain a main.cpp tab. This is the area in which you can type and save a program.

Now that we have a file in which to store a program, we are ready to enter the program. Copy the following program into the main.cpp editing area:

 /* main.cpp
  * Student name:
  * Date:
  * Author: Prof. Joel Adams
  * For: CS 112, Lab 0 at Calvin College.
  */


#include <iostream>
#include <string>
//#include "Matrix.h"
using namespace std;

int main() {
        cout << "Welcome to the Matrix program!" << endl;

//      cout << "Enter name of file containing the first matrix: ";
//      string fileName1;
//      cin >> fileName1;
//      Matrix m1;
//      m1.fill(fileName1);
//      cout << m1 << endl;
//
//      cout << "Enter name of file containing the second matrix: ";
//      string fileName2;
//      cin >> fileName2;
//      Matrix m2;
//      m2.fill(fileName2);
//      cout << m2 << endl;
//
//      if (m1 == m2) {
//              cout << "The two matrices are the same!" << endl;
//      } else {
//              cout << "The two matrices are different!" << endl;
//      }
}
 

Be sure to customize the Student name: and Date: files with your name and today's date.

Saving and Compiling Your Program

In some IDEs, you have to save your program and then compile it. Eclipse is this way by default, but to save time , you should change your project's properties so that Eclipse compiles your program each time you save it, "killing two birds with one stone".

Once you have made this change to your project properties, you can save your program various ways:

When Eclipse compiles your program, the results of the compilation will appear in the Console area at the bottom of your IDE. If all is well, you should see something like this at the end:

   g++ -o lab0 ./main.o
   Finished building target: lab0
 
   Build complete for project lab0
If you see instead see a message like this:
   make: *** [main.o] Error 1
   make: Target `all' not remade because of errors.
   Build complete for project lab0
then your program contains one or more errors. (You may have to scroll up to see the error message.) Eclipse will display a red X-sign like this: to the left of each line where it detected an error. If you 'hover' the mouse over the red X-sign for a second or so, Eclipse will display a short summary of the error message. (Note that the actual error may be on a preceding line.) Find and fix any errors in your program before you continue.

Running Your Program

The compiler creates a binary executable version of your program. If your program has compiled successfully, you can run this binary executable version by clicking the Run button, which is a green circle containing a white triangle like this: . When you click this button, you should see the output from your program appear in Eclipse's Console area at the bottom of your IDE.

If for some reason this does not work, try right-clicking in the main.cpp window and choosing

   Run as > Local C/C++ Application
from the context menu. If your program is free of errors, that should work. (If that doesn't work, get your professor to take a look.)

When your program builds and runs successfully, congratulations! You have compiled and run your first C++ program in Eclipse!

ReRunning Your Program

To run your program again, you can use the same Run button: . (FYI: When you have multiple projects, you can switch between them by clicking the downward-arrow next to the Run button.)

Importing Existing Files

Sometimes, instead of creating a new program file, you want to make use of an existing file in a project. For example, the first data structure we are going to use is the Matrix -- a 2-dimensional grid for storing real numbers (as opposed to integers). To make life "easy", I have prepared a Matrix class for you to add to your project.

C++ classes generally use two files (as opposed to 1 if you if are used to Java):

To add my Matrix class to your project, you need to download these two files -- the header file Matrix.h and the implementation file Matrix.cpp -- to your desktop. (To do so, either From there, we will import them into our project and make use of them.

Importing in Eclipse involves three steps:
  1. First, you tell Eclipse what folder you want to import files from.
  2. Then, you select the particular files you want to import.
  3. Last, you tell Eclipse what folder you want to import those files to.
Let's walk through these steps with the first file Matrix.h:

1. Select the import folder. To import Matrix.h, choose File > Import... and Eclipse will display the following dialog:

Since we are going to import an already-existing file, select General > File System as the mechanism we will use for the import. This will "undim" the Next button, so click it. This will replace the current Import dialog with a new one, shown below:

Click the top Browse button, and then navigate to your desktop (or whatever folder contains the file you want to import into your project). When you choose that folder, it should appear in the dialog's left-hand panel, and the files it contains will appear on the dialog's right-hand panel.

2. Select the files to import. Click the "check box" next to each file you want to import -- in this case, both Matrix.h and Matrix.cpp.

3. Select your project folder. Below those two panes is a text-box labeled Into folder. Click the Browse button next to this text-box and Eclipse will display a dialog listing your project folders, from which you can choose the project into which you wish to import these files (in this case, lab0).

Click the Ok button, then click the Finish button from the previous dialog, and Eclipse will import the selected files into your project.

Once the files have been imported, they will appear in Eclipse's Project Explorer pane. From there, you can open the files, view their contents, modify them, save them, and so on, from within Eclipse. Even better, Eclipse will automatically compile these files when you rebuild your project, without your having to do anything special.

Using Imported Files

To use these files in this project, you can "uncomment" the commented-out lines by

  1. Select the line(s) you want to uncomment, so that they are highlighted;
  2. Right-click in the highlighted area and choose Source > Comment/Uncomment; or use the keyboard shortcut Cntrl-/.

You should be able to "uncomment" all of the "commented out" lines just by doing this twice (once for the #include directive, and once for the bulk of the lines in the program). Note that you can "comment out" lines in the same fashion.

When you have "uncommented" all the lines in the program, save your file and make certain it compiles correctly. When it does so, you can run it on some sample input files like mat1.txt and mat2.txt. Then import these files into your project folder (i.e., lab0), as you did with the C++ files before.

When everything is working correctly, take a few minutes to look over the code in main.cpp.

Study its code, comparing it against its run-time behavior, until you understand how it is doing what it's doing.

A Bug Hunt!

The Matrix class has a logic error in it. To see the problem, save and import mat3.txt into your project, and then rerun your program and use it to compare mat1.txt and mat3.txt. The Matrix equality operator indicates that these matrices are equal, when clearly they are not!

To fix the problem, open Matrix.cpp and locate the definition of the equality (==) operator. Study this definition. Trace through its execution (using the values in mat1.txt and mat3.txt) to figure out what it's doing wrong.

Once you understand the error, fix it! (Hint: You can and should simplify the method by completely eliminating the result variable, and by eliminating unnecessary sections of the if statement.)

To text your fix:

When your "fix" passes all of these tests, show it to your instructor and you are almost finished!

Quitting

To quit Eclipse, you can choose File > Exit or just click the "close box" on Eclipse's window. Eclipse will save its current state, so that when you start it next time, it will be all set for you!

Be sure to logout from your workstation, so that no one plays any pranks with your account!


CS > 112 > Labs > 00


This page maintained by Joel Adams.