For me it's a rule to define and declare static functions inside source files, I mean .c files.
However in very rare situations I saw people declaring it in the header file. Since static functions have internal linkage we need to define it in every file we include the header file where the function is declared. This looks pretty odd and far from what we usually want when declaring something as static.
On the other hand if someone naive tries to use that function without defining it the compiler will complaint. So in some sense is not really unsafe to do this even sounding strange.
My questions are:
- What is the problem of declaring static functions in header files?
- What are the risks?
- What the impact in compilation time?
- Is there any risk in runtime?
This is not an answer to the stated questions, but hopefully shows why one might implement a
static
(orstatic inline
) function in a header file.I can personally only think of two good reasons to declare some functions
static
in a header file:If the header file completely implements an interface that should only be visible in the current compilation unit
This is extremely rare, but might be useful in e.g. an educational context, at some point during the development of some example library; or perhaps when interfacing to another programming language with minimal code.
A developer might choose to do so if the library or interaface implementation is trivial and nearly so, and ease of use (to the developer using the header file) is more important than code size. In these cases, the declarations in the header file often use preprocessor macros, allowing the same header file to be included more than once, providing some sort of crude polymorphism in C.
Here is a practical example: Shoot-yourself-in-the-foot playground for linear congruential pseudorandom number generators. Because the implementation is local to the compilation unit, each compilation unit will get their own copies of the PRNG. This example also shows how crude polymorphism can be implemented in C.
prng32.h:
An example using the above, example-prng32.h:
The reason for marking both the
_state
variable and the_next()
functionstatic
is that this way each compilation unit that includes the header file has their own copy of the variables and the functions -- here, their own copy of the PRNG. Each must be separately seeded, of course; and if seeded to the same value, will yield the same sequence.One should generally shy away from such polymorphism attempts in C, because it leads to complicated preprocessor macro shenanigans, making the implementation much harder to understand, maintain, and modify than necessary.
However, when exploring the parameter space of some algorithm -- like here, the types of 32-bit linear congruential generators, this lets us use a single implementation for each of the generators we examine, ensuring there are no implementation differences between them. Note that even this case is more like a development tool, and not something you ought to see in a implementation provided for others to use.
If the header implements simple
static inline
accessor functionsPreprocessor macros are commonly used to simplify code accessing complicated structure types.
static inline
functions are similar, except that they also provide type checking at compile time, and can refer to their parameters several times (with macros, that is problematic).One practical use case is a simple interface for reading files using low-level POSIX.1 I/O (using
<unistd.h>
and<fcntl.h>
instead of<stdio.h>
). I've done this myself when reading very large (dozens of megabytes to gigabytes range) text files containing real numbers (with a custom float/double parser), as the GNU C standard I/O is not particularly fast.For example, inbuffer.h:
Note that the above
inbuffer_skip()
andinbuffer_getc()
do not check ifib
is non-NULL; this is typical for such functions. These accessor functions are assumed to be "in the fast path", i.e. called very often. In such cases, even the function call overhead matters (and is avoided withstatic inline
functions, since they are duplicated in the code at the call site).Trivial accessor functions, like the above
inbuffer_skip()
andinbuffer_getc()
, may also let the compiler avoid the register moves involved in function calls, because functions expect their parameters to be located in specific registers or on the stack, whereas inlined functions can be adapted (wrt. register use) to the code surrounding the inlined function.Personally, I do recommend writing a couple of test programs using the non-inlined functions first, and compare the performance and results to the inlined versions. Comparing the results ensure the inlined versions do not have bugs (off by one type is common here!), and comparing the performance and generated binaries (size, at least) tells you whether inlining is worth it in general.
Why would you want a both global and static function? In c, functions are global by default. You only use static functions if you want to limit the access to a function to the file they are declared. So you actively restrict access by declaring it static...
The only requirement for implementations in the header file, is for c++ template functions and template class member functions.
First I'd like to clarify my understanding of the situation you describe: The header contains (only) a static function declaration while the C file contains the definition, i.e. the function's source code. For example
some.h:
some.c:
If this is the situation you describe, I take issue with your remark
If you declare the function but do not use it in a given translation unit, I don't think you have to define it. gcc accepts that with a warning; the standard does not seem to forbid it, unless I missed something. This may be important in your scenario because translation units which do not use the function but include the header with its declaration don't have to provide an unused definition.
Now let's examine the questions:
What is the problem of declaring static functions in header files?
It is somewhat unusual. It would make sense only if most translation units which include the header with a given function declaration indeed use that function, because the main rationale and benefit of static functions is their limited visibility. They don't pollute the global namespace (the only one C has) and can be used as a poor man's "private" methods which are not meant to be used by the general public and hence a declared such that they are accessible only where they are needed.
On the other hand it may actually be beneficial to have a declaration in a header because it ensures that all local definitions agree at least in the function signature. (Two functions with the same name but different return types would cause a compile time error in C (and C++); different parameter types would cause a compile time error only in C because it does not have function overloading.) From this uniformity perspective it may be adequate to provide the function definition proper in the header file right away, if the function is meant to be identical in each translation unit. The overhead of this approach depends on whether all translation units which include the header also actually use the function.
What are the risks?
I do not see risks in your scenario. (As opposed to also including the function definition in a header which may violate the encapsulation principle.)
What the impact in compilation time?
A function declaration is small and its complexity is low, so the overhead of having additional function declarations in a header is likely negligible. But if you create and include an additional header for the declaration in many translation units the file handling overhead can be significant (i.e. the compiler idles a lot while it waits for the header I/O).
Is there any risk in runtime?
I cannot see any.