Can anyone tell me how a static function created in header file can be invoked in other files, though static function which is not created in header file can't be invoked in other files. What exactly happens when we declare static functions in header file vs other cpp/c files.
-
2Since it's in a header, each file that includes that header has its own copy of the function defined in it. – Shawn Feb 03 '23 at 10:24
-
please choose one language. and if possible show some code. Code is always better than describing the code in english – 463035818_is_not_an_ai Feb 03 '23 at 10:27
-
1If you want a function to be called from other files, don't use the `static` specifier. That's like putting a fence in and wondering why your dog can't get to the other side. – Andrew Henle Feb 03 '23 at 10:27
-
1You need to understand that header files are substituted during the preprocessing phase. If a header file defines a static function, and five .c files include that header file, then each of those five .c files gets its own copy, exactly as if it were defined five times, once in each .c file. There's no more to it than that. Of course, normally you wouldn't put a static function in a header file, unless it's being inlined. – Tom Karzes Feb 03 '23 at 10:27
-
@AndrewHenle Except in case of inline functions https://stackoverflow.com/questions/7762731/whats-the-difference-between-static-and-static-inline-function – Lundin Feb 03 '23 at 10:32
-
@Andrew Henle I'm just learning the alternative ways of avoiding error that comes when we don't include pragma once or header guards. i know by making the function in header file as static or inlining it can avoid the ambiguity. But my question is not whether i should use static functions or not. what i'm asking is how it can be invoked in other files. – Indu Feb 03 '23 at 10:36
-
1@Lundin That just creates a dog on each side of the fence. ;-) – Andrew Henle Feb 03 '23 at 10:36
-
2@AndrewHenle Fair enough. And also, the nerdiest-ever reply to "Who let the dogs out?" ought to be "yeah sorry that was me, I forgot to specify internal linkage and to privately encapsulate the dogs." :) – Lundin Feb 03 '23 at 10:38
-
@Indu *what i'm asking is how it can be invoked in other files.* If the function is defined in only one file, making it `static` makes it impossible to be invoked in other files. That's what `static` does - you can't invoke it from other files as it's simply not "visible" ("internal linkage" is the language used in the C standard.) – Andrew Henle Feb 03 '23 at 10:40
-
@Andrew then how it is defining a static function in header file can be invoked in other files – Indu Feb 03 '23 at 10:42
-
@Indu Because that creates a separate function in each file - which is why defining functions in header files is usually a bad idea. – Andrew Henle Feb 03 '23 at 10:45
-
Also... speaking of dogs, I actually recall making an analogy for "global static" as "dog cat". That is: "I wanted a cat so I went to the pet store and bought a dog. However, it doesn't behave like a cat at all. How do I turn my dog into a cat?" And the answer to that is: "if you wanted a cat then why did you buy a dog in the first place". Don't declare something as `static` if you want to access it from multiple files and vice versa - simple as that. – Lundin Feb 03 '23 at 10:50
-
The "file" is a more or less meaningless concept in C and C++. What matters is the "translation unit". (Look it up.) – molbdnilo Feb 03 '23 at 11:48
2 Answers
If you define static function in a header file it will be be visible in any source file which includes this header file. You will have as many copies of the static function as you have source files that include it. It will increase the program size. I would suggest only defining static
inline
functions in the header files.
It is a common misunderstanding in C. Beginner programmers think that the header file is a "library" but in the fact "#include" only textually replaces #include <file.h>
with the context of file.h
creating one source file which is compiled by the compiler.
Example: https://godbolt.org/z/Y5s9fecP6

- 730,956
- 141
- 904
- 1,278

- 60,014
- 4
- 34
- 74
The "file" is a more or less meaningless concept in C and C++ - what matters is the "translation unit".
Somewhat simplified, when you compile one source file, that's one translation unit.
The #include
mechanism is extremely simple - it literally just includes the contents of a file at that point.
And static
makes a name local to the translation unit; it can't be seen anywhere else.
So, say you have a header "H.h":
static something X;
and two source files,
A.cpp:
#include "H.h"
and B.cpp:
#include "H.h"
then after preprocessing, this is what the compiler sees:
A.cpp:
static something X;
and B.cpp:
static something X;
In other words, you have two something
s called "X", completely independent of each other, and neither translation unit can access the other's.
(This is a frequent source of bugs that can be very hard to find, in particular if the intent is to have a globally mutable variable. Don't make things static
in headers.)

- 64,751
- 3
- 43
- 82
-
1*The "file" is a more or less meaningless concept...* Then why did you use files in your examples? ;-) – Andrew Henle Feb 03 '23 at 14:25
-
The concept of files is not meaningless, though the translation unit is indeed very important. – Jonathan Leffler Feb 03 '23 at 14:38
-
I knew the preprocessor replaces the #include"header.h" with its content. Though I was totally confused. But now I'm clear that we are not calling functions from a header ,we're copying the whole content like if said before, so that's the answer to my question. This static function in header file is not being called outside that file however but is copied into each file that includes this headerfile. so defining it as static is not a problem. i was confusing with this two. – Indu Feb 03 '23 at 14:49