Am 29.08.2012 10:01, schrieb David Brown:
Hm, I use inline functions a lot and my mileage seems to be completely
different from yours.
I have small to medium sized functions in headers as inline, and I
don't want them to be repeated in every compilation unit, so I don't
have them static.
I think you (or perhaps your compiler) have this backwards.
"extern inline" (or just "inline", since "extern" is default for
functions) will create a function body with external linkage _and_ tell
the compiler to inline calls. So, if you use this in a header file that
is included in ten different source files, you will end up with TEN
function bodies with external linkage.
"static inline" will NOT create a function body with external linkage,
but it still tells the compiler to inline calls. So, if you use this in
a header file that is included in ten different source files, you will
end up with ZERO function bodies with external linkage.
I use the "instantiation" syntax of C99 to force the generation of the
symbol in exactly one .c file. So I am always sure that everything
will link well together, e.g when I compile without optimization for
debugging (and the compiler doesn't inline) or when I occasionally
take a pointer to the function.
AIUI, explicit inline directives are not an "optimization"; if your
compiler ignores them, it is not conforming.
It is only an "optimization" for the compiler to inline calls (and
possibly remove function bodies) when there are NOT any "inline" directives.
AFAKS this model is the reason that the "inline" feature was included
in C. For everything else we could have stayed with "static". To me,
"static inline" makes not much sense since it doesn't bring in
anything new.
"static" suppresses the generation of (potentially redundant) function
bodies that will never be called.
If your compiler ignores valid "inline" directives, then it becomes
necessary for ONE of the function definitions to be "extern", so that a
function body is generated, but the rest should be "static".
S