headers and linking confusion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • przemek
    New Member
    • Oct 2007
    • 6

    headers and linking confusion

    I used to think that including header files gives me some extra function to use.
    Like putting <stdlib.h> allows use of system("PAUSE") without doing anything special.

    I disovered lately that it doesn't work that way with my own header files.
    With main function in file A, extra functions in file B (both including header file C with declarations of functions in B) to use these functions in A I still need to link A and B - what raises the question: WHAT'S THE USE OF MY HEADER?
    Help, please!
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by przemek
    I used to think that including header files gives me some extra function to use.
    Like putting <stdlib.h> allows use of system("PAUSE") without doing anything special.

    I disovered lately that it doesn't work that way with my own header files.
    With main function in file A, extra functions in file B (both including header file C with declarations of functions in B) to use these functions in A I still need to link A and B - what raises the question: WHAT'S THE USE OF MY HEADER?
    Help, please!
    For compilation the compiler needs to know the type of functions while it is compiling the code.
    While linking it needs the original function for which you needs the body of the function.
    Try to search in the net for difference between function definition and function declaration to get more info about this

    Raghuram

    Comment

    • przemek
      New Member
      • Oct 2007
      • 6

      #3
      Thank You,
      to be more specific:

      Using gcc -c source.c :
      I can compile any code with undeclared functions and no header leading to declaration at all. I can link that output later to binaries with all needed definitions but again without any headers. So what is the point of writing header files (besides kind of documentation) ?

      Using just gcc source.c :
      I cannot compile code even with headers leading to declarations of functions which definitions are alredy compiled. I need to do all the linking I can't see any use for headers again.

      My problem is not using but seeing the point of using separate .h declaration file?

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Originally posted by przemek
        Thank You,
        to be more specific:

        Using gcc -c source.c :
        I can compile any code with undeclared functions and no header leading to declaration at all. I can link that output later to binaries with all needed definitions but again without any headers. So what is the point of writing header files (besides kind of documentation) ?

        Using just gcc source.c :
        I cannot compile code even with headers leading to declarations of functions which definitions are alredy compiled. I need to do all the linking I can't see any use for headers again.

        My problem is not using but seeing the point of using separate .h declaration file?
        using gcc source.c means compiling and linking so you need to add the other .c file which has the declaration.
        So the header file you have added will be used only during compilation and .c file is required during linking

        Raghuram

        Comment

        • przemek
          New Member
          • Oct 2007
          • 6

          #5
          Originally posted by gpraghuram
          using gcc source.c means compiling and linking so you need to add the other .c file which has the declaration.
          If you mean -- using gcc source.c means compiling and linking so you need to add the other .c file which has the definitions (of everything declared in .h)
          -- then I'm ok.
          Originally posted by gpraghuram
          So the header file you have added will be used only during compilation and .c file is required during linking.

          Raghuram.
          Yes, and HOW is this header file used during compilation if it goes the same with it or witout it?
          Please go a little deeper this path as I need undestanding more than confirmation of what I see.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            The importance of the header file is adding robustness to your code and having the compiler pick up more errors for you.

            By declaring the function in the header and then including this header in the c file in which the function is defined and the c files in which the function is called you cause the compiler to check that the function is being defined as per its declaration and being called as per the declaration. If it isn't in either case then compiler warnings (if not errors) will be produced.

            By using a header to declare the API of you function(s) you ensure that the function is used in the same way where ever it is used and you use the compiler to check this for you making your programs more robust and easier to maintain.

            Comment

            • przemek
              New Member
              • Oct 2007
              • 6

              #7
              It's all clear now, thank you!
              Przemek

              Comment

              Working...