Creating a C++ Program

You are going to create your first C++ program. At the command line type emacs proj0.cpp to start Emacs. The file proj0.cpp should not exist yet, so it will be created the first time you save your file.

The program is a simple "Hello, world!" Here is how the output should appear when the program is run:

  linux2[1]% ./proj0
  Hello, world!
  linux2[2]% 

Note that linux2[1]% is the Linux shell prompt, not part of the program.

A program template is given below; you will need to add one line to write the message "Hello, world!" You can write to the screen using cout and the insertion operator (<<). For example, the following line writes "I love CMSC 202" to the screen:

  cout << "I love CMSC 202" << endl;

Program Template

#include <iostream> using namespace std;

int main() {

   // Insert your code here

   return 0;
}

Once you have typed your code in Emacs, be sure to save (C-x C-s) or save and quit (C-x C-c).

Compiling and Running

To compile your program, enter the following command at the Linux prompt:

  g++ -Wall proj0.cpp -o proj0

This command runs the GNU C++ compiler (g++). The option -Wall instructs the compiler to be verbose in its production of warning messages; the option -o proj0 (hyphen followed by the letter "o", not the digit zero), instructs the compiler to give the executable program the name proj0. If the program compiles corectly, the executable file proj0 will be created in the current directory.

At the Linux prompt, enter the command ./proj0 to run your program. It should look like the sample output provided above.