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.C
Lab46:~$ 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:

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

The same result can be achieved using the following three commands:

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

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.C
Lab46:~$ 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.

1 #include <iostream>
2
3 using namespace std;
4
5 int main()
6 {
7     cout << "Hello World!" << endl
8     return 0;
9 }

Attempting compilation will give you the following error.

Lab46:~$ g++ test1.cc -o test1
test1.cc: In function 'int main()':
test1.cc:8: error: expected `;' before 'return'

As you can see, the compiler points out that there is a missing semicolon in function main() at or around line 8 (errors can and do cascade down) before the return statement.

The corrected code is:

1 #include <iostream>
2
3 using namespace std;
4
5 int main()
6 {
7     cout << "Hello World!" << endl;
8     return 0;
9 }

When this is now compiled, the compiler will execute and exit, without any resulting output.

Testing the newly compiled program results in this:

Lab46:~$ ./test1
Hello World!

Great success!