Inline not working properly in GCC?

Discussion in 'C Programming' started by James Douglas, Feb 2, 2009.

  1. I've copy-pasted the following from my terminal. I thought "inline"
    functions were allowed to have multiple identical definitions, but GCC is
    complaining.

    $ cat f.h
    inline void Func(void)
    {

    }

    $ cat a.c
    #include "f.h"

    int main(void)
    {
    Func();

    return 0;
    }

    $ cat b.c
    #include "f.h"

    void Hello(void)
    {
    Func();
    }

    $ gcc -std=c99 a.c b.c
    /tmp/ccYf20Ik.o: In function `main':
    a.c:(.text+0x12): undefined reference to `Func'
    /tmp/ccsnIe6q.o: In function `Hello':
    b.c:(.text+0x7): undefined reference to `Func'
    collect2: ld returned 1 exit status

    For now I'm using the following hack:

    $ gcc -D inline=static -std=c99 a.c b.c
     
    James Douglas, Feb 2, 2009
    #1
    1. Advertising

  2. James Douglas

    Huibert Bol Guest

    James Douglas wrote:

    > I've copy-pasted the following from my terminal. I thought "inline"
    > functions were allowed to have multiple identical definitions, but GCC is
    > complaining.
    >
    > $ cat f.h
    > inline void Func(void)
    > {
    >
    > }
    >
    > $ cat a.c
    > #include "f.h"
    >
    > int main(void)
    > {
    > Func();
    >
    > return 0;
    > }
    >
    > $ cat b.c
    > #include "f.h"
    >
    > void Hello(void)
    > {
    > Func();
    > }
    >
    > $ gcc -std=c99 a.c b.c
    > /tmp/ccYf20Ik.o: In function `main':
    > a.c:(.text+0x12): undefined reference to `Func'
    > /tmp/ccsnIe6q.o: In function `Hello':
    > b.c:(.text+0x7): undefined reference to `Func'
    > collect2: ld returned 1 exit status


    Section 6.7.4 paragraph 6:

    Any function with internal linkage can be an inline function. [...] If
    all of the file scope declarations for a function in a translation unit
    include the inline function specifier without extern, then the
    definition in that translation unit is an inline definition.

    [Func() is an inline definition]

    An inline definition does not provide an external definition for the
    function, and does not forbid an external definition in another
    translation unit.

    [Func() is *not* an external defintion]

    [...] It is unspecified whether a call to the function uses the inline
    definition or the external definition.

    [GCC if free to use an external definition for Func(), which you
    didn't supply]

    --
    Huibert
    "The Commercial Channel! All commercials all the time.
    An eternity of useless products to rot your skeevy little mind, forever!"
    -- Mike the TV (Reboot)
     
    Huibert Bol, Feb 2, 2009
    #2
    1. Advertising

  3. James Douglas

    Kaz Kylheku Guest

    On 2009-02-02, Trent <> wrote:
    > James Douglas wrote:
    >> I've copy-pasted the following from my terminal. I thought "inline"
    >> functions were allowed to have multiple identical definitions, but GCC is
    >> complaining.
    >>

    > [snip]
    >
    > This is because even with -std=c99 GCC doesn't comply with the C99 rules
    > for "inline" declarations.


    Is that so?

    > http://www.greenend.org.uk/rjk/2003/03/inline.html

    ^^^^

    How confident are about your statement as pertaining to current GCC?
     
    Kaz Kylheku, Feb 3, 2009
    #3
  4. Trent wrote:
    > James Douglas wrote:
    >> I've copy-pasted the following from my terminal. I thought "inline"
    >> functions were allowed to have multiple identical definitions, but GCC is
    >> complaining.
    >>

    > [snip]
    >
    > This is because even with -std=c99 GCC doesn't comply with the C99 rules
    > for "inline" declarations.
    >
    > http://www.greenend.org.uk/rjk/2003/03/inline.html


    Are you sure? GCC 4.3 and later think they implement Standard 'inline'.
    I'm sure they'd welcome a bug report if you think they're wrong.
     
    J. J. Farrell, Feb 3, 2009
    #4
    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:
    766
    E. Robert Tisdale
    Jul 3, 2003
  2. Alvin
    Replies:
    7
    Views:
    511
    E. Robert Tisdale
    May 6, 2005
  3. Nish
    Replies:
    4
    Views:
    545
    Thomas Stegen
    Oct 8, 2004
  4. Replies:
    5
    Views:
    390
    Nathan Addy
    Sep 17, 2005
  5. Jon Slaughter

    to inline or not to inline

    Jon Slaughter, Apr 20, 2007, in forum: HTML
    Replies:
    9
    Views:
    365
    Andy Dingley
    Apr 20, 2007
Loading...

Share This Page