Header file help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iovecpsc
    New Member
    • Oct 2007
    • 2

    Header file help

    Say I wanted to create a header file for my program. Would the variables in the header be defined globally for the file that it is attached to.
    Code:
     
    //header.h
    
    //#ifndef HEADER_EXAMPLE 
    //#define HEADER_EXAMPLE
    char array[10] ={"hello world"};
    
    //prog.c
    //#endif
    
    #include <stdio.h>
    #include "header.c" //#include <header.c>
    {
    printf("%s", &array);
     return 0;
    }
    don't really understand the parts that I commented out. What does the header_example mean. Is it supposed to be a specific word of anything or its just random words?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Originally posted by iovecpsc
    Say I wanted to create a header file for my program. Would the variables in the header be defined globally for the file that it is attached to.

    Code: ( text )
    //header.h

    //#ifndef HEADER_EXAMPLE
    //#define HEADER_EXAMPLE
    char array[10] ={"hello world"};

    //prog.c
    //#endif

    #include <stdio.h>
    #include "header.c" //#include <header.c>
    {
    printf("%s", &array);
    return 0;
    }
    Let's takes this in pieces:

    1) This header can be included in many source files. Each time it is included, its contents become part of the object file for that source file. When the linker combines the object files to create your executable, and variables created in the header file will be duplicated in each object file. Your build dies here with redefinition errors.

    RULE 1: Header files contain declarations and not definitions.

    A declaration says a thing exists. A definition is the code that creates it.

    [code=c]
    extern int data; //declaration. data is not in this file

    int data; //definition. data is created right here

    void MyFunction(int) ; //declaration. The function is not in this file

    void MyFunction(int arg) //definition. This is the actual function.
    {
    cout << arg << endl;
    }
    [/code]

    The business with the #ifndef has to do with protecting against complications resulting from including the same header more than once in the same source file.

    You get into this pickle when you include HeaderA and HeaderB and each of these includes HeaderC. When Header C is included the second time you die with redefinition errors again.

    So, you need to be sure HeaderC is include only once. The common practice is to use an inclusion guard. This is a series of preprocessor directives.

    [code=c]
    #ifndef XXX
    #define XXX

    // header file contents here
    #endif
    [/code]

    The first time the header is included XXX is not define. Hence the "if-not-defined" preprocessor directive evaluates to true and you define XXX and then include the header file contents. The #endif terminates the area affected by the #ifndef.

    The second time the header is included (in the same source file, remember) the #ifndef evaluates to false because XXX was defined during the first inclusion. Hence, the preprocessor skips to the #endif neatly bypassing the header file contents.

    RULE 2: All header files need inclusion guards.

    RULE 3: The symbol defined (XXX) needs to be unique among all header files ever written or this scheme doesn't work. Making XXX a unique thing is left to your imagination.

    RULE 4: Because of RULE 1, you never include source files.
    [code=c]
    #include <MyStuff.c> //Big no-no.
    #include <MyStuff.cpp>
    [/code]

    Comment

    • GGnOrE
      New Member
      • Jun 2007
      • 8

      #3
      1.)Thanks for the advice weaknessforcate s

      2.)
      Okay,
      When should I use multiple header files. It doesn't seem necessary if that one header file may be include at the top of every file. Also, functions can't access the declarations made by the header, so I pretty much have to make local variables for those.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by GGnOrE
        When should I use multiple header files. It doesn't seem necessary if that one header file may be include at the top of every file. Also, functions can't access the declarations made by the header, so I pretty much have to make local variables for those.
        A header file should contain related declarations. Unrelated declarations shoul bd in separate header files. For example, you have Date and a Person. It would make little sense to carry Person declarations into an application that only needs a Date. Here you should have separate header files.

        Nothing prevents the Person header file from including the Date header file so that the Person user only has to include the Person header.

        Header file management is a learned skill.

        Functions can access declarations in the header. I don't see the connection between that and having to create local variables.

        Do you have a example of what you are talking about?

        Comment

        Working...