The GNU G++ Compiler

There are many different C and C++ compilers for LINUX and UNIX environments. Most of these are specific for a particular architecture and operating system. The fine folks in the GNU project have created a C/C++ compiler that has been ported to many different architectures. The name of the compiler is gcc or g++ for the C or C++ compiler respectively. Because of the similarities between C and C++, the two commands actually call the same compiler with different default options. Because of the similarity, the rest of this document will talk exclusively about g++. The discussion will automatically apply to g++ unless stated otherwise.

Creating an executable program is a two step process. First the source code is compiled to object code. The object code may or may not be stored as a separate file, depending on the compiler used and the compiler options. Second, the object code is linked with other collections of object code (including system libraries) to form an executable file.

The GNU g++ compiler can be used to control both the compilation phase and the linking phase of creating executable files.


Options

Like most LINUX or UNIX commands, g++ has numerous options. The most useful of these are:


Examples

Here are some examples of using g++ with many of the options given above.

For the compilation phase:

g++ -c -O -Wall -Imy_include_dir myprog.cc

This compiles myprog.cc to an object code file with optimization turned on, showing all warnings, and looking in the directory my_include_dir for any additional include files. The file created will be "myprog.o".

For the linking phase:

g++ -g -Wall -o fun fun.o support.o graphics.o -L/usr/lib/X11 -lX11 -lm

This links the object code files fun.o support.o and graphics.o into the executable file "fun". All warnings are displayed and debugging information is preserved. In addition, the directory /usr/lib/X11 is used to search for additional libraries and the libraries X11 and m (X11 graphics and math) are linked in as well.