How to manage C header file dependencies?

前端 未结 2 1120
醉话见心
醉话见心 2020-12-06 23:51

I\'ve a lot of C files, some have a header (.h), some files not.

Here\'s my makefile :

.SUFFIXES: 

SRC := $(wildard ./src/*.c)
OBJ := $(SRC:%.c=%.o)         


        
相关标签:
2条回答
  • 2020-12-07 00:08

    (I'm not sure how this is not stating the obvious, but anyway:)

    Add rules to list those dependencies explicitly, file by file. Preferably in a separate makefile that you include from the main one.

    Tools exist (such as gcc) that can generate them for you; if you can't use or build such a tool, you'll need to maintain these rules yourself.

    0 讨论(0)
  • 2020-12-07 00:17

    The standard approach is to generate header dependencies automatically while compiling.

    For the first compilation no dependencies are necessary since every source file must be compiled. Subsequent recompilations load dependencies generated by the previous compilation to determine what needs to be recompiled.

    Your $(MyNotGCCCompiler) is likely to have a command line option to generate a dependencies file.

    When using gcc it works like this:

    .SUFFIXES: 
    
    SRC := $(wildard ./src/*.c)
    OBJ := $(SRC:%.c=%.o)
    DEP := $(OBJ:%.o=%.d)
    
    all: $(OBJ)
    
    # when compiling produce a .d file as well 
    %.o: %.c
        gcc -c -o $@ $(CPPFLAGS) $(CFLAGS) -MD -MP -MF ${@:.o=.d} $<
    
    # don't fail on missing .d files
    # there won't be any on the first run
    -include $(DEP) 
    
    0 讨论(0)
提交回复
热议问题