This document is part of the html publication "An Introduction to the Imperative Part of C++"
The original version of this document was produced by Rob Miller at Imperial College London, September 1996.
Version 1.1 (modified by David Clark at Imperial College London, September 1997)
Version 1.2 (modified by Bob White at Imperial College London, September 1998)
Version 1.3, 1.4, 2.0, ..., 2.10 (modified by William Knottenbelt at Imperial College London, September 1999-September 2011)
Appendix 1 - Guide to emacs and g++
Using emacs and g++ to create and run a simple C++ program
This tutorial shows you how to create, compile and execute a simple
"hello_world" C++ program using the emacs editor and the g++ compiler.
Step One - Create a new directory/folder
Log into a UNIX machine (either log in at the console, or connect
from a Windows machine using Exceed and/or a ssh-client like putty).
At the UNIX prompt, type mkdir hello_world to create a
new directory/folder for your application.
Change into the new directory by typing cd hello_world.
It is recommended that you create a new folder for each program that
you wish to write.
Step Two - Create the program files
At the UNIX prompt, type emacs hello_world.cpp
Using emacs enter the following short program (click here
for emacs help and quick reference):
#include <iostream>
using namespace std;
int main() {
cout << "hello world!" << endl;
return 0;
}
Save your program by pressing Ctrl-x Ctrl-s.
Should you
wish to quit emacs you can do so with Ctrl-x Ctrl-c, but in fact
everything you need to do to run your program can be done
from inside emacs so this is not necessary.
Step Three - Create a Makefile
Create a file called makefile inside the hello_world
folder. You can do this by editing a new file from inside emacs by pressing
Ctrl-x Ctrl-f, or you can run emacs makefile from the UNIX prompt.
Enter (and save) the following text:
hello_world: hello_world.cpp
[TAB]g++ -Wall -g hello_world.cpp -o hello_world
For [TAB] you should press the TAB key (the key to the left of the
Q key); if you are using Exceed you may need to press Ctrl-i.
A "makefile" is a set of rules for compiling a program for use by
the Unix utility make. The
interpretation of this particular makefile is that the target
of the compilation is an executable file called
hello_world. hello_world depends on the file
hello_world.cpp, such that whenever hello_world.cpp is
altered, hello_world should be remade. The command for remaking
hello_world from hello_world.cpp using the g++ compiler
is given in the second line. Here the -Wall option turns all
warnings on, -g includes debug information and -o
hello_world tells the compiler to put the executable program in a
file called hello_world. For more information about target directed
compilation using make click here.
Step Four - Compile the program and prepare the executable
Now, to compile your program just type make at the UNIX
prompt.
Alternatively (and this is recommended) if you have installed
the appropriate .emacs file you can
continue to do everything from inside emacs:
Use Ctrl-x b to switch back to hello_world.cpp.
Now press Ctrl-x c
and hit enter to begin the compilation.
The screen will split into two
windows (press Ctrl-x o to switch between them and Ctrl-x 1 to
return to single window mode).
If there are any errors they should be listed in the compilation
window. To correct them use Ctrl-x g to go to a particular line
(alternatively advanced users might like to edit their .emacs file and
map the emacs command next-error onto a key of their choice).
Step Five - run the executable program
To execute your program just type ./hello_world at the
UNIX prompt.
Alternatively, you can continue to work from inside emacs (with the
recommended .emacs files):
If you are not in two window mode, press Ctrl-x 2.
If you are not in the lower of the two windows, press Ctrl-x
o until you are.
Press Ctrl-x Ctrl-u to bring up a UNIX prompt inside the current
emacs window.
Now type ./hello_world at the UNIX prompt. All
the output from the program will be displayed in the emacs window.
(BACK TO COURSE CONTENTS)