makefile依赖无效

时间:2017-04-26 03:22:31

标签: gcc makefile

这是我的目录树:

tree ./
./
├── common
│   └── common.h
├── dir1
│   ├── a1.c
│   ├── a1.h
│   ├── b1.c
│   ├── b1.h
│   ├── c1.c
│   ├── c1.h
│   └── dir1.mk
├── makefile
└── sys-make
    ├── gcc.mk
    └── rules.mk

每个c文件都包含它自己的头文件和公共标题,a1.c包括a1.hcommon.h。我主要有4个makediles:

生成文件:

TOP_DIR = $(PWD)
MKFILE_DIR = $(TOP_DIR)/sys-make
OUTPUT_DIR = $(TOP_DIR)/output

export TOP_DIR MKFILE_DIR 

all:
    @echo "$(PWD)/makefile"
    @echo "$(CC_DEFS)"
    @$(MAKE) -f $(TOP_DIR)/dir1/dir1.mk


clean:
    @$(MAKE) -f $(TOP_DIR)/dir1/dir1.mk clean

dir1.mk:

MODULE = dir1

MOD_LIB = ./lib$(MODULE).a

SRC_FILES =
SRC_FILES += a1.c
SRC_FILES += b1.c
SRC_FILES += c1.c

INC_PATH =
INC_PATH += $(TOP_DIR)/common

SRC_PATH =
SRC_PATH += $(TOP_DIR)/dir1
SRC_PATH += $(TOP_DIR)/dir1/sub_dir1

vpath %.c $(SRC_PATH)

include $(MKFILE_DIR)/gcc.mk
include $(MKFILE_DIR)/rules.mk

gcc.mk

CC :=
CC = gcc

CFLAG :=
CFLAG = -ggdb -Wall

rules.mk

OUTPUT_DIR = $(TOP_DIR)/output/$(MODULE)
OBJS = $(addprefix $(OUTPUT_DIR)/,$(SRC_FILES:.c=.o))
DEPS = $(addprefix $(OUTPUT_DIR)/,$(SRC_FILES:.c=.d))

$(OUTPUT_DIR)/$(MOD_LIB): $(OBJS)
    @ar cr $@ $^
    @echo "ar $@"

$(OUTPUT_DIR)/%.o: %.c
    @echo "objs: $(OBJS)"
    @echo "deps: $(DEPS)"
    @echo "outs: $(OUTPUT_DIR)"
    @mkdir -p $(OUTPUT_DIR)
    $(CC) $(CFLAG) $(CC_DEFS) -I$(INC_PATH) -MM -MF  $(patsubst %.o,%.d,$@) -o $@ $< 
    @echo "cc $(notdir $<)"

clean:
    -@rm -r $(OUTPUT_DIR)

-include $(DEPS)

首次编译后,没有问题*.o*.d*.a生成到./output/dir1。问题是,无论我触摸*1.h还是common.h,都会显示:

make[1]: `/home/haochen/Work/test_code/test_makefile/output/dir1/./libdir1.a' is up to date.

似乎我的依赖文件有问题,当我cat a.d时,它显示:

cat output/dir1/a1.d
a1.o: /home/haochen/Work/test_code/test_makefile/dir1/a1.c \
 /home/haochen/Work/test_code/test_makefile/dir1/a1.h \
 /home/haochen/Work/test_code/test_makefile/common/common.h

我的makefile中的任何问题?我尝试将-MM -MF $(patsubst %.o,%.d,$@)替换为-MMD,但输出如下:

make                                                                                                                                                                                                                                                                                                             
/home/haochen/Work/test_code/test_makefile/makefile
SYS1 SYS2 SYS3 SYS4
make[1]: Entering directory `/home/haochen/Work/test_code/test_makefile'
objs: /home/haochen/Work/test_code/test_makefile/output/dir1/a1.o /home/haochen/Work/test_code/test_makefile/output/dir1/b1.o /home/haochen/Work/test_code/test_makefile/output/dir1/c1.o
deps: /home/haochen/Work/test_code/test_makefile/output/dir1/a1.d /home/haochen/Work/test_code/test_makefile/output/dir1/b1.d /home/haochen/Work/test_code/test_makefile/output/dir1/c1.d
outs: /home/haochen/Work/test_code/test_makefile/output/dir1
gcc -ggdb -Wall  -I /home/haochen/Work/test_code/test_makefile/common -MMD -o /home/haochen/Work/test_code/test_makefile/output/dir1/a1.o /home/haochen/Work/test_code/test_makefile/dir1/a1.c
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
make[1]: *** [/home/haochen/Work/test_code/test_makefile/output/dir1/a1.o] Error 1
make[1]: Leaving directory `/home/haochen/Work/test_code/test_makefile'
make: *** [all] Error 2

1 个答案:

答案 0 :(得分:1)

您可以在显示的.d文件的输出中看到问题:

a1.o: /home/haochen/Work/test_code/test_makefile/dir1/a1.c \

此处的目标是a1.o。但是,您实际构建的目标是$(OUTPUT_DIR)/a1.o。因此,make不会将这些额外的先决条件应用于该目标,因此它不会检查它们以确定它们在构建目标时是否已更改。

您需要将目标名称作为您正在构建的确切目标:您可以使用-MT选项。例如:

$(OUTPUT_DIR)/%.o: %.c
        @mkdir -p $(OUTPUT_DIR)
        $(CC) $(CFLAG) $(CC_DEFS) -I$(INC_PATH) -MM -MF $(patsubst %.o,%.d,$@) -MT $@ -o $@ $< 
        @echo "cc $(notdir $<)"
相关问题