next up previous
Next: C++ Up: A simple Makefile Previous: Removing targets

Using libraries

Sometimes the user wants to link other libraries with his programs. A very common example is the use of the math library, which is not linked by default. Consider the following program:

#include <stdio.h>
#include <math.h>
 
int main(void)
{
    printf("sqrt(17.0) = %f\n", sqrt(17.0));
    return 0;
}

A Makefile for this program would look something like:

SOURCES =       example.c
OBJECTS =       $(SOURCES:%.c=%.o)

LIBRARIES =     -lm
 
CC =            gcc
CFLAGS =        -O2 -ansi -pedantic -Wall
 
%.o:            %.c
                $(CC) -c $(CFLAGS) <
 
example:        $(OBJECTS)
                $(CC) $(OBJECTS) $(LIBRARIES)



Megens SA
Thu Jun 20 11:26:28 MET DST 1996