Reputation: 1383
Iam programing in C. I want some description about static and inline functions. I know that if we make a function static then it is an indication to compiler that it is under internal linkage for only one translation unit. I have following doubts regarding static and inline :
Please shed some light on the above things inline. Platform is Linux, gcc compiler , C language.
Upvotes: 1
Views: 700
Reputation: 78903
static
and inline
serve two very different purposes.
As you say correctly static
means that the symbol of the function is not exported from the compilation unit where it is defined. Therefore different compilation units can have such symbols with the same name without conflict. Whether or not this corresponds to the same function declaration and definition is up to you. But such functions as all statically allocated objects can well be used in a different compilation unit by statically or dynamically exporting a pointer to it.
inline
is different. Its intent is to make it possible for the compiler to inline your function, thus the name, but its major direct effect is that the function symbol is usually not emitted at all. This is designed for the purpose that you may put the definition of the function in a header file and include that file in several compilation units without creating multiple symbols in each of them. For the defined function it also has the effect that you are not allowed to declare static
variables inside an inline
function, since it would not be clear at all in which compilation unit that object would have to be realized.
So to summarize, static
generates plenty of copies of your function, inline
generates none; static
has its major use in ".c"
files and inline
in ".h"
The first has the effect that you may at certain places not detect that two function pointers point to the "same" function, the second may have the effect that if you need a function pointer to the function there is no function object to which it would refer. Such a function can be forced to be emitted (in just one compilation unit!) by placing a sort of "instantiation" in the .c file:
// .h definition
inline void toto(void) { }
// .c instantiation
void toto(void);
Upvotes: 3
Reputation: 11162
A static
function can't be used in other translation units. That's their raison d'être.
inline
hints to the compiler that the function should be inlined instead of called.
Making a function static is different than using a macro. A macro essentially overrules the compiler. Whether it thinks it wise to inline or not, a macro will be inlined; macros are textual substitution. You can also pass a static
function to something requiring a function pointer. Can't do that with a macro.
Macros will forcefully inline anything. Even specifying inline
can be overruled.
Make functions you don't want to export static
. If a function is really small, and you really think it should be inlined, you can tell the compiler that with inline
. Macros are really only for metaprogramming. The compiler knows better than you.
Upvotes: 4
Reputation: 6822
These days compilers will determine whether an inline function will actually be inlined or not, as not all functions are good candidates for it. If so, then function body is simply injected/inlined where the respective function is referenced.
Such functions should be reserved for frequent function calls where the function body is usually quite short, though i suppose this doesnt have to be the case.
Upvotes: 1