This document is part of the HTML publication "An Introduction to the Imperative Part of C++"
This tutorial shows you how to create, compile and execute a simple "hello_world" C++ program using the emacs editor and the g++ compiler.
It is recommended that you create a new folder for each program that you wish to write.
#include <iostream>
using namespace std;
int main() {
cout << "hello world!" << endl;
return 0;
}
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.
hello_world: hello_world.cpp [TAB]g++ -Wall -g hello_world.cpp -o hello_world
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.
Alternatively (and this is recommended) if you have installed the appropriate emacs file you can continue to do everything from inside emacs:
Alternatively, you can continue to work from inside emacs (with the recommended .emacs files):