Viewing 6 posts - 1 through 6 (of 6 total)
  • 'C' help #include in .C file
  • Earl
    Free Member

    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

    al2000
    Full 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.

    IanMunro
    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.

    TheBrick
    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

    allthepies
    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.

    Earl
    Free Member

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

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘'C' help #include in .C file’ is closed to new replies.