extern inline

E

evan

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.

For example (extremely simplified),

inline void Bob(void)
{
printf("Bob\n");
}

void Ted(void)
{
printf("Ted"\n");
Bob();
}

produces...
void Ted(void)
{
printf("Bob\n");
}

but if I now refer to 'Bob' from an external file, nothing happens...
in names.c...
inline void Bob(void)
{
printf("Bob\n");
}

and in getnames.c...
extern inline void Bob(void);

void GetNames(void)
{
Bob();
}

produces...
void GetNames(void)
{
Bob();
}

I would have expected...
void GetNames(void)
{
printf("Bob\n");
}

I am using the gcc compiler. I thought that maybe it needed the -O
option (as suggested in the help files), but have had no luck.

Am I doing something wrong?

/evan
 
J

joshc

I am using the gcc compiler. I thought that maybe it needed the -O
option (as suggested in the help files), but have had no luck.

Am I doing something wrong?

Hopefully you've been referring to the following information:
http://gcc.gnu.org/onlinedocs/gcc/Inline.html . If not, take a look at
it. Because your function has external linkage it is not inlined. Try
compiling with the -Winline flag, it might help.
 
K

Kevin Bracey

In message <[email protected]>
"evan said:
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++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top