3

I have a very simple makefile, that basically does the following:

# Pre-compiled header
CORE_PCH_FILENAME =Core.h

CORE_PCH: 
        $(CXX) $(CXX_CFLAGS) -x c++-header $(CORE_PCH_FILENAME)

#Objects
obj/%.o: CORE_PCH %.cpp obj/%.d
        @mkdir -p obj
        $(CXX) $(CXX_CFLAGS) -c $*.cpp -o $@

#Dependencies 
obj/%.d: %.cpp
        @mkdir -p obj
        $(CXX) $(CXX_CFLAGS) -MM -MT obj/$*.o -MF $@ $<

My problem is, the first time I make, the Core.h.gch gets created and the project gets built. That's fine.

But, even if change nothing, running make again will re-create the Core.h.gch and recompile everything.

If I don't have a pre-compiled header, make behaves fine (will not rebuild anything if nothing has changed, and rebuild only what's necessary if modifications were made)

But I'd really like to have that pre-compiled header, is there something I'm not doing right!?

EDIT:

The following was suggested:

# Pre-compiled header
$(CORE_PCH_FILENAME) =Core.h
CORE_PCH: $(CORE_PCH_FILENAME)
    $(CXX) $(CXX_CFLAGS) -x c++-header $(CORE_PCH_FILENAME)

But it still gets created every time :(

zedxz
  • 113
  • 1
  • 8

2 Answers2

6

The problem is that you do not refer to the gch by name, and there is no file named CORE_PCH, literally. Try:

CORE_PCH_FILENAME=Core.h
CORE_PCH=$(CORE_PCH_FILENAME).gch

$(CORE_PCH):
    $(CXX) $(CXX_CFLAGS) -x c++-header $(CORE_PCH_FILENAME)
rodrigo
  • 94,151
  • 12
  • 143
  • 190
2

CORE_PCH is in the pre-requisites for obj/%.o, and it has no pre-requisites itself, so it always gets built. If you add Core.h to the pre-requisites for CORE_PCH, that should take care of it.