makefile gcc -MD,一些.d文件为空

时间:2015-08-06 13:21:45

标签: gcc makefile

我已经在这个问题上工作了很长时间,比如8个小时,请帮忙。

这是我的makefile(在deveop中,应该有一些其他问题):

.PHONY = all clean

#global directory defined
TOPDIR     = $(shell pwd)
SRCDIR     = $(TOPDIR)/src
LIBDIR     = $(TOPDIR)/lib
OBJECTDIR  = $(TOPDIR)/build
#maybe used in future.
INCLUDEDIR = 


CROSS_COMPILE = arm-none-eabi-

CC      = $(CROSS_COMPILE)gcc
AS      = $(CROSS_COMPILE)as 
LD      = $(CROSS_COMPILE)gcc
OBJCP   = $(CROSS_COMPILE)objcopy
AR      = $(CROSS_COMPILE)ar
NM      = $(CROSS_COMPILE)nm
STRIP   = $(CROSS_COMPILE)strip
RANLIB  = $(CROSS_COMPILE)ranlib

#local host tools defined
CP       := cp
RM       := rm
MKDIR    := mkdir
SED      := sed
FIND     := find
MKDIR    := mkdir
XARGS    := xargs

#target name
TARGET       = sat_fw
TARGETBIN    = $(TARGET).bin
TARGETELF    = $(TARGET).elf
#static lib, run make sat_fw.a
TARGETLIBS   = $(TARGET).a
#dynamic lib, run make sat_fw.so
TARGETSLIBS  = $(TARGET).so

#.c .o and .d files defined
VPATH       = $(shell ls -AxR $(SRCDIR)|grep ":"|grep -v "CVS"|tr -d ':')
SOURCEDIRS  = $(VPATH)
C_SOURCES   = $(foreach subdir,$(SOURCEDIRS),$(wildcard $(subdir)/*.c))
S_SOURCES   = $(foreach subdir,$(SOURCEDIRS),$(wildcard $(subdir)/*.s))
SOURCES     = $(C_SOURCES) $(S_SOURCES)

C_OBJS     = $(patsubst %.c,%.o,$(C_SOURCES))
S_OBJS     = $(patsubst %.s,%.so,$(S_SOURCES))
SRCOBJS    = $(C_OBJS) $(S_OBJS)
BUILDOBJS  = $(subst $(SRCDIR),$(OBJECTDIR),$(SRCOBJS))

DEPS        = $(patsubst %.o,%.d,$(BUILDOBJS))
DEPS        = $(patsubst %.so,%.d,$(BUILDOBJS))

#external include file define
CFLAGS  = -O2 -Wall -MD $(foreach dir,$(INCLUDEDIR),-I$(dir))
CFLAGS += -mcpu=arm1176jz-s -mlittle-endian
CFLAGS += -ffunction-sections -fdata-sections
CFLAGS += -fshort-enums -fomit-frame-pointer -fno-strict-aliasing
CFLAGS += -Wall -std=c99

ARFLAGS   = rc

#c file compile parameters and linked libraries
LDFLAGS = -nostartfiles -T sat.ld
XLDFLAGS  = -Xlinker "-(" $(LDFLAGS) -Xlinker "-)"
LDLIBS   += -L $(LIBDIR) 

#或$(TARGETSLIBS)到all中
#defaut target:compile the currrent dir file and sub dir 
all: $(TARGET)

#for .h header files dependence
-include $(DEPS)

$(TARGET_BIN): $(TARGET_ELF)
    $(OBJCP) $(CPFLAGS) $< $@

$(TARGET_ELF): $(BUILDOBJS)
    $(LD) $(LDFLAGS) -o  $@  $^ 

$(TARGET): $(BUILDOBJS)
    @$(CC) $(subst $(SRCDIR),$(OBJECTDIR),$^) $(CFLAGS) $(XLDFLAGS) -o $@ $(LDLIBS) 
    @$(STRIP) --strip-unneeded $(TARGET)

$(TARGETLIBS): $(BUILDOBJS)
    @$(AR) $(ARFLAGS) $@ $(BUILDOBJS)
    @$(RANLIB) $@

$(TARGETSLIBS): $(BUILDOBJS)
    @$(CC) -shared $(subst $(SRCDIR),$(OBJECTDIR),$^) $(CFLAGS) $(XLDFLAGS) -o $@ $(LDLIBS)

$(OBJECTDIR)%.o: $(SRCDIR)%.c 
    @[ ! -d $(dir $(subst $(SRCDIR),$(OBJECTDIR),$@)) ] & $(MKDIR) -p $(dir $(subst $(SRCDIR),$(OBJECTDIR),$@))
    @$(CC) $(CFLAGS) -o $(subst $(SRCDIR),$(OBJECTDIR),$@) -c $<

$(OBJECTDIR)%.so: $(SRCDIR)%.s
    @echo target: $($@) prere: $($<)
    @[ ! -d $(dir $(subst $(SRCDIR),$(OBJECTDIR),$@)) ] & $(MKDIR) -p $(dir $(subst $(SRCDIR),$(OBJECTDIR),$@))
    @$$(AS) $(AS_FLAGS) -o $(subst $(SRCDIR),$(OBJECTDIR),$@) -c $<

clean:
    @$(FIND) $(OBJECTDIR) -name "*.o" -o -name "*.d" | $(XARGS) $(RM) -f
    @$(RM) -f $(TARGET) $(TARGETLIBS) $(TARGETSLIBS)

运行后,它会在构建中创建文件夹,并生成.d文件,但大多数都是空的,我查看了相应的.c文件,包括部分,看起来没什么特别的。

和另一个问题:在第一次make之后,干净的目标将无法正常工作。 如果我删除构建文件夹,然后尝试清理,它将构建为没有'干净'输入。

有什么问题吗?

2 个答案:

答案 0 :(得分:0)

我通过添加如下参数解决了这个问题:

$(CC) -MMD -MG -MF $@ $(CFLAGS)

-MF  write the generated dependency rule to a file
-MG  assume missing headers will be generated and don't stop with an error
-MMD generate dependency rule for prerequisite, skipping system headers 
and put into .d file.

只有上面的MD是不够的。

答案 1 :(得分:0)

第二个问题的答案是:

事实是.d每次都重建,添加以下代码将有所帮助:

ifneq ($(MAKECMDGOALS),clean)
DEPS   = $(patsubst %.o,%.d,$(BUILDOBJS))
endif