static inline function not found during linking

Discussion in 'C++' started by ciccio, Dec 21, 2007.

  1. ciccio

    ciccio Guest

    Hi,

    I have a problem with my code that the compiler does not find any inline
    functions which are static.

    The simple code example is written below, this is what the compiler
    throws at me.

    ] $ g++ main.cpp foo.cpp
    /home/klaas/tmp/cciAcYgl.o: In function `main':
    inline.cpp:(.text+0x9): undefined reference to `foo::bar()'
    collect2: ld returned 1 exit status

    Is this normal behaviour??? I seriously doubt that.
    If foo::bar(void) is not inline, it works.
    When everything is written in one file, it works too.

    regards,


    This is the simple code

    == foo.hpp ==
    #ifndef _FOO_
    #define _FOO_
    class foo {
    public:
    static int a;
    static int bar(void) ;
    };
    #endif
    == foo.cpp ==
    #include "foo.hpp"
    int foo::a = 0;
    inline int foo::bar(void) { return a; }
    == main.cpp ==
    #include "foo.hpp"
    int main(void) {
    int b = foo::bar();
    return 0;
    };
    ==========
    ciccio, Dec 21, 2007
    #1
    1. Advertising

  2. ciccio wrote:
    > I have a problem with my code that the compiler does not find any
    > inline functions which are static.


    Since your function is never declared 'inline' when 'main.cpp' is
    compiled, the compiler expects a definition of that function to exist
    somewhere. Since the implementation of that function is declared
    'inline' in 'foo.cpp', the body is essentially ignored because it is
    not called anywhere in 'foo.cpp'.

    Either put the function in the header (where declaring it 'inline'
    makes sense), or remove the 'inline' modifier from the function
    definition in 'foo.cpp'.

    >
    > The simple code example is written below, this is what the compiler
    > throws at me.
    >
    > ] $ g++ main.cpp foo.cpp
    > /home/klaas/tmp/cciAcYgl.o: In function `main':
    > inline.cpp:(.text+0x9): undefined reference to `foo::bar()'
    > collect2: ld returned 1 exit status
    >
    > Is this normal behaviour??? I seriously doubt that.
    > If foo::bar(void) is not inline, it works.
    > When everything is written in one file, it works too.
    >
    > regards,
    >
    >
    > This is the simple code
    >
    > == foo.hpp ==
    > #ifndef _FOO_
    > #define _FOO_
    > class foo {
    > public:
    > static int a;
    > static int bar(void) ;
    > };
    > #endif
    > == foo.cpp ==
    > #include "foo.hpp"
    > int foo::a = 0;
    > inline int foo::bar(void) { return a; }
    > == main.cpp ==
    > #include "foo.hpp"
    > int main(void) {
    > int b = foo::bar();
    > return 0;
    > };
    > ==========


    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Dec 21, 2007
    #2
    1. Advertising

  3. ciccio

    ciccio Guest

    Thanks for the quick response,

    I just read it in stroustrup. I had not a clue!

    Regards


    Victor Bazarov wrote:
    > ciccio wrote:
    >> I have a problem with my code that the compiler does not find any
    >> inline functions which are static.

    >
    > Since your function is never declared 'inline' when 'main.cpp' is
    > compiled, the compiler expects a definition of that function to exist
    > somewhere. Since the implementation of that function is declared
    > 'inline' in 'foo.cpp', the body is essentially ignored because it is
    > not called anywhere in 'foo.cpp'.
    >
    > Either put the function in the header (where declaring it 'inline'
    > makes sense), or remove the 'inline' modifier from the function
    > definition in 'foo.cpp'.
    >
    >> The simple code example is written below, this is what the compiler
    >> throws at me.
    >>
    >> ] $ g++ main.cpp foo.cpp
    >> /home/klaas/tmp/cciAcYgl.o: In function `main':
    >> inline.cpp:(.text+0x9): undefined reference to `foo::bar()'
    >> collect2: ld returned 1 exit status
    >>
    >> Is this normal behaviour??? I seriously doubt that.
    >> If foo::bar(void) is not inline, it works.
    >> When everything is written in one file, it works too.
    >>
    >> regards,
    >>
    >>
    >> This is the simple code
    >>
    >> == foo.hpp ==
    >> #ifndef _FOO_
    >> #define _FOO_
    >> class foo {
    >> public:
    >> static int a;
    >> static int bar(void) ;
    >> };
    >> #endif
    >> == foo.cpp ==
    >> #include "foo.hpp"
    >> int foo::a = 0;
    >> inline int foo::bar(void) { return a; }
    >> == main.cpp ==
    >> #include "foo.hpp"
    >> int main(void) {
    >> int b = foo::bar();
    >> return 0;
    >> };
    >> ==========

    >
    ciccio, Dec 21, 2007
    #3
  4. ciccio

    ciccio Guest

    Okay I got this part now,

    But ... when I use templates this is not the case.

    Here is again an example (now without the static, since that does not
    matter).

    The compilation does not give any linking error here.
    So what is the reason of this then? Does this mean that inline
    functions of templates are just bogus?

    Regards


    == foo.hpp ==
    #ifndef _FOO_
    #define _FOO_

    template<typename T>
    class foo {
    public:
    int a;
    int bar(void) ;
    };
    #endif
    == foo.cpp ==
    #include "foo.hpp"
    template<typename T>
    inline int foo<T>::bar(void) { return a; }
    template class foo<int>;
    == main.cpp ==
    #include "foo.hpp"
    int main(void) {
    int b = foo<int>().bar();
    return 0;
    };
    ciccio, Dec 21, 2007
    #4
  5. ciccio wrote:
    > Okay I got this part now,
    >
    > But ... when I use templates this is not the case.


    What is not the case?

    > Here is again an example (now without the static, since that does not
    > matter).
    >
    > The compilation does not give any linking error here.
    > So what is the reason of this then? Does this mean that inline
    > functions of templates are just bogus?


    Not "inline functions of templates", but "function templates declared
    inline".

    Right after defining your function and declaring it 'inline' (which
    you're not really supposed to do, so your compiler _probably_ ignored
    it), you _explicitly_ instantiate your template, which basically
    introduces the definitions of all member functions (if it can, and it
    should be able to, since you just defined your function), which are
    of course _not_ inline.

    IOW, whatever you declared 'inline' is *not* the function the linker
    is looking for when resolving the reference from 'main'. It's looking
    for 'foo<int>::bar', not for 'foo<T>::bar'. And your 'foo<int>::bar'
    is non-inline (as the result of explicit instantiation).

    >
    > Regards
    >
    >
    > == foo.hpp ==
    > #ifndef _FOO_
    > #define _FOO_
    >
    > template<typename T>
    > class foo {
    > public:
    > int a;
    > int bar(void) ;
    > };
    > #endif
    > == foo.cpp ==
    > #include "foo.hpp"
    > template<typename T>
    > inline int foo<T>::bar(void) { return a; }
    > template class foo<int>;
    > == main.cpp ==
    > #include "foo.hpp"
    > int main(void) {
    > int b = foo<int>().bar();
    > return 0;
    > };


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Dec 21, 2007
    #5
  6. ciccio

    red floyd Guest

    ciccio wrote:
    > Okay I got this part now,
    >
    > But ... when I use templates this is not the case.
    >
    > Here is again an example (now without the static, since that does not
    > matter).
    >
    > The compilation does not give any linking error here.
    > So what is the reason of this then? Does this mean that inline
    > functions of templates are just bogus?
    >
    > Regards
    >
    >
    > == foo.hpp ==
    > #ifndef _FOO_
    > #define _FOO_
    >
    > template<typename T>
    > class foo {
    > public:
    > int a;
    > int bar(void) ;
    > };
    > #endif
    > == foo.cpp ==
    > #include "foo.hpp"
    > template<typename T>
    > inline int foo<T>::bar(void) { return a; }
    > template class foo<int>;
    > == main.cpp ==
    > #include "foo.hpp"
    > int main(void) {
    > int b = foo<int>().bar();
    > return 0;
    > };


    Irrelevant to your issue, but still a problem, is your include guard.

    Any identifier with a leading underscore, followed by an uppercase
    letter, is reserved to the implementation. Period. You may not use it
    for your own purposes.

    So your include guard of _FOO_ is illegal.
    red floyd, Dec 21, 2007
    #6
    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. Nish
    Replies:
    4
    Views:
    484
    Thomas Stegen
    Oct 8, 2004
  2. TGOS
    Replies:
    3
    Views:
    375
    Kevin Bracey
    Feb 28, 2005
  3. Ajay
    Replies:
    5
    Views:
    384
    Pete Becker
    Apr 1, 2006
  4. Sean
    Replies:
    4
    Views:
    665
    Rolf Magnus
    Apr 30, 2006
  5. regis
    Replies:
    3
    Views:
    1,487
    regis
    Oct 10, 2008
Loading...

Share This Page