473,385 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.

Nov 15 '05 #1
6 31449


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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Leroy van Engelen | last post by:
Hi group, Say, I wanted to create a class like the following: template < typename T > struct Foo { static T *bar; }; template < typename T > T *Foo< T >::bar = 0;
5
by: A_StClaire_ | last post by:
annoying one. can anyone spot the issue? it appears communityCards can't be accessed via my static function but I don't know why... Game.h: #pragma once #include <vector> #include...
5
by: Daniel Nichols | last post by:
I've noticed that in a C module (.c, .h file combination) that if you create a function's definition before it is used in other functions than a declaration is not necessary. I believe if the...
4
by: thinktwice | last post by:
i have just made a test project :(win32 console) //file : func.h #ifndef _FUNC_H_ #define _FUNC_H_ void func1() { return; };
4
by: nospam_timur | last post by:
Let's say I have two files, myfile.h and myfile.c: myfile.h: int myfunction(int x); myfile.c: #include "myfile.h"
2
by: mt7 | last post by:
Hi, I am trying to declare static member function where I declare my class in test.h file (see below), and define it in test.cc file. But it seems I cannot do that :( ... Here is the program in...
11
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is...
7
by: Giovanni Gherdovich | last post by:
Hello, As you will see from the following code snippets, I'm trying to declare a function object in an header file and implement it in a (different) source file, but my compiler complains (he...
28
by: Why Tea | last post by:
I seem to remember that in ANSI C, all static functions should have their function prototypes listed at the beginning of the file for better consistency (or something like that). I googled and...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.