I have a number of small functions which are defined in a .h
file. It is a small project (now) and I want to avoid the pain of having declarations and definitions separate, because they change all the time. To avoid multiply-defined symbols, I can either have them static
or inline
. What should be preferred and why?
I know it is in general bad practice to define functions in headers. You don't have to mention that in answers, this question is meant technically.
Since the question is about C (not C++),
inline
means thatextern
(ISO9899-1999, 6.7.4(6), for example aninline
funciton with external linkage must be defined in the same compilation unit, and an inline definition allows anextern
definition elsewhere without an error (which is not necessarily a good thing, because the two functions need not be functionally equivalent, and it is unspecified which one the compiler uses at any time!).The linker implications given by Heptic are required for C++, but not required by C (as far as I can tell). They are necessarily required by the "shall have the same address in all translation units" clause in ISO14882, 7.1.2(4). I am not aware of any similar clause in C99.
However, since the entirely different languages C and C++ usually go through the same C/C++ compiler and linker, it likely works identically for C, anyway.
So... how to answer your question? Use
inline
when you feel it's adequate. Be aware of the possible pitfalls ofextern
. Otherwise, leave it away and trust the compiler to do it right.I think static inline is the way to go for functions you want to inline, and only static for those you don't want.
static refers to visibility, but inline is ambiguous about visibility in the standard (C99). Anyway, it's not its purpose: inline is for inlining functions, thus it has a side-effect from a visibility point of view you might not want.
I'd use
static inline
, butstatic
would work just as well.extern
andextern inline
are out because you'd get multiple external definitions if the header is included in more than one translation unit, so you need to considerstatic
,static inline
andinline
specification.Heptic correctly states in his answer that most compilers consider functions for inlining regardless of whether
inline
is specified or not, ie the main impact ofinline
is its effect on linkage.However,
static
definitions have internal linkage, so there's not much difference betweenstatic
andstatic inline
; I preferstatic inline
for function definitions in header files for purely stylistic reasons (rule of thumb: header files should only containextern
declarations,static const
variable definitions andstatic inline
function definitions).inline
withoutstatic
orextern
results in an inline definition, which the standard states (C99 6.7.4, §6)ie inline definitions should always be accompanied by external definitions, which is not what you're looking for.
Some more information about the subtleties of C99 inline semantics can be found in this answer, on the Clang homepage and the C99 Rationale (PDF).
Keep in mind that GCC will only use C99 semantics if
-std=c99
or-std=gnu99
is present...