By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,190 Members | 2,204 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,190 IT Pros & Developers. It's quick & easy.

static function declaration in header file

P: n/a
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.

Nov 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a


Ravi wrote:
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.


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

--
Er*********@sun.com

Nov 15 '05 #2

P: n/a
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:
Ravi wrote:
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.


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

--
Er*********@sun.com


Nov 15 '05 #3

P: n/a
On 24 Jun 2005 07:51:43 -0700, "Ravi" <ra**************@gmail.com>
wrote:
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.

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
re************************@att.net
Nov 15 '05 #4

P: n/a
On 2005-06-24 11:53:21 -0400, "Ravi" <ra**************@gmail.com> said:
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?


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
cl*******@gmail.com

Nov 15 '05 #5

P: n/a
Ravi wrote:

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:


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.com, 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
Nov 15 '05 #6

P: n/a
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!
On 2005-06-24 11:53:21 -0400, "Ravi" <ra**************@gmail.com> said:
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?
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


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.
(you will likely get a linker complaining about undefined symbols)


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!

http://alphalink.com.au/~phaywood/
"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 "technically correct" English; but since when was rock & roll "technically correct"?
Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.