0

My Makefile looks like this:

BIN = bin OBJECTS = object1.o \ object2.o \ object3.o HDR = $(OBJECTS:%.o=%.h) header1.h header2.h MAIN = main.c CC = gcc CFLAGS = -Wall -g -std=c99 -fstack-protector-all LDFLAGS = -lpthread $(BIN): $(OBJECTS) $(MAIN) $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ %.o: %.c $(HDR) $(CC) $(CFLAGS) -c $< -o $@ 

It seems that the %.o: %.c $(HDR) rule is not used. When invoking with option make -r it says that there's no rule to make target object.o. The build of each object file should depend on every header file. What am I missing?

Edit: I should mention that when doing echo $(HDR) than it looks like the variable contains the right values: object1.h object2.h object3.h header1.h header2.h

1
  • Could you try make -d object.o, and look at the last twenty lines or so? Commented Oct 4, 2011 at 0:34

3 Answers 3

2

In the declaration of HDR, try $(OBJECTS:.o=.h) instead. Or, better yet, use gcc -MM or the like to generate your dependencies instead.

Sign up to request clarification or add additional context in comments.

3 Comments

Hm, okay, I have no idea what's going on then. But still, manual dependency management is kind of a bad idea, and you should look into gcc -MM to generate a .depend file or the like (see the link in my answer).
I just edited my question to mention that HDR (seems to) contains the correct values.
Okay, I have no idea what's going on here. It's still a good idea in general to use automatic dependency generation. Jonathan Leffler's answer seems more plausible.
2

A pattern rule can't have auxilliary dependencies like ${HDR}.

Use:

%.o : %.c $(CC) $(CFLAGS) -c $< -o $@ ${OBJECTS}: ${HDR} 

2 Comments

That's not what the GNU make manual says: gnu.org/software/make/manual/make.html#Pattern-Intro
Indeed the problem seems to be the $(HDR). When stating the prerequisites explicitly, like "%.o: %.c header1.h header2.h ..." it seems to work.
0

Ok, the given Makefile should work, I had a typo in one of the header file names.

It's a pitty, but make doesn't warn about that. It seems that when a pattern based rule is missing a prerequisite than it's just ignored. The built-in .o creation rule is used instead.

Jonathan Leffler's proposal of ${OBJECTS}: ${HDR} brought that up, because than there's an error regarding "no rule to make target misspelled.h" - I would have expected that from my rule too.

So I can just agree to fluffy, it's better to use auto-generated dependencies instead.

1 Comment

That's a feature. You can have more than one implicit rule rule that fits a target, and the first one whose prerequisites exist (or can be built) is the one Make uses.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.