next up previous
Next: Debugging Up: Hartebeesthoek 26m New Computer Previous: Compiling

Make

All projects must be managed with the wonderful Unix tool - make.

Make requires an input file (called Makefile or makefile) which describes the dependencies and rules for compiling. This file must be stored in the ~project/src directory. For every target which has to be made list a dependency. Make maximum use of environment variables and implicit rules. If the implicit rules are not sufficient the define new ones (there are many implicit rules - to find out a list of implicit rules type make -p | less).

The following standard targets must exist in every Makefile :

Multi-platform support can be handled in one of two ways :

1.
One master Makefile for all platforms with conditional statements in the Makefile to handle differences between platforms; for this to work one has to use the GNU make (sometimes referred to as gmake),

A simple example Makefile using this method is :

Makefile
--------

#
# Makefile for compiling the program "main" on multiple platforms 
# (sun and linux)
#
# author : andy gotz
#
# history : 7nov95 - created
#
#ifdef "sun4"
CC = cc
CFLAGS = -suncflags
LFLAGS = -sunlflags
INSTALLDIR = /usr/local/bin
#else
CC = gcc
CFLAGS = -linuxcflags
LFLAGS = -linuxlflags
INSTALLDIR = /bootes/usr/local/bin
#endif

all : main

main : main.o
        $(CC) $(LFLAGS) -o main main.o

install : all
        cp main $(INSTALLDIR)

clean : 
        rm -rf *.o
2.
One Makefile per platform, this is the simple way out but can lead to more work because there are more Makefiles to deal with.

Using this approach the above example would be two files :

Makefile.sun4
-------------

#
# Makefile for comiling the program "main" on a sun4
#
# author : andy gotz
#
# history : 7nov95 - created
#
CC = cc
CFLAGS = -suncflags
LFLAGS = -sunlflags
INSTALLDIR = /usr/local/bin

all : main

main : main.o
        $(CC) $(LFLAGS) -o main main.o

install : all
        cp main $(INSTALLDIR)

clean :
        rm -rf *.o

Makefile.linux
-------------

#
# Makefile for comiling the program "main" on a sun4
#
# author : andy gotz
#
# history : 7nov95 - created
#
CC = cc
CFLAGS = -linuxcflags
LFLAGS = -linuxlflags
INSTALLDIR = /bootes/usr/local/bin

all : main

main : main.o
        $(CC) $(LFLAGS) -o main main.o

install : all
        cp main $(INSTALLDIR)

clean :
        rm -rf *.o

My own preference is for the first method.


next up previous
Next: Debugging Up: Hartebeesthoek 26m New Computer Previous: Compiling
Mike Gaylard
1999-06-14