83

Sometimes, I find .d files for a given source file. For instance, if I compile test.c, I've got

test.d, test.o

I understand that test.o is the object file but have no idea what is test.d for. Could you give any hints or pointers?

Vishnu CS
  • 748
  • 1
  • 10
  • 24
jaeyong
  • 8,951
  • 14
  • 50
  • 63

2 Answers2

84

Many build systems add automatically detected make dependencies into the .d file. In particular, for C/C++ source files they determine what #include files are required and automatically generate that information into the .d file.

The .d files are then included by the makefile so make is aware of that information. If you look at the contents of those files they'll be make prerequisite statements, like:

foo.o : foo.h bar.h biz.h

etc.

Vishnu CS
  • 748
  • 1
  • 10
  • 24
MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • 1
    Is there a better way to calculate and define the dependencies than to clutter your directory with .d files? – liv2hak May 17 '16 at 20:52
  • 5
    Many people configure their makefiles to write the `.d` files into a subdirectory so they don't clutter the current directory. – MadScientist May 18 '16 at 01:58
  • 1
    That would be a very useful thing for me.How can I do that? – liv2hak May 18 '16 at 02:05
  • 4
    The comments of a SO question are not the place to discuss it. It's not hard: basically wherever your current makefile writes to `$*.d` you write to `.deps/$*.d` or whatever instead. If you can't figure it out you can ask a new question, showing your current makefile content that generates dependencies then someone can help you modify it to write to a subdirectory. Also you can read http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/ – MadScientist May 18 '16 at 12:37
  • 1
    How do you use the .d files in a recipe to generate objects? – MarcusJ Aug 18 '17 at 11:48
  • Look for -MMD in https://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html – Ben Butterworth Oct 31 '19 at 18:24
1

the .d files contain rules and can be added to Makefile's existing rules.

Makefile fragment:

clean:
        rm -v *.o *.d

DEPS := $(wildcard *.d)
ifneq ($(DEPS),)
include $(DEPS)
endif
James Risner
  • 5,451
  • 11
  • 25
  • 47