Table of Contents

The GNU Compiler Collection includes front ends for C, C++, Objective-C, Fortran, Java, and Ada, as well as libraries for these languages (libstdc++, libgcj,…). GCC was originally written as the compiler for the GNU operating system. The GNU system was developed to be 100% free software, free in the sense that it respects the user's freedom.

This documentation only scratches the surface on the usage of the GCC/C++ compiler, there are many more options and functionality. For more in depth information a good place to start is the manual page found in the Lab46 shell. Simply type “man gcc” at your prompt and you will be presented with a vast amount of information.

Getting Started

This section contains collected information geared towards folks who are new to GCC and may be feeling
a bit lost in the barbaric jungles of GCC's source code. Although the GCC community is continuously trying
to improve GCC's internal modularity and interfaces, the fact remains that GCC is about 20 years old, fairly large
and needs to keep working on the multitude of supported architectures and languages. So, getting to
work with GCC can be a challenge.

Everyone is welcome to add links to tutorials, HOWTOs, cheat sheets, etc that may be floating around the net.

Example 1: Compiling a simple program

Consider the following example: Let “hello.C” be a file that contains the following C++ code.

#include "iostream.h"
int main()
{
cout << "Hello\n";
}

The standard way to compile this program is with the command

Lab46:~$ g++ hello.C -o hello

This command compiles hello.C into an executable program named “hello” that you run by typing 'hello' at the command line. It does nothing more than print the word “hello” on the screen.
Alternatively, the above program could be compiled using the following two commands.

Lab46:~$ g++ -c hello.CLab46:~$ g++ hello.o -o hello

The end result is the same, but this two-step method first compiles hello.C into a machine code file named “hello.o” and then links hello.o with some system libraries to produce the final program “hello”. In fact the first method also does this two-stage process of compiling and linking, but the stages are done transparently, and the intermediate file “hello.o” is deleted in the process.

Example 2: Compiling a program with multiple source files

If the source code is in several files, say “file1.C” and “file2.C”, then they can be compiled into an executable program named “myprog” using the following command:

The advantage of the second method is that it compiles each of the source files separately. If, for instance, the above commands were used to create “myprog”, and “file1.C” was subsequently modified, then the following commands would correctly update “myprog”.

Lab46:~$ g++ -c file1.CLab46:~$ g++ file1.o file2.o -o myprog

Note that file2.C does not need to be recompiled, so the time required to rebuild myprog is shorter than if the first method for compiling myprog were used. When there are numerous source file, and a change is only made to one of them, the time savings can be significant. This process, though somewhat complicated, is generally handled automatically by a makefile.

Finding and Dealing With Errors

The fact that the compiler gives warnings is very helpful in debugging code. The example below contains a common error, a missing semicolon.