In message <11*********************@l41g2000cwc.googlegroups. com>
"evan" <ev******@gmail.com> wrote:
Hi, I've got an easy one...
I need to inline a few functions from one module to another. By looking
at the compiled code I can see that the function is inlined if it is
called from within the same module but not if it is called from a
second module i.e. extern inline.
When an implementation inlines is up to it, but in principle you require the
definition to be visible in the translation unit making the call.
The standard form for inline functions with external linkage in C99 is:
bob.h
-----
inline void Bob(void)
{
printf("Bob\n");
}
bob.c
-----
#include "bob.h"
extern void Bob(void);
getnames.c
----------
#include "bob.h"
void GetNames(void)
{
Bob();
}
The semantics are slightly different from C++, which originally introduced
inline, and probably what many C compilers have done as an extension.
The definition in bob.h is called an "inline definition". After including
bob.h, calls to Bob() may use the inline definition. However, the compiler is
still free to call the external definition of Bob(), ignoring the inline
definition. The compiler is not required to have an inlining capability, and
for debugging or other reasons, it may choose not to inline at any time.
To support this, there must be one external definition of Bob() in the
program. The simplest way to achieve this is to have a declaration with
"extern" as shown in bob.c above. This form tells the compiler to generate an
external definition for Bob() in bob.c.
The rules are a bit odd (as indeed are most of the rules covering
extern/static), but if you just remember that basic template, you'll be fine.
C++ does it differently - you just need to place the inline definition in the
header file, and the compiler will automatically and silently produce any
required out-of-line versions when required. It then relies on linker magic
to eliminate duplicate out-of-line versions from multiple object files. The
C99 semantics are designed to remove the need for linker magic. I believe
(but I'm not sure) that the code shown above would be valid and working C++ -
it would just ignore the extern declaration in bob.c.
For static inline functions, it's simpler. "inline" does not change the
semantics of static functions at all - any static function could be inlined
regardless of the keyword - inline merely provides a hint. This is exactly
the same as C++.
--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1728 727430
Cambridge, CB5 8HE, United Kingdom WWW:
http://www.tematic.com/