0

I have a test directory with a makefile like:

    EXECS = pgm1 pgm2 pgm3 pgm4 pgm5 ...

    OBJS = $(addsuffix .o, $(EXECS))

    all: $(EXECS)

    %.o : %.c
        $(CC) -c $< -o $@ $(IFLAGS)

    $(EXECS) : $(OBJS)
        $(CC) $@.o -o $@ $(LFLAGS)

    pgm1 : pgm1.o
        $(CC) $< -o $@ $(LFLAGS)

    % : %.o
        $(CC) $< -o $@ $(LFLAGS)

The pgm1 : pgm1.o link rule works, but I don't want to have to enter one for each program.

The % : %.o rule doesn't work (it picks up a default rule with no link flags)

The $(EXECS) : $(OBJS) rule works, but all the executables are linked every time a .o file changes. This doesn't really matter 'cos the programs are small, but it would be nice to fix.

Any suggestions, please?

Later...

Thanks to all for your suggestions. The solution is a gloriously simple combination of all of them:

% : %.c
    $(CC) $(IFLAGS) $< -o $@ $(LFLAGS)
ncvp
  • 21
  • 2

2 Answers2

0

The %:%.o rule should work, but I guess that make is using a higher-priority default rule as

%:%.c
    $(CC) $@.o -o $@

that compiles and links directly from the C, without your variables.

You can redefine this default rule to suit your need, or drop it by writing

%:%.c

with no command, see http://ftp.gnu.org/old-gnu/Manuals/make-3.79.1/html_chapter/make_10.html#SEC104

J.P. Tosoni
  • 549
  • 5
  • 15
0

The pattern rule doesn't work because you've already defined an explicit rule for the targets, in the $(EXECS) : $(OBJS) line. Explicit rules take precedence over implicit rules, like pattern rules. Remove the explicit rules and it will work as you want:

EXECS = pgm1 pgm2 pgm3 pgm4 pgm5 ...

all: $(EXECS)

%.o : %.c
    $(CC) -c $< -o $@ $(IFLAGS)

% : %.o
    $(CC) $< -o $@ $(LFLAGS)
MadScientist
  • 92,819
  • 9
  • 109
  • 136