2

I have a macro which is used in afile.c and bfile.c (both in module A) This macro is in aheader.h

Both of these are in different modules/directories and aheader.h module is complied before module A is complied.

Now one way is to do #include "aheader.h" in each of the .c files. But instead of doing this, is there a way to make some addition in the Makefile (like adding it to the list of headers) for module A, so that aheader.h is picked for everywhere the macro is used?

Jeyaram
  • 9,158
  • 7
  • 41
  • 63
Sunny
  • 7,444
  • 22
  • 63
  • 104
  • Possible duplicate of http://stackoverflow.com/questions/11346935/how-to-include-header-file-through-makefile – Jeyaram Jun 25 '13 at 11:51
  • `aheader.h` is not compiled. it is a header, it can only be `#include`d. – Elazar Jun 25 '13 at 11:52
  • @Jeyaram, close, but not exact duplicate, because he want to include only when the macro is used. – ugoren Jun 25 '13 at 11:53
  • The real question is: *why* do you need such feature? – Elazar Jun 25 '13 at 11:56
  • We have a multi-platform code base that we are trying to compile after integrating a new functionality. And since this macro is required in many places, it was thought to be better to just include it via make – Sunny Jun 25 '13 at 12:02
  • If you use a marco you have created in a header, always include that header. A subsequent programmer on the project hasn't got a clue where to look where that macro came from. – hetepeperfan Jun 25 '13 at 12:23

1 Answers1

3

#include "aheader.h" is the simple and correct thing to do. C has no feature to auto-include headers when a macro is used.

If you insist on doing it in the makefile, you can add -include aheader.h as a compilation flag. It will include it in all files.

It's possible to use the makefile to add this flag only when the macro is found in the C file, by using grep. But it's complicated makefile work, and I think you're better off without it.

ugoren
  • 16,023
  • 3
  • 35
  • 65
  • I agree it is better to do a #include. But i just wanted to get my basics clear – Sunny Jun 25 '13 at 11:55
  • So would it be something like: CFLAGS + = -include aheader.h – Sunny Jun 25 '13 at 11:56
  • Yes, though this will apply to all files. Doing it for those which use the macro is significantly more complicated. – ugoren Jun 25 '13 at 13:47
  • One question: CFLAGS += -include aheader.h bheader.h when i do this, it does not pick bheader.h? could you please help – Sunny Jun 26 '13 at 10:35
  • `-include aheader.h -include bheader.h`. The way you did it, `bheader.h` was unrelated to the `-include` parameter. – ugoren Jun 26 '13 at 12:08