Compiler Basics

The first thing you would want to do after you write a program is to check and see if it has any errors. There is a very effective and user friendly compiler called GNU C/C++ compiler/linker gcc. This utility is very helpful in finding out where your program has errors (especially when you have a coredump due to segmentation faults).

Generally, a large software system will consist of multiple programs, each of which uses multiple source files (MODULES) plus user and standard libraries. For example, a user's program may consist of n modules x1.c,x2.c, ..., xn.c.

To create an object from a C module x1.c (for c++ files use g++), use the command

gcc -c x1.c
If this command is successful then gcc will create and object file x1.o. (An object file is a file that contains linkable code plus symbol table information.)

If one of the n modules x1.c, ...., xn.c contains the ``main'' function, then we can create an executable from the object files as follows. (Here I assume that n=4.)

gcc x1.o x2.o x3.o x4.o
This will create the executable a.out. To create an executable named x, use the command

gcc -o x x1.o x2.o x3.o x4.o

More useful command-line options
-g

Include debugging information into the object/executable. (Note that this must be done at both the object creation and linking stage.)

-Wall

Generate all warning messages.

-O

Optimize.

-O2

Perform more optimizations.

-O3

Perform even more optimizations.

Difference Between g++ and gcc:

G++ links to the necessary libraries to support C++ programs, while gcc is for straight C programs


PREVIOUS HOME NEXT