0

I'm going rounds with the compiler right now, and I want to make sure the problem isn't a fundamental misunderstanding of how header files. If I include a header file, and that header file has includes in it (like <stdbool.h> or <stdio.h>, etc...), there shouldn't be any issue on the dependent C file, right? The preprocessor should just insert the assembled code accordingly when I call my makefile, by my understanding. Am I mistaken?

To reiterate:

  • Say I have a main.c with prototype.h.

  • prototype.h has in it all of my usual includes for libraries and what-not.

  • I have a couple other C files (secondary.c and tertiary.c, for instance), both of which need the usual libraries, as well, and may or may not need some of the prototypes and also have their own header files.

All I'd need to do is include prototype.h in each of the C files, correct?

Also, in that instance, if I were making .o file using the -c flag of gcc in my makefile, would I need to update the dependency in the target of the makefile?

I thought I had a solid handle on this when I started, but now I'm thoroughly confused.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Araymer
  • 1,315
  • 1
  • 10
  • 16
  • Wrong. Each source file must include the headers necessary for the compilation of instructions **contained in that source file**. (a source file can of course have a header that includes the necessary header files -- but you can't rely on the fact that some other file somewhere in your code also includes that file -- compilers don't work that way) Each source file is considered a **compilation unit** and it must have the needed headers directly available to it. – David C. Rankin Jun 27 '15 at 04:28
  • Yes, including a header from within another header still works, as long as it's included either directly or indirectly by the source files that need it... but it's a good idea to include the headers right in the files that require them anyway, though, if for no other reason than clarity. – Dmitri Jun 27 '15 at 04:37
  • See [Should I use `#include` in headers?](http://stackoverflow.com/questions/1804486/), to which the answer is 'yes'. You should distinguish between libraries (normally object code stored in special files) and headers (definitely not object code). Headers declare the facilities provided by a library, but that's not the same as including the library. If `tertiary.c` needs `prototypes.h`, include it. If it also needs `tertiary.h` and that is not included by `prototypes.h`, then include `tertiary.h` before `prototypes.h` in `tertiary.c`. Headers should be both self-contained and idempotent. – Jonathan Leffler Jun 27 '15 at 05:03
  • @DavidC.Rankin: "Must"? No. If I have a source file that calls `printf`, for example, it's perfectly legal for that source file to include `"foo.h"` which in turn includes ``. I agree that *best practice* is to include any needed headers directly; the point is that the compiler will not enforce this practice. – Keith Thompson Jun 27 '15 at 05:16
  • Yes, that is what I was saying. If the header file includes the header needed in the compilation unit your fine. But you can't rely on the fact that some other compilation unit may include it for you. – David C. Rankin Jun 27 '15 at 05:24

1 Answers1

0

That is questions with multiple answers.

In simple case you only include headers if they are used in .c file. Headers are dependencies for compilation so for Make you just put it in the same bucket where .c files using those.

For big projects to speedup compilation you may use what called 'precompiled header'. You must include the same header file into every source file, compiler will only process that header once. That is very useful when headers are complicated (e.g. boost library)

Andrey
  • 56
  • 4