gcc and makefiles


What is gcc?

"GCC is a free compiler collection for C, C++, Objective C and other languages."
There is extensive documentation at http://www.gnu.org/software/gcc/onlinedocs/gcc_toc.html .

Most common uses for gcc

Common options

Using gcc to compile C++ programs

Use g++ instead of gcc.
Although gcc will recognize C++ source files by their extension, the second step of the two-step method has no way of knowing to use the C++ libraries instead of the C libraries. Using g++ will tell the linker to use the C++ libraries.


What is a makefile?

A makefile is a set of rules that tell how a program is compiled and linked. They are useful for two reasons:
  1. Simplifies building of large programs (Just type 'make').
  2. If the program is built already, 'make' only compiles the parts of the program that need to be compiled.

Writing a rule

A Makefile (usually named Makefile with a capital M so it appears at the beginning of the directory listing) in its simplest form is a list of rules that look like this...
target : prerequisites
        command
	...
The target and prerequisites refer to filenames in the local directory. If any file in the prerequisites list was modified more recently than the target file, the command following is executed. The command line must begin with a tab character.

Multiple rules

The order of rules in the Makefile does not matter, except the default rule is the top one, so put the link command as the top rule and all the compilation steps beneath it.

This is only a very basic introduction to Makefiles. More information can be found on make's info page (type info make).


Last updated by Jason Long on 2000 Feb 15.