OBJECTS = main.o edit.o
edimh: $(OBJECTS)
gcc -o edimh $(OBJECTS)
When make runs, it simply plugs in main.o edit.o
wherever you specify $(OBJECTS). If you have to add another
object file to the project, just specify it on the first line of
the file. The dependency line and command will then be updated
correspondingly.
Don't forget the parentheses when you refer to $(OBJECTS).
Macros may resemble shell variables like $HOME and $PATH, but they're not the same.
One macro can be defined in terms of another macro, so you could say
something like:
ROOT = /usr/local
HEADERS = $(ROOT)/include
SOURCES = $(ROOT)/src
In this case, HEADERS evaluates to the directory
/usr/local/include and SOURCES to /usr/local/src.
If you are installing this package on your system and don't want it to
be in /usr/local, just choose another name and change the line
that defines ROOT.
By the way, you don't have to use uppercase names for macros, but
that's a universal convention.
An extension in GNU make allows
you to add to the definition of a
macro. This uses a := string in place of an equal sign:
DRIVERS =drivers/block/block.a
ifdef CONFIG_SCSI
DRIVERS := $(DRIVERS) drivers/scsi/scsi.a
endif
The first line is a normal macro definition, setting the
DRIVERS macro to the filename
drivers/block/block.a. The next definition adds
the filename drivers/scsi/scsi.a. But it takes
effect only if the macro CONFIG_SCSI is defined.
The full definition in that case becomes:
drivers/block/block.a drivers/scsi/scsi.a
So how do you define CONFIG_SCSI? You could put it in the
makefile, assigning any string you want:
CONFIG_SCSI = yes
But you'll probably find it easier to define it on the make
command line. Here's how to do it:
papaya$ make CONFIG_SCSI=yes target_name
One subtlety of using macros is that you can leave them undefined. If
no one defines them, a null string is substituted (that is, you
end up with nothing where the macro is supposed to be). But this also
give you the option of defining the macro as an environment variable.
For instance, if you don't define CONFIG_SCSI in the makefile,
you could put this in your .bashrc file, for use with the
bash shell:
export CONFIG_SCSI=yes
Or put this in .cshrc if you use csh or tcsh:
setenv CONFIG_SCSI yes
All your builds will then have CONFIG_SCSI defined.