Question about #ifndef fileName_h???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • plowgrammer2010
    New Member
    • Jun 2009
    • 12

    Question about #ifndef fileName_h???

    Hi, All
    this is written in most of header files but i dont know its mean.The statements of file prototyp.h are
    Code:
    [B]#ifndef PROTOTYP_H
    #define PROTOTYP_H[/B]
    ArrayAdd(LPARRAY,LPARRAY);
    ArraySubtract(LPARRAY,LPARRAY);
    #endif
    the upper bold words are. what is its purpose and where it is used
    i have used #ifndef but in simple to check if any thing is not defined then run following code, but here with file name is new to me.
    Thanks.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Start here by reading about the C/C++ preprocessor directives.

    In an include file it is common to use the preprocessor statements you have highlighted to prevent double inclusion of the file. The first time the file is included PROTOTYP_H is not defined and the code between the #ifndef and #endif is compiled, this includes defining PROTOTYP_H. That means that the second time the file is included PROTOTYP_H is defined so the code between the #ifndef and #endif is left out.

    Preventing double inclusion is useful because declaring data structures (classes structures unions etc.) more than once produces a compiler diagnostic (an error message).

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by plowgrammer2010
      Hi, All
      this is written in most of header files but i dont know its mean.The statements of file prototyp.h are
      Code:
      [B]#ifndef PROTOTYP_H
      #define PROTOTYP_H[/B]
      ArrayAdd(LPARRAY,LPARRAY);
      ArraySubtract(LPARRAY,LPARRAY);
      #endif
      the upper bold words are. what is its purpose and where it is used
      i have used #ifndef but in simple to check if any thing is not defined then run following code, but here with file name is new to me.
      Thanks.
      It's not a file name, it's a 'pp-token' which stands for 'pre-processor token'. If it isn't defined it is immediately defined at the next line and the entire content of the file is pre-processed; if it was defined before nothing is pre-processed.

      It's a 'guard' mechanism that protects a file from multiple inclusion, i.e. if it was read before (the pp-token is defined) then the entire content of the file is skipped.

      kind regards,

      Jos

      edit: too slow again, Banfa beat me again ;-)

      Comment

      • plowgrammer2010
        New Member
        • Jun 2009
        • 12

        #4
        Thanks, and another question...

        Thanks for your kind replies, it helped me a lot. i happy to join this forum. i will also use pp token for my next programming. here is another question.
        I am micro-controller programmer for atmel 8051 in keil(ide and compiler).
        My question is related to the topic.
        When we include math.h in our standard c language or keil(microcontr oller's c), when we call any function like abs(12-50);
        Only called function is included in our object or hex file. the object or hex file size is not so, big means there are so, many other functions in math.h they are not compiled and added in our hex, or object file.
        But when i make my own header file put there 10 functions , and try to use one of them all are compiled and added in hex(i know because of size is large in compare without other 9 functions).
        what is the method to adopt that technique
        sorry for my English
        Regards,
        Jon.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          I assume your implementation of the standard library is included as a lib. Typically the smallest unit a linker can add to a program is 1 object file.

          A lib is normally a collection of object files but a well written standard library will have each function in a separate object file. It will do this by putting the source code for each function in the library in a separate source (c) file.

          Probably what you have done is put all you functions into 1 c file producing 1 object file so that even if you only use 1 function the linker has to include the whole object and thus the code for all 10 functions making the program larger than needed.

          Advanced linkers can remove unused functions from the final image, however most of these micro-processor compilers and linkers are not particularly advanced and so probably would not do this.

          Try spliting your functions out into separate c files.

          Comment

          • plowgrammer2010
            New Member
            • Jun 2009
            • 12

            #6
            re.

            There are for example 10 math functions, 20 string functions, 10 display functions, and so, on...
            means i have to make 40 files, its ok for permenant library i will do. is there any method in our hands besides linker so, that include 1 file call function and related object file is included by the linker.

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              The compiler program translates a source file (.c) into an object file (.o). On unix the compiler program is typically called "cc".

              The librarian program collects multiple object files (.o) and library files (.a) into a single library file (.a). On unix the librarian program is typically called "ar", for 'archiver'.

              The linker program translates multiple object files (.o) and library files (.a) into a single executable file. On unix the linker program is typically also invoked by the "cc" command. Typically, the executable file produced by the linker includes all input object files in their entirety, but only the minimum number of object files from within the input library files needed to resolve all references.

              If you know you want a set of object files to be present in your executable then you can just ask the linker to include those files -- no need to use the librarian. The purpose of the librarian is to provide the opportunity to minimize the size of the executable.

              Comment

              • solita
                New Member
                • Jun 2009
                • 17

                #8
                it is to ensure that header file is included only once

                Comment

                Working...