【发布时间】:2015-02-18 20:27:52
【问题描述】:
我正在尝试在多个目录中创建具有多个源的 Makefile。我非常接近,但依赖项无法正常工作。 .d 文件正在正确创建。它正确列出了sample.o:sample.c options.h。如果我更改 sample.c,它会重新编译和链接。如果我更改 options.h,它会说一切都是最新的。我认为问题在于将 .c 编译为 .o 的显式规则覆盖了 .d 文件中的规则。 问题是如何包含依赖项并定义自己的规则来构建它?
样本.c:
#include "options.h"
int main(int argc, char** argv) {return(0);}
options.h:
#define SAMPLE 1
目录结构
---------- dir1
options.h
---------- dir2
sample.c
---------- output
sample.o
sample.d
生成文件:
TARGET=output/sample
CC=gcc
CFLAGS=-Wall -O2 -g
LDFLAGS=-g
LIBS+=-lm
PATHS=-Idir1 -Idir1/dir2
CORE_SRC += sample.c
SRCS+= $(addprefix dir1/dir2/, $(CORE_SRC))
OBJS:= $(addprefix output/, $(SRCS:.c=.o))
DEPS:= $(OBJS:.o=.d)
.PHONY: all
all: $(TARGET)
$(TARGET) : $(OBJS) $(DEPS)
@echo "Linking ..."
$(CC) $(LDFLAGS) -o $@ $(OBJS)
output/%.d: %.c
@echo "generating dependency $@"
@mkdir -p output/$(dir $*.d)
$(CC) -MM $(CFLAGS) $(PATHS) $^ > output/$*.d
@mv -f output/$*.d output/$*.d.tmp
@sed -e 's|.*:|$*.o:|' < output/$*.d.tmp > output/$*.d
@sed -e 's/.*://' -e 's/\\$$//' < output/$*.d.tmp | fmt -1 | \
sed -e 's/^ *//' -e 's/$$/:/' >> output/$*.d
@rm -f output/$*.d.tmp
output/%.o: %.c output/%.d
@echo "Compiling $@ from $*.c"
@mkdir -p output/$(dir $*.o)
$(CC) -c $(CFLAGS) $(PATHS) $*.c -o output/$*.o
-include $(DEPS)
【问题讨论】:
-
我认为我编辑正确。如果我没有,请检查并修复它。
标签: makefile dependencies gnu