Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

c - Recursive call to makefile does not update object files

I have a makefile with a recursive call to itself. When I first run make, the object files are created and linked just fine. But when I modify a source file and run make again, the objects are not recreated (make says the target is up to date). I need to run make clean and then run make. Here is simplified version of my makefile which I think includes all relevant information:

PCC = mpicc

#Locations
OBJDIR = ./objects

#Compiler and linker flags
FLAGS = -g -lm

#Object files
SHAREDOBJS = $(addprefix $(OBJDIR)/,shared1.o shared2.o)
SPECIFICOBJS = $(addprefix $(OBJDIR)/,specific1.o specific2.o)

#How to compile and link
$(OBJDIR)/%.o: %.c
    $(PCC) -c $*.c $(EXTRA_FLAGS) -o $(OBJDIR)/$*.o

PROGNAME:
    $(MAKE) $(MAKEFLAGS) EXTRA_FLAGS="$(FLAGS)" PROGNAME_TARGET

PROGNAME_TARGET: $(SHAREDOBJS) $(SPECIFICOBJS)
    $(PCC) $(SHAREDOBJS) $(SPECIFICOBJS) $(EXTRA_FLAGS) -o PROGNAME

So running make PROGNAME the first time compiles just fine. But the second returns make: "PROGNAME" is up to date.

It appears to me that the recursive call is never made. For example, if I add an echo right before the call to make, nothing is displayed in stdout.

Why is this happening? Why are the timestamps on the source files not checked in the recursive call? I don't understand why the recursion breaks the dependency on the source files.

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The target PROGNAME has no prerequisites.

The first time you make PROGNAME, Make sees that there is no such file, so it executes the rule.

The second time (after you modify shared1.o), Make sees that PROGNAME already exists, and that the target has no prerequisites, so it sees no need to rebuild the target. It doesn't know that PROGNAME depends on shared1.o, because you didn't tell it.

There's more than one way to solve this problem. I suggest you do away with the recursion entirely and use a target-specific variable value:

PROGNAME: EXTRA_FLAGS="$(FLAGS)"
PROGNAME: PROGNAME_TARGET

(Your target names could be improved, but that can wait.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...