User guide
Chapter 4. Compiling and Building
44
In brief, the tools work via the gcc command. This is the main driver for the compiler. It can be used
from the command line to pre-process or compile a source file, link object files and libraries, or perform
a combination thereof. By default, gcc takes care of the details and links in the provided libgcc
library.
The compiler functions provided by GCC are also integrated into the Eclipse IDE as part of the CDT.
This presents many advantages, particularly for developers who prefer a graphical interface and
fully integrated environment. For more information about compiling in Eclipse, refer to Section 1.3, “
Development Toolkits”.
Conversely, using GCC tools from the command-line interface consumes less system resources. This
also allows finer-grained control over compilers; GCC's command-line tools can even be used outside
of the graphical mode (runlevel 5).
4.1.6.1. Simple C Usage
Basic compilation of a C language program using GCC is easy. Start with the following simple
program:
hello.c
#include <stdio.h>
int main ()
{
printf ("Hello world!\n");
return 0;
}
The following procedure illustrates the compilation process for C in its most basic form.
Procedure 4.1. Compiling a 'Hello World' C Program
1. Compile hello.c into an executable with:
gcc hello.c -o hello
Ensure that the resulting binary hello is in the same directory as hello.c.
2. Run the hello binary, i.e. hello.
4.1.6.2. Simple C++ Usage
Basic compilation of a C++ language program using GCC is similar. Start with the following simple
program:
hello.cc
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello World!" << endl;
return 0;