Gcc makefile dependent无效
这是我的目录树:Gcc makefile dependent无效,gcc,makefile,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.h和commo
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.h
和common.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)
似乎我的依赖文件有问题,当我cata.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有问题吗?我尝试用-MMD
替换-MM-MF$(patsubst%.o,%.d,$@)
,但输出如下:
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
您可以在显示的
.d
文件的输出中看到问题:
a1.o: /home/haochen/Work/test_code/test_makefile/dir1/a1.c \
这里的目标是a1.o
。但是,您实际构建的目标是$(OUTPUT\u 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 $<)"
$(输出目录)/%.o:%.c
@mkdir-p$(输出目录)
$(CC)$(CFLAG)$(CC_DEFS)-I$(INC_PATH)-MM-MF$(patsubst%.o,%.d,$@)-MT$@-o$@<
@echo“cc$(notdir$尝试用-MMD
@user657267替换-MM-MF$(patsubst%.o,%.d,$@)
,我应该把我的依赖项放在哪里?放在底部或目标之前?对不起,我不明白,你需要用-MMD
@user657267替换我上面引用的整个部分,我的意思是如果我的-包括$(DEPS)
position是正确的吗?我尝试了你的建议,error ourput,我在我的问题中更新了它可以帮助你调用make--debug=v
来检测依赖项在哪里被破坏。它解决了问题,但我发现生成的*。o
是空的。使用上面的命令。我做了一个测试来使用gcc-o./a1.o./a1.c
,我可以生成c正确的对象文件,有任何注释吗?我使用$(CC)$(CFLAG)$(CC_DEFS)-I$(INC_PATH)-MMD-c$<-o$@
,似乎问题已经解决
$(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 $<)"