Introduction to the C++ Compiler (hello, hello2)
CS 161 - Fall 2004 - Ms. Katz

Goals

- to learn how to compile and execute a C++ program
- to modify a program to enhance it

Overview

This activity will introduce you to the C++ compiler. You will type in the C++ program shown, compile it, and execute it. You will submit that program. Then, you will copy that program, make some changes to it, compile it, and submit it.
  // hello.cpp: traditional first program
  // your name, today's date
  #include <iostream>
  #include <string>

  using namespace std;

  int main( )
  {
     string personName;
     cout << "Please type your name: ";
     cin >> personName;
     cout << "Hello, " << personName << endl;
     return 0;
  }

Creating C++ program (hello.cpp)

Create C++ Source File in 161 folder: Be sure that the 161 folder is open in the file viewer. Do you see your myinfo.txt file? Choose File/New and then Emacs. Then choose File/Open File. At the bottom of the window you'll see Find File:~/161/, and you should name the file hello.cpp and press return. Now there should be some new menus available in emacs including C++, Tools, and Options. Choose Options/SyntaxHighlighting. The button should look pushed in. The other options are okay. Emacs knows about C++ and the default style is two spaces of indenting. You may press spaces or tabs in emacs to indent your lines.

Type in the C++ program at above right. Include your name in the comments. Get into the habit of indenting the program with the #include directives at the left margin but statements nested within constructs indented two spaces. This program has minimal comments. I will expect more on later assignments.

In this program, two lines are indented two spaces: the cout statement and the return statement. All other lines start at the left margin. Programs are for people to read as much as for compilers to translate. Comments and indenting are essential.

The editor will highlight the special C++ words such as include, int and return. As you learn more about C++ and this highlighting, you will recognize when the code has a bug even before you compile.

Save the file, but leave it open in the editor.

The Translation Process: You can read your C++ source program, but the computer needs it in a form it can execute. You must translate it from its current language into an executable language. This translation process is shown in the picture below. For this course, we will use the term compiler and the c++ command to refer to the combined compiler and linker.

Translating from source code to executable

Compile in emacs: We can access the c++ compiler through emacs or the terminal. The same command works the same way in either place. The compiling command option we use turns on all warning messages and saves the executable program in an a.out file. In emacs, choose Tools/Compile and note that the bottom line now reads Compile command: make -k But we don't need the make part, so you should backspace over that back to the colon(:) and type c++ -Wall hello.cpp and press return.

That command tells your computer to run your source program (hello.cpp) through the c++ compiler. It asks that all warnings (-Wall means Warnings all) as well as errors be reported. That is the option I will be using, so you want to see all the warnings and fix the problems so that I don't see them when grading. We didn't provide a name for the executable program that results; therefore, it is placed in the default file, a.out. If you were doing this in the terminal, you would just type the part in bold (starting with c++).

When you compile in emacs, it splits the window and writes compiling information and error messages in the bottom one. If the translation was successful, you'll get a message saying something like Compilation finished. If it was unsuccessful, read the error messages and warnings. Each one suggests what is wrong and on which line. Sometimes the mistake is on the line above the one mentioned. The current line number is shown in the bar below your program text in emacs. You can also choose Edit/Go to and then Go to line and type in the line. Fix the first mistake first. Check to see if you made similar ones and correct those as well. But sometimes one little mistake will cause a great many error messages. So don't try to fix all of the bugs before compiling again. Compile again by repeating Tools/Compile (or in the terminal, use !c+ or press the up arrow key to get to previous commands and press return). Your c++ command should be there. Emacs will ask if you want to save the file. But if you are compiling in the terminal, you have to remember to save. This is one of those emacs advantages.

Keep fixing problems and compiling until you have no errors or warnings. If you are compiling in the terminal, you will just get the prompt back without a Compilation finished message.

Be in Correct Folder to Execute: Move to the terminal window. You need to be in the 161 directory where you just compiled your program. That directory will now have more files. Does the prompt show that you are in your 161 directory? If not, use cd to get there. Type ls -l (those are the letter l) to see a listing of the files in your 161 folder. hello.cpp should be there and should be bigger than zero characters. The time stamp should be recent. You should also have an a.out file that contains your translated and linked program.

To execute your program, use ./a.out and press return. The dot at the front says to look in the current directory for this file. What do you expect it to print? Does it? Is everything spelled correctly? If not, fix the program, compile it again> and execute it again until it is correct.

Submit your Program: In the terminal, use submit and think about your answers to give your source program (not your executable program) to my katz161 account as the hello lab. Note that the name to use for submitting is usually at the top of the lab handout. Always be in the directory where you compiled and executed your program when you submit.

Modifying an existing program (hello2.cpp)

For this next activity, you will modify the program you created above. You don't want to replace your previous program, so you will copy it and modify the copy. The new program should ask for the person's first name and their last name and then print a greeting and both names with appropriate spacing.

Create a New Program from an Existing Program: In the terminal while in the 161 folder, type
  cp  hello.cpp  hello2.cpp
This copies the contents of the original file to a new file with a different name. There are spaces between the three words. Then choose Open File in emacs. An alternative way to create the file, if hello.cpp is already open in the editor, is to use Save Buffer As.

Make a Few Small Changes: Edit hello2.cpp. Check to be sure that you are changing the correct file. Think about what you need to do to ask for both names from the user and then print those neatly in one output line. You need two string objects that must be declared before they are used. Be sure to put << before each thing you print. Save before you compile.

Compile and Execute the Program: Compile hello2.cpp being sure to put its name in the compiling command. After you are successful compiling, execute it in the terminal. Because we are using the default a.out file, its old contents will be replaced by the new executable. That's okay. You submit the much smaller source program. Does your program do what you expected?

When you are done, submit your modified program as hello2. This ends the lab activity. Check with submitlog to see that you have submitted your info file, a hello program, and a hello2 program.

To compile a C++ program (for example, hello.cpp) be sure you: