GCC dependency generation for a different output directory

后端 未结 7 434
深忆病人
深忆病人 2021-01-30 14:30

I\'m using GCC to generate a dependency file, but my build rules put the output into a subdirectory. Is there a way to tell GCC to put my subdirectory prefix in the dependency f

7条回答
  •  梦如初夏
    2021-01-30 15:16

    You may like this briefer version of Don McCaughey's answer:

    SRCS = \
        main.c \
        foo.c \
        stuff/bar.c
    

    DEPS = $(SRCS:.c=.d)

    Add -include $(DEPS) note the - prefix, which silences errors if the .d files don't yet exist.

    There's no need for a separate pattern rule to generate the dependency files. Simply add -MD or -MMD to your normal compilation line, and the .d files get generated at the same time your source files are compiled. For example:

    %.o: %.c
         gcc $(INCLUDE) -MMD -c $< -o $@
    
    # -MD can be used to generate a dependency output file as a side-effect of the compilation process.
    

提交回复
热议问题