与 gmake 的自动头依赖关系 [英] Automatic header dependencies with gmake

查看:29
本文介绍了与 gmake 的自动头依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已编辑

我正在尝试重新编译源文件,而不必为 makefile 中的每个 CPP 指定头文件.

I'm trying to have source files recompiled without having to specify header files for each CPP in the makefile.

我想:

#CoreObj1.cpp(and .h)
#CoreObj2.cpp(and .h)

#This is the makefile.

CORE_COMPONENT_OBJECTS = 
  obj/CoreObj1.o 
  obj/CoreObj2.o 

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

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

DEPS = $(CORE_COMPONENT_OBJECTS:.o=.d)

ifneq ($(MAKECMDGOALS),clean)
-include $(DEPS)
endif   

但是修改一个头文件不会触发包含它的源文件被重新编译.

But modifying a header files does not trigger the source files including it to be recompiled.

注意: 事实上,如果我的 .o、.d 和 .cpp 在同一个文件夹中,它就可以工作.但是如果我的 .d 和 .o 在 obj/文件夹中,它不会触发重新编译.

NOTE: In fact, it works if my .o, .d and .cpp are in the same folder. But if my .d and .o are in a obj/ folder, it doesn't trigger the recompile.

推荐答案

您没有将依赖文件作为编译规则的先决条件.应该是这样的:

You don't have dependency file as a prerequisite for compilation rule. Should be something like this:

#This is the rule for creating the dependency files
src/%.d: src/%.cpp
    $(CXX) $(CXX_CFLAGS) -MM -MF $(patsubst obj/%.o,obj/%.d,$@) -o $@ $<

obj/%.o: %.cpp %.d
    $(CXX) $(CXXFLAGS) -o $@ -c $<

-include $(SRC:%.cpp=%.d)

最后一个字符串增加了对标题的依赖.

The last string adds dependency on headers.

编辑

我看你有

-include $(DEPS)

但检查 $(warning DEPS = $(DEPS)) 是否真的包含现有文件,否则只需默默忽略这些.

but check with $(warning DEPS = $(DEPS)) whether you really include existing files, otherwise make just silently ignore these.

这篇关于与 gmake 的自动头依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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