[ Prev ] [ Index ] [ Next ]

make

Created Monday 3/8/2009

Discuses use of GNU make (as opposed to Solaris make). It is important to note that all commmands within any make target requires a tab character at the start of the line. In addition, the default makefile that make(1) looks for is Makefile in the current directory. Alternate files can be processed using make's -f option

1. Simple makefile structure

Todo: expand on these points

The structure of a Makefile is broken down into targets and dependences, variable definitions and command executions.

2. Invoking make within make

It is trivial to invoke a subsequent make process within the context of executing a make target (either invoking a target in the same Makfile or another). What is less obvious is invoking make in a different directory than the the current working directory. Any attempt at executing cd will be per process only, which means that when the child make process is launched, the current working directory will revert to that of whatever the working directory was when the parent make was started. To get around this problem, combine the execution of the change directory command with the invocation of the child make process. E.g.,

mytarget:
    @cd /tmp && make foo

3. Implicit rules

If the objects created by a makefile are generated from implicit rules then it becomes possible to group object creation by prerequisites rather than by explicit target names. The following is adapted from 2.6 Another Style of Makefile:

objects = main.o kbd.o command.o display.o insert.o search.o files.o utils.o

edit : $(objects)
    cc -o edit $(objects)

$(objects) : defs.h
    kbd.o command.o files.o : command.h
    display.o insert.o search.o files.o : buffer.h

In this example, defs.h is specified as a prerequisite for all the files given in objects. The files command.h and buffer.h are prerequisites of the specific files (e.g., command.h' is a prerequisite for kdb.o, command.o and files.o''). This style of makefile trades compactness for readability and it may be more appealing for some people to explicity put all the information about each target in one place.

Stuart Moorfoot © 3 August 2009 foo@bund.com.au


Backlinks: