使用gcc -MM标志在单个文件中生成所有项目依赖关系 [英] Generate all project dependencies in a single file using gcc -MM flag
问题描述
我想生成一个依赖关系文件,它由源文件的所有依赖关系通过Makefile使用gcc -M标志。我为此解决方案搜索,但是所提到的所有解决方案都是为多个对象生成多个deps文件。
I want to generate a single dependency file which consists of all the dependencies of source files using gcc -M flags through Makefile. I googled for this solution but, all the solutions mentioned are for generating multiple deps files for multiple objects.
DEPS = make.dep
$(OBJS): $(SOURCES)
@$(CC) -MM $(SOURCEs) > $(DEPS)
@mv -f $(DEPS) $(DEPS).tmp
@sed -e 's|.$@:|$@:|' < $(DEPS).tmp > $(DEPS)
@sed -e 's/.*://' -e 's/\\$$//' < $(DEPS).tmp | fmt -1 | \
sed -e 's/^ *//' -e 's/$$/:/' >> $(DEPS)
@rm -f $(DEPS).tmp
但它是不能正常工作请告诉我我犯错误的地方。
But it is not working properly. Please tell me where i'm making the mistake.
推荐答案
这些方面的东西是我用来取得我所有依赖的东西一个文件:
Something along these lines is what I use to get all my dependencies in a single file:
program_H_SRCS := $(wildcard *.h)
program_C_SRCS := $(wildcard *.c)
DEPS = make.deps
make.deps: $(program_C_SRCS) $(program_H_SRCS)
$(CC) $(CPPFLAGS) -MM $(program_C_SRCS) > make.deps
include $(DEPS)
这基本上导致所有在项目中的任何C或H文件被修改时,用户(而不是系统)依赖关系被重建成单个文件。
This basically causes all the user ( as opposed to system ) dependencies to be rebuilt into a single file whenever any C or H file in the project is modified.
+++++ ++++ EDIT +++++++++++
我发现了一个更好的做事方式。我为每个源文件生成一个单独的dep文件。这是基本的makefile:
I've since found a better way of doing things. I generate a separate dep file for each source file. Here is the basic makefile:
program_NAME := myprogram
program_SRCS := $(wildcard *.c)
program_OBJS := ${program_SRCS:.c=.o}
clean_list += $(program_OBJS) $(program_NAME)
# C Preprocessor Flags
CPPFLAGS +=
# compiler flags
CFLAGS += -ansi -Wall -Wextra -pedantic-errors
.PHONY: all clean distclean
all: $(program_NAME)
clean:
@- $(RM) $(clean_list)
distclean: clean
# Generate dependencies for all files in project
%.d: $(program_SRCS)
@ $(CC) $(CPPFLAGS) -MM $*.c | sed -e 's@^\(.*\)\.o:@\1.d \1.o:@' > $@
clean_list += ${program_SRCS:.c=.d}
$(program_NAME): $(program_OBJS)
indent -linux -brf $(program_SRCS)
splint $(program_SRCS)
$(LINK.c) $(program_OBJS) -o $(program_NAME)
ifneq "$(MAKECMDGOALS)" "clean"
# Include the list of dependancies generated for each object file
-include ${program_SRCS:.c=.d}
endif
这有两件事:
- 如果有任何foo的文件。 c依赖于更改,然后重建foo.o,而无需重新构建项目中的其他文件。
- dep文件本身具有与对象文件相同的依赖关系,因此如果任何deps被修改,dep文件本身也被重新生成,在对象文件deps被检查之前。
这篇关于使用gcc -MM标志在单个文件中生成所有项目依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!