'C' help ...
 

Subscribe now and choose from over 30 free gifts worth up to £49 - Plus get £25 to spend in our shop

[Closed] 'C' help #include in .C file

5 Posts
5 Users
0 Reactions
100 Views
 Earl
Posts: 1902
Free Member
Topic starter
 

What is the difference in putting the #include in the .c file instead of the .h file? I've pretty much only seen itin the .h file but ecently come across a file whith the #include in the .c file with teh comment /*private include */.

e.g function2.c
#include<functon1.h>

Answers please

Thanks


 
Posted : 29/01/2012 8:38 pm
Posts: 19
Free Member
 

If the include is in a header (.h) file, then it will be included (or at least opened) by every file that includes the initial header file.

Including it in the c file means that it is only included within that file.

It's good practice to be explicit with your includes, and restrict includes within header files to ones that are absolutely necessary (and then explicitly guard them). Compile times on large projects can be be greatly reduced by careful .include management.


 
Posted : 29/01/2012 9:08 pm
Posts: 0
Free Member
 

If you put the function1.h in the the function 2.h then all the functions and variables in function1.h will be visible to anything that has
#include "function2.h"

If you put it in function2.c, it won't be.

No idea what counts a good practice, but i'd only put in function2.h include files that were directly needed by the declarations in function2.h, anything else I'd put in function2.c, as it always seems unwise to expose anything other than you explictly need to.


 
Posted : 29/01/2012 9:09 pm
Posts: 4954
Free Member
 

As I understand it is just the same, the file extension has little real meaning to the preprocessor, .h or .c.

This might help you.

http://stackoverflow.com/questions/232693/including-one-c-source-file-in-another


 
Posted : 29/01/2012 9:10 pm
Posts: 0
Free Member
 

I always used to #include the header files required by the code, some were library files other were my own. Some headers will contain #includes as they're dependant upon declarations contained within and the burden to include shouldn't be placed on the user of the library/function etc.


 
Posted : 29/01/2012 9:11 pm
 Earl
Posts: 1902
Free Member
Topic starter
 

Thanks very much everyone. Makes lots of sense but will follow that link to have more of a read.


 
Posted : 29/01/2012 10:18 pm