Generate all project dependencies in a single file using gcc -MM flag

前端 未结 2 1356
余生分开走
余生分开走 2021-02-04 17:35

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

2条回答
  •  执念已碎
    2021-02-04 18:09

    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)
    

    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 +++++++++++

    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
    

    This does two things:

    1. If any of the files that foo.c depend on change then foo.o is rebuilt without having to rebuild other files in the project.
    2. The dep file itself has the same dependencies as the object file, so that if any of the deps are modified the dep file itself is also regenerated, before the object file deps are checked.

提交回复
热议问题