[英]`Makefile` rule to regenerate object files directory without recompiling from scratch?
I have the following Makefile
which works great but everytime it is recompiling from start evenif nothing has changed. 我有以下
Makefile
,该Makefile
运行良好,但每次从头开始重新编译,即使没有任何更改。
CXX = g++
CXXFLAGS = -std=c++11
INC_PATH = `pkg-config --cflags ../openCV/build/lib/pkgconfig/opencv.pc` \
`pkg-config --cflags ../SDL2-2.0.8/instDir/lib/pkgconfig/sdl2.pc` \
`pkg-config --cflags ../jsoncpp/build/pkg-config/jsoncpp.pc` \
-I ../poco/instDir/include/
#LIB_PATH = -L../cmake_bin_dir/lib/ ./gainput/build/lib -L../SDL2-2.0.8/build/ -L../SDL2-2.0.8/build/lib
LIBS = `pkg-config --libs ../openCV/build//lib/pkgconfig/opencv.pc` \
`pkg-config --libs ../SDL2-2.0.8/instDir/lib/pkgconfig/sdl2.pc` \
`pkg-config --libs ../jsoncpp/build/pkg-config/jsoncpp.pc` \
-L../poco/instDir/lib/ -lPocoNetd -lPocoUtild -lPocoFoundationd \
SOURCEDIR := ./
SOURCES := $(wildcard $(SOURCEDIR)/*.cpp)
OBJDIR=$(SOURCEDIR)/obj
OBJECTS := $(patsubst $(SOURCEDIR)/%.cpp,$(OBJDIR)/%.o, $(SOURCES))
DEPENDS := $(patsubst $(SOURCEDIR)/%.cpp,$(OBJDIR)/%.d,$(SOURCES))
# ADD MORE WARNINGS!
WARNING := -Wall -Wextra
# .PHONY means these rules get executed even if
# files of those names exist.
.PHONY: all clean
# The first rule is the default, ie. "make",
# "make all" and "make parking" mean the same
all: parking
clean:
$(RM) $(OBJECTS) $(DEPENDS) parking
# Linking the executable from the object files
parking: $(OBJECTS)
$(CXX) $(WARNING) $(CXXFLAGS) $(INC_PATH) $^ -o $@ $(LIBS)
-include $(DEPENDS)
$(OBJDIR):
mkdir -p $(OBJDIR)
$(OBJDIR)/%.o: $(SOURCEDIR)/%.cpp Makefile $(OBJDIR)
$(CXX) $(WARNING) $(CXXFLAGS) $(INC_PATH) -MMD -MP -c $< -o $@
The problem seems to be these lines 问题似乎是这些行
$(OBJDIR)/%.o: $(SOURCEDIR)/%.cpp Makefile $(OBJDIR)
$(CXX) $(WARNING) $(CXXFLAGS) $(INC_PATH) -MMD -MP -c $< -o $@
and particularly the dependency from $(OBJDIR)
(where *.o
and *.d
files are saved) in fact when I remove it seems not to recompile. 尤其是
$(OBJDIR)
的依赖项$(OBJDIR)
*.o
和*.d
文件)实际上在我删除它时似乎没有重新编译。 The problem is that if I remove the $(OBJDIR)
, the directory is not regenerated again. 问题是,如果删除
$(OBJDIR)
,则不会再次重新生成目录。
What is the Makefile
rule to regenerate the directory where object files are stored without starting all the compilation from scratch? Makefile
规则用于重新生成存储目标文件的目录而不从头开始所有编译的规则是什么?
$(OBJDIR)
is a prerequisite of your object files. $(OBJDIR)
是目标文件的先决条件。 As with any directory, its last modification time changes every time its content changes... Declare it as an order-only prerequisite instead: 与任何目录一样,其上一次修改时间随其内容的更改而改变...声明为仅订购的先决条件 :
$(OBJDIR)/%.o: $(SOURCEDIR)/%.cpp Makefile | $(OBJDIR)
This way, if it exists already, its last modification time will not be considered by make to decide which targets need to be re-built. 这样,如果已经存在,则make不会考虑其最后修改时间来决定需要重建哪些目标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.