自动头依赖用gmake命令 [英] Automatic header dependencies with gmake

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

问题描述

EDITED

我想有源文件重新编译,而无需在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.

推荐答案

您不必依赖文件为prerequisite编译规则。应该是这样的:

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)

$检查(警告DEPS = $(DEPS))你是否真的有存在的文件,以其他方式只是默默地忽略这些。

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

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

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