help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Automatic dependencies and clean rule


From: Philip Guenther
Subject: Re: Automatic dependencies and clean rule
Date: Wed, 27 Mar 2013 20:36:09 -0700

On Wed, Mar 27, 2013 at 10:19 AM, Nick Andrik <address@hidden> wrote:
> I have tuned my Makefile for automatic dependencies generation and it
> looks like this
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> OBJDIR  := objs/
> OBJS    := $(SRCS:%.cpp=$(OBJDIR)%.o)
> DEPDIR  := $(OBJDIR)
> DEPS    := $(SRCS:%.cpp=$(DEPDIR)%.d)
> TARGET   := prog
>
>
> # Default target
> $(TARGET): $(OBJS)
>         $(LD) $(LDFLAGS) -o $@ $^
>
> $(OBJDIR)%.o: %.cpp
>         test -d $(OBJDIR) || mkdir -p $(OBJDIR)
>         $(CXX) $(CXXFLAGS) -c $< -o $@
>
> $(DEPDIR)%.d: %.cpp
>         test -d $(DEPDIR) || mkdir -p $(DEPDIR)
>         $(CXX) $(CXXFLAGS) $< -MM -MG -MP -MT '$(OBJDIR)$*.o' -MF $@
>
> clean:
>         rm -f  $(TARGET)
>         rm -rf  $(OBJDIR) $(DEPDIR)
>
> -include $(DEPS)
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> My problem is that every time that I run make clean, the rules for
> creating DEPS are executed again.
> Is there any way to tell the Makefile to ignore the final include line
> (or empty the DEPS var) when I run make clean?

The really short answer is that you should read
        http://make.paulandlesley.org/autodep.html#advanced


> In my case I need the .d files before I generate the .o ones since I
> use generated files from flex/bison and I need to respect the
> dependencies.

When there are file that require two stages to build, the second of
which may require dependencies that you cannot calculate until the
first is done (the lexer.c file might not be compilable without the
parser.tab.h file generated by bison).  So, to auto-generate
dependencies for the .o files you'll need to previously inform make of
the relevant ".c: .y" and ".c: .l" dependencies.

My preference is to declare the generated .h files to be order-only
dependencies of *all* the .o files.  That way make will know that it
has to generate the .h before it tries to compile the .o's, but once
the .h exists the .o's will only be recompiled if the auto-dependency
bits generate a *real* dependency on the .h.

${OBJS} : | parser.tab.h

(...or whatever the name of the generated .h file(s) are)


Philip Guenther



reply via email to

[Prev in Thread] Current Thread [Next in Thread]