1

I was learning about make's vpath recently and I stumbled upon this question:

Makefile vpath not working for header files

Is there any point of using make's vpath for header (.h) files when the header files/directory still need to be included for g++ by using -I?

Community
  • 1
  • 1
Tek
  • 2,888
  • 5
  • 45
  • 73

1 Answers1

1

One reason I can think to add the "include" directory (for .h files) to the vpath would be for dependency checking.

Your .c files should depend on all of the .h files they include - so if you change a header file, all compilation units that include that might be affected are re-built.

Lets say you have a src/ directory for all of your .c files, and an include/ directory for all of your .h files. By using vpath for include/, and adding a -I include path for g++, you can simply refer to all the header files by name (in the Makefile), and not have to be concerned with their paths.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • Can't you just refer to them by name anyway without the vpath once you do a `-Iinclude/`? – Tek Jul 24 '13 at 06:11
  • Edited: (in the Makefile) – Jonathon Reinhart Jul 24 '13 at 06:11
  • Oh, interesting. Do you think dependency checking for .h files is useful considering `g++` will tell you if you're missing a header file anyway? – Tek Jul 24 '13 at 06:13
  • @Tek absolutely! Please re-read my second paragraph. Makefiles are all about keeping things *up-to-date* -- think re-building, not just building the first time. – Jonathon Reinhart Jul 24 '13 at 06:16
  • Sorry, I forgot to mention besides that. But to be clear on that matter, you mean to tell me that without the dependency checking it won't rebuild lets say an object `.o` when the header is changed? – Tek Jul 24 '13 at 06:20
  • Nope. If you have only `foo.o: foo.c`, how would Make know to look at the timestamp on `someother.h`? That said, you *can* do things with [automatic dependency generation](http://mad-scientist.net/make/autodep.html). – Jonathon Reinhart Jul 24 '13 at 06:57