static function declaration in header file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ravi

    static function declaration in header file

    Hi All:
    Is there any reason for declaring functions as static in a header file
    if that header file is going to be included in several other files? The
    compiler throws a warning for every such function declared but not
    called in the source file. Here is what I heard someone mention:

    The functions are declared static as an optimization. Making static
    "hidden-from-the-user" function to extern to appease -Wall compile
    argument is not a good solution since extern functions have additional
    overhead at compile/run time.

    Is this true? What overhead is saved by declaring the functions static?
    Are there any rules of thumb for when to declare functions static as
    opposed to extern in header files? Any pointers in this regard are
    appreciated.

    thanks,
    -Ravi.

  • Eric Sosman

    #2
    Re: static function declaration in header file



    Ravi wrote:[color=blue]
    > Hi All:
    > Is there any reason for declaring functions as static in a header file
    > if that header file is going to be included in several other files? The
    > compiler throws a warning for every such function declared but not
    > called in the source file. Here is what I heard someone mention:
    >
    > The functions are declared static as an optimization. Making static
    > "hidden-from-the-user" function to extern to appease -Wall compile
    > argument is not a good solution since extern functions have additional
    > overhead at compile/run time.
    >
    > Is this true? What overhead is saved by declaring the functions static?
    > Are there any rules of thumb for when to declare functions static as
    > opposed to extern in header files? Any pointers in this regard are
    > appreciated.[/color]

    (I'm assuming that the function is not only declared but
    actually defined in the headers -- if that's not the case,
    what follows is probably wrong.)

    The idea is probably that a sufficiently simple static
    function might be expanded in-line instead of incurring the
    overhead of an actual call-and-return linkage. If the function
    has external linkage (or if it is static but a pointer to it
    is "exported") the compiler must actually generate full-blown
    function code that some other module could call.

    I've seen the technique used in a vendor's <ctype.h> --
    that particular compiler didn't complain about unused static
    functions, but just discarded them silently. The C99 Standard
    adds the `inline' keyword to control this sort of optimization
    a little more cleanly.

    --
    Eric.Sosman@sun .com

    Comment

    • Ravi

      #3
      Re: static function declaration in header file

      Eric,
      The functions declared static in the header file are defined in a
      separate source file. In such a case, does the optimization apply or
      even make sense?
      thanks,
      -Ravi.

      Eric Sosman wrote:[color=blue]
      > Ravi wrote:[color=green]
      > > Hi All:
      > > Is there any reason for declaring functions as static in a header file
      > > if that header file is going to be included in several other files? The
      > > compiler throws a warning for every such function declared but not
      > > called in the source file. Here is what I heard someone mention:
      > >
      > > The functions are declared static as an optimization. Making static
      > > "hidden-from-the-user" function to extern to appease -Wall compile
      > > argument is not a good solution since extern functions have additional
      > > overhead at compile/run time.
      > >
      > > Is this true? What overhead is saved by declaring the functions static?
      > > Are there any rules of thumb for when to declare functions static as
      > > opposed to extern in header files? Any pointers in this regard are
      > > appreciated.[/color]
      >
      > (I'm assuming that the function is not only declared but
      > actually defined in the headers -- if that's not the case,
      > what follows is probably wrong.)
      >
      > The idea is probably that a sufficiently simple static
      > function might be expanded in-line instead of incurring the
      > overhead of an actual call-and-return linkage. If the function
      > has external linkage (or if it is static but a pointer to it
      > is "exported") the compiler must actually generate full-blown
      > function code that some other module could call.
      >
      > I've seen the technique used in a vendor's <ctype.h> --
      > that particular compiler didn't complain about unused static
      > functions, but just discarded them silently. The C99 Standard
      > adds the `inline' keyword to control this sort of optimization
      > a little more cleanly.
      >
      > --
      > Eric.Sosman@sun .com[/color]

      Comment

      • Alan Balmer

        #4
        Re: static function declaration in header file

        On 24 Jun 2005 07:51:43 -0700, "Ravi" <ravi.gummadida la@gmail.com>
        wrote:
        [color=blue]
        >Hi All:
        >Is there any reason for declaring functions as static in a header file
        >if that header file is going to be included in several other files? The
        >compiler throws a warning for every such function declared but not
        >called in the source file. Here is what I heard someone mention:
        >
        >The functions are declared static as an optimization. Making static
        >"hidden-from-the-user" function to extern to appease -Wall compile
        >argument is not a good solution since extern functions have additional
        >overhead at compile/run time.
        >
        >Is this true? What overhead is saved by declaring the functions static?
        >Are there any rules of thumb for when to declare functions static as
        >opposed to extern in header files? Any pointers in this regard are
        >appreciated.
        >[/color]
        I may still be sleepy this morning, but I can't think of any reason to
        have static function declarations in a header file included by
        programs that do not define those functions.

        Move the declarations to the top of the file where the functions are
        defined. That's the only file that can use them.

        --
        Al Balmer
        Balmer Consulting
        removebalmercon sultingthis@att .net

        Comment

        • Clark S. Cox III

          #5
          Re: static function declaration in header file

          On 2005-06-24 11:53:21 -0400, "Ravi" <ravi.gummadida la@gmail.com> said:
          [color=blue]
          > Eric,
          > The functions declared static in the header file are defined in a
          > separate source file. In such a case, does the optimization apply or
          > even make sense?[/color]

          If that's the case, I can't even imagine how that code compiles and
          links. Attempting to call that function from any translation unit other
          than the one in which it is defined will result in undefined behavior
          (you will likely get a linker complaining about undefined symbols)

          --
          Clark S. Cox, III
          clarkcox3@gmail .com

          Comment

          • CBFalconer

            #6
            Re: static function declaration in header file

            Ravi wrote:[color=blue]
            >
            > Is there any reason for declaring functions as static in a header file
            > if that header file is going to be included in several other files? The
            > compiler throws a warning for every such function declared but not
            > called in the source file. Here is what I heard someone mention:[/color]

            Why would you even mention them in the header file? The purpose of
            that .h file is to expose portions of the .c file to other
            modules. static functions are intrinsically private to the .c file
            in which they were declared.

            --
            "If you want to post a followup via groups.google.c om, don't use
            the broken "Reply" link at the bottom of the article. Click on
            "show options" at the top of the article, then click on the
            "Reply" at the bottom of the article headers." - Keith Thompson


            Comment

            • Peter Shaggy Haywood

              #7
              Re: static function declaration in header file

              Groovy hepcat Clark S. Cox III was jivin' on Fri, 24 Jun 2005 12:59:32
              -0400 in comp.lang.c.
              Re: static function declaration in header file's a cool scene! Dig it!
              [color=blue]
              >On 2005-06-24 11:53:21 -0400, "Ravi" <ravi.gummadida la@gmail.com> said:
              >[color=green]
              >> Eric,
              >> The functions declared static in the header file are defined in a
              >> separate source file. In such a case, does the optimization apply or
              >> even make sense?[/color]
              >
              >If that's the case, I can't even imagine how that code compiles and
              >links. Attempting to call that function from any translation unit other
              >than the one in which it is defined will result in undefined behavior[/color]

              In fact, he should get a diagnostic (which he is, of course) if the
              function has indeed been called from other translation units. But he
              didn't say it has, just that it is declared in a header.
              [color=blue]
              >(you will likely get a linker complaining about undefined symbols)[/color]

              Static functions may not be seen by the linker. In that case he
              won't get any indication of anything wrong from the linker. (The
              compiler, on the other hand...)

              --

              Dig the even newer still, yet more improved, sig!


              "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
              I know it's not "technicall y correct" English; but since when was rock & roll "technicall y correct"?

              Comment

              Working...