单步构建目标时如何预编译头文件?

How to precompile headers when building target in single step?

我有一个简单的 makefile,它可以找到与其相关的 CPP 文件并从那里构建目标。我如何修改它以在构建目标时也预编译与其相关的头文件?

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)

看来我无法按照我的想法去做。这里的解决方案是遵循@Rup 的建议并将主要依赖项包含在单个编译头文件中。

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.