Makefile规则可以重新生成目标文件目录,而无需从头开始重新编译? [英] `Makefile` rule to regenerate object files directory without recompiling from scratch?

查看:660
本文介绍了Makefile规则可以重新生成目标文件目录,而无需从头开始重新编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Makefile效果很好,但是每次从头开始重新编译,即使没有任何更改.

I have the following Makefile which works great but everytime it is recompiling from start evenif nothing has changed.

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 $@

问题似乎出在这些行上

$(OBJDIR)/%.o: $(SOURCEDIR)/%.cpp Makefile $(OBJDIR)
    $(CXX) $(WARNING) $(CXXFLAGS) $(INC_PATH) -MMD -MP -c $< -o $@

,尤其是$(OBJDIR)中的依赖项(保存了*.o*.d文件的),实际上,当我删除它时,似乎无法重新编译.问题是,如果删除$(OBJDIR),则不会再次重新生成目录.

and particularly the dependency from $(OBJDIR) (where *.o and *.d files are saved) in fact when I remove it seems not to recompile. The problem is that if I remove the $(OBJDIR), the directory is not regenerated again.

Makefile规则如何重新生成目标文件的存储目录,而无需从头开始进行所有编译?

What is the Makefile rule to regenerate the directory where object files are stored without starting all the compilation from scratch?

推荐答案

$(OBJDIR)是对象文件的先决条件.与任何目录一样,其上一次修改时间随其内容的更改而改变...声明为仅限订购的先决条件:

$(OBJDIR) is a prerequisite of your object files. 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)

这样,如果已经存在,则make不会考虑其最后修改时间来决定需要重建哪些目标.

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.

这篇关于Makefile规则可以重新生成目标文件目录,而无需从头开始重新编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆