extern inline

Discussion in 'C Programming' started by evan, Mar 6, 2005.

  1. evan

    evan Guest

    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
    evan, Mar 6, 2005
    #1
    1. Advertising

  2. evan

    joshc Guest

    > 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.
    joshc, Mar 6, 2005
    #2
    1. Advertising

  3. evan

    Kevin Bracey Guest

    In message <>
    "evan" <> 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/
    Kevin Bracey, Mar 7, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Abhi
    Replies:
    2
    Views:
    736
    E. Robert Tisdale
    Jul 3, 2003
  2. Thomas Matthews
    Replies:
    5
    Views:
    2,414
    tom_usenet
    Aug 2, 2004
  3. Sean
    Replies:
    4
    Views:
    669
    Rolf Magnus
    Apr 30, 2006
  4. sinbad

    extern inline

    sinbad, Jul 16, 2009, in forum: C Programming
    Replies:
    8
    Views:
    1,135
    Flash Gordon
    Jul 16, 2009
  5. Andre
    Replies:
    5
    Views:
    537
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page