static function declaration in header file

R

Ravi

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

Eric Sosman

Ravi said:
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.
 
R

Ravi

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

Alan Balmer

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

Clark S. Cox III

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)
 
C

CBFalconer

Ravi said:
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.
 
P

Peter Shaggy Haywood

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!
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"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top