Romonov
Romonov

Reputation: 8605

Difference between include in .h file and .c file

What is the difference between doing a #include in a .h file and in a .c file?

For example, I have the file a.h and a.c which have the class A. I use class A in class B (b.h, b.c). What is the difference between doing an include:

#include "a.h"

in b.h vs b.c?

Upvotes: 1

Views: 13406

Answers (6)

Sheldon Pinkman
Sheldon Pinkman

Reputation: 1106

Usually, the class definition is typically in your .h file, and the implementation is in your .c(pp) file.

One advantage of doing #include "a.h" in your b.c file, rather than in your b.h file, is that whenever a.h changes, not only b.c but also any other file that includes b.h will have be recompiled.

Also, you're kind of unnecessary exposing implementation details of B to anyone using b.h. The idea is that b.h should not contain additional info that is of no interest to someone using class B (not because it's secret, but because people don't care and you don't want to bother them with it).

There's no need to force anyone including b.h, to indirectly include a dozen of other .h files as well (because they're #included in b.h), just because you happen to use that stuff in b.c. Avoid clutter.

So if possible, it's best to #include "a.h" in b.c !

But this is not always possible. If you're just using A inside b.c, and there are no references to A in b.h, then it's OK. Also, if b.h only contains pointers to A (i.e. as members, function arguments or return values) but no 'type dependent' references, you could also put just this in b.h:

class A;

And still keep #include "a.h" in your b.c. But if there are more references or dependencies on a.h, that anyone including b.h really couldn't do without, then #include "a.h" should go in b.h

Upvotes: 6

Ed Heal
Ed Heal

Reputation: 59997

You can include .c files - but by convention you do not.

.h files are for declarations - i.e. the .c file is the definition and the this .h file is going to do this. It is like the .h file is the contents of the cook book, and the .c file is the actual recipes.

Upvotes: 0

Thomas Allen
Thomas Allen

Reputation: 393

If you put the include directive in your header file, other files that include that header file will also get the included header.

foo.h:

#include "dependency.h"

bar.h:

#include "foo.h"

In this case, bar.h has both foo.h and dependency.h.

Upvotes: 2

.h files put at top of .c or .h files before compile

but .c files compile separately then link to getter to make executable file

Upvotes: 0

Mankarse
Mankarse

Reputation: 40603

#include "a.h" expands to the contents of a.h.

If #include "a.h" is placed in b.h, then a.h will be copied into b.h during compilation.

If #include "a.h" is placed in b.c, then a.h will be copied into b.c during compilation instead.

Upvotes: 0

Alok Save
Alok Save

Reputation: 206518

There is No difference in including a header file in .h or .c file.
The contents of the included file are just copy pasted in to the file in which you include it.

Upvotes: 2

Related Questions