Josh Olson
Josh Olson

Reputation: 407

How to precompile headers when building target in single step?

I have a simple makefile which finds the CPP files relative to it and builds the target from there. How could I modify this to also precompile the header files relative to it when building the target?

TARGET ?= application
CXXFLAGS += -Os -I. -MMD -MP -std=c++11    

SOURCES := $(shell find -L . -name '*.cpp')
OBJECTS := $(SOURCES:.cpp=.o)
DEPENDS := $(SOURCES:.cpp=.d)    

$(TARGET): $(OBJECTS) 
    $(CXX) $(OBJECTS) -o $@ $(LDLIBS)    

.PHONY: clean    

clean : 
    $(RM) $(TARGET) $(OBJECTS) $(DEPENDS)    

-include $(DEPENDS)

Upvotes: 3

Views: 770

Answers (1)

Josh Olson
Josh Olson

Reputation: 407

Looks like I'm not able to do what I had in mind. The solution here is to follow @Rup's advice and include the major dependencies in a single compiled header.

GCC - 3.21 Using Precompiled Headers

A precompiled header file can be used only when these conditions apply:

  • Only one precompiled header can be used in a particular compilation.

Upvotes: 2

Related Questions