tez
tez

Reputation: 5300

Makefile pattern matching failure

BINS = $(patsubst %.c, %, $(SRCS))

all: $(BINS)

%: %.o
        $(info ---------- linking $< ---------)
        $(CC) $< -o $@ -L$(LIBPATH) -llibrary

Will name in $(BINS) match %? I need the rule %: %.o to be invoked which in turn calls other rule to create .os. But this match is not happening due to which implicit rules are getting triggered.

My goal is to create binaries with same name as their .c files with out the extension. .os should be created in the process

Upvotes: 0

Views: 55

Answers (2)

Jens
Jens

Reputation: 72639

This might sound drastic, but you should remove all rules, then it will work. Yes, I'm serious. make has built-in rules to create foo from foo.c by compiling, since it is so common. Watch it happen:

$ cat hello.c
#include <stdio.h>

int main (void)
{
    printf ("hello, world\n");
    return 0;
}
$ cat Makefile
cat: Makefile: No such file or directory

$ make CFLAGS=-lm hello
cc -lm  hello.c  -o hello
$ ./hello
hello, world
$

More realistically, if you want to compile a set of executables each from their *.c counterpart, all you need in your Makefile is

all: prog1 prog2 progN

Sweet, ain't it?

Upvotes: 2

laune
laune

Reputation: 31290

This works for me:

all : ${BINS}

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

$(BINS) : %: %.o
    @echo ---------- linking $< ---------
    $(CC) -o $@ $< -L$(LIBPATH) -llibrary

It would be simpler to compile and link in one go:

$(BINS) : %: %.c
    @echo ---------- compiling and linking $< ---------
    $(CC) -o $@ $<

Upvotes: 1

Related Questions