473,657 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 31484


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.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
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 "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1821
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
2041
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 "Card.h"
5
562
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 compiler can find the definition of the function prior to encountering the use of the function it will generate the prototype itself. I don't currently use this feature, I explicitly create declarations for all functions in a header file. However, I...
4
13217
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
6103
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
6733
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 more detail: test.h #include <iostream> using namespace std; class test {
11
2915
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 this possible.I thought it will throw "Variable redefinition Error". Giving the source code for reference.Please help me out on this... a.h ----- int g; void fun(void);
7
1952
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 cannot find the implementation I provided, apparently). (the -c option tells the compiler to don't try to link, just compile).
28
8857
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 didn't find any good explanation about it. Could the experts please give me an explanation or point me to some link? Thanks! /Why Tea
0
8833
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8509
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7345
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6174
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2735
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.