Next: , Previous: , Up: Particular Modules   [Contents][Index]


17.9 Extern inline functions

The extern-inline module supports the use of C99-style extern inline functions so that the code still runs on compilers that do not support this feature correctly.

C code ordinarily should not use inline. Typically it is better to let the compiler figure out whether to inline, as compilers are pretty good about optimization nowadays. In this sense, inline is like register, another keyword that is typically no longer needed.

Functions defined (not merely declared) in headers are an exception, as avoiding inline would commonly cause problems for these functions. Suppose aaa.h defines the function aaa_fun, and aaa.c, bbb.c and ccc.c all include aaa.h. If code is intended to portable to non-C99 compilers, aaa_fun cannot be declared with the C99 inline keyword. This problem cannot be worked around by making aaa_fun an ordinary function, as it would be defined three times with external linkage and the definitions would clash. Although aaa_fun could be a static function, with separate compilation if aaa_fun is not inlined its code will appear in the executable three times.

To avoid this code bloat, aaa.h can do this:

/* aaa.h */
/* #include any other headers here */
#ifndef _GL_INLINE_HEADER_BEGIN
 #error "Please include config.h first."
#endif
_GL_INLINE_HEADER_BEGIN
#ifndef AAA_INLINE
# define AAA_INLINE _GL_INLINE
#endif
...
AAA_INLINE int
aaa_fun (int i)
{
  return i + 1;
}
...
_GL_INLINE_HEADER_END

and aaa.c can do this:

/* aaa.c */
#include <config.h>
#define AAA_INLINE _GL_EXTERN_INLINE
#include <aaa.h>

whereas bbb.c and ccc.c can include aaa.h in the usual way. C99 compilers expand AAA_INLINE to C99-style inline usage, where aaa_fun is declared extern inline in aaa.c and plain inline in other modules. Non-C99 compilers that are compatible with GCC use GCC-specific syntax to accomplish the same ends. Other non-C99 compilers use static inline so they suffer from code bloat, but they are not mainline platforms and will die out eventually.

_GL_INLINE is a portable alternative to C99 plain inline.

_GL_EXTERN_INLINE is a portable alternative to C99 extern inline.

Invoke _GL_INLINE_HEADER_BEGIN before all uses of _GL_INLINE in an include file. This suppresses some bogus warnings in GCC versions before 5.1. If an include file includes other files, it is better to invoke this macro after including the other files.

Invoke _GL_INLINE_HEADER_END after all uses of _GL_INLINE in an include file.


Next: Handling closed standard file descriptors, Previous: Static inline functions, Up: Particular Modules   [Contents][Index]