still confused about headers, inline functions, linking, and redefining

Discussion in 'C++' started by pookiebearbottom@yahoo.com, Jul 18, 2006.

  1. Guest

    Let's say I have headers Sal.h with this class

    class Sal
    {
    public:
    int doit() { return 1;}
    };

    now I know that the compilier can choose NOT to inline this function.
    So if I include this in two different libraries, they both choose to
    NOT inline it, and then I link to both libs, am I going to get/should I
    get a redefinition error?

    -sal
     
    , Jul 18, 2006
    #1
    1. Advertising

  2. wrote:
    > Let's say I have headers Sal.h with this class
    >
    > class Sal
    > {
    > public:
    > int doit() { return 1;}
    > };
    >
    > now I know that the compilier can choose NOT to inline this function.
    > So if I include this in two different libraries, they both choose to
    > NOT inline it, and then I link to both libs, am I going to get/should I
    > get a redefinition error?


    You will get a redefinition error.

    Say both library A and library B have a source file including this
    header. This means that, say, a.cpp and b.cpp, after preprocessing,
    will include the code above -- the declaration for class Sal and the
    definition for Sal::doit.

    If you link these libraries together, the linker will simply see two
    definitions for the same name, and issue an error.

    If you really want to, you can use "extern inline" to provide a
    definition only for inlining -- and then provide another definition
    somewhere, probably in a source file for the library, when the compiler
    decides not to inline the call.

    Something like this:

    Sal.h:

    class Sal
    {
    public:

    extern inline
    int doit() { return 1; }
    };

    Sal.cpp:

    int Sal::doit() { return 1; }
     
    =?iso-8859-1?q?Pedro_Lamar=E3o?=, Jul 18, 2006
    #2
    1. Advertising

  3. Pete Becker Guest

    Re: still confused about headers, inline functions, linking, andredefining

    wrote:
    > Let's say I have headers Sal.h with this class
    >
    > class Sal
    > {
    > public:
    > int doit() { return 1;}
    > };
    >
    > now I know that the compilier can choose NOT to inline this function.
    > So if I include this in two different libraries, they both choose to
    > NOT inline it, and then I link to both libs, am I going to get/should I
    > get a redefinition error?
    >


    No. The compiler will handle it.
     
    Pete Becker, Jul 18, 2006
    #3
  4. Pete Becker Guest

    Re: still confused about headers, inline functions, linking, andredefining

    Pedro Lamarão wrote:

    > wrote:
    >
    >>Let's say I have headers Sal.h with this class
    >>
    >>class Sal
    >>{
    >> public:
    >> int doit() { return 1;}
    >>};
    >>
    >>now I know that the compilier can choose NOT to inline this function.
    >>So if I include this in two different libraries, they both choose to
    >>NOT inline it, and then I link to both libs, am I going to get/should I
    >>get a redefinition error?

    >
    >
    > You will get a redefinition error.
    >


    No, the compiler will handle it.
     
    Pete Becker, Jul 18, 2006
    #4
  5. Guest

    Pete Becker wrote:
    > Pedro Lamarão wrote:
    >
    > > wrote:
    > >
    > >>Let's say I have headers Sal.h with this class
    > >>

    > > You will get a redefinition error.
    > >

    >
    > No, the compiler will handle it.


    This is why I get confused. Wouldn't it be a linker issue anyway? Is
    this covered by any "standard"?
     
    , Jul 18, 2006
    #5
  6. Artie Gold Guest

    Re: still confused about headers, inline functions, linking, andredefining

    wrote:
    > Pete Becker wrote:
    >
    >>Pedro Lamarão wrote:
    >>
    >>
    >>> wrote:
    >>>
    >>>
    >>>>Let's say I have headers Sal.h with this class
    >>>>
    >>>
    >>>You will get a redefinition error.
    >>>

    >>
    >>No, the compiler will handle it.

    >
    >
    > This is why I get confused. Wouldn't it be a linker issue anyway? Is
    > this covered by any "standard"?
    >

    It is covered by the C++ standard.

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com
    "You can't KISS* unless you MISS**"
    [*-Keep it simple, stupid. **-Make it simple, stupid.]
     
    Artie Gold, Jul 18, 2006
    #6
  7. Ron Natalie Guest

    Re: still confused about headers, inline functions, linking, andredefining

    Artie Gold wrote:

    >>
    >> This is why I get confused. Wouldn't it be a linker issue anyway? Is
    >> this covered by any "standard"?
    >>

    > It is covered by the C++ standard.
    >

    Yes, the standard says that duplicates of inline functions are allowed
    as long as they are the same sequence of tokens in the function definition.

    This is actually the only practical difference inline does. Whether
    the declaration does anything else is an implementation detail (a
    implementation is free to not inline inline functions or to inline
    those not declared inline).
     
    Ron Natalie, Jul 18, 2006
    #7
    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. Xiangliang Meng
    Replies:
    1
    Views:
    1,613
    Victor Bazarov
    Jun 21, 2004
  2. Daniel Vallstrom
    Replies:
    2
    Views:
    1,896
    Kevin Bracey
    Nov 21, 2003
  3. Sylvain Audi
    Replies:
    1
    Views:
    273
    Ian Collins
    Jan 13, 2007
  4. Rahul
    Replies:
    3
    Views:
    457
    James Kanze
    Feb 28, 2008
  5. arjor
    Replies:
    0
    Views:
    309
    arjor
    May 22, 2008
Loading...

Share This Page