fails to link with GCC on mac .. works with VC on Win. What's wrong and how to fix?

Discussion in 'C++' started by Inertial, Aug 23, 2010.

  1. Inertial

    Inertial Guest

    I have the following source (cut down from a larger real source code).

    It compiles and links and runs ok in MS VC2008 with extensions turned off
    .... under GCC on a mac, it compiles, but linker complains

    ===
    template <class T> class Test {
    public:
    class SubClass {
    public:
    SubClass() : i(1) {}
    int i;
    };
    static SubClass statics;
    };

    template<> Test<int> :: SubClass Test<int> :: statics;

    int _tmain(int argc, _TCHAR* argv[])
    {
    int j = Test<int> :: statics.i;
    return 0;
    }
    ===

    Any ideas as to why this won't link and how to make this work???
     
    Inertial, Aug 23, 2010
    #1
    1. Advertising

  2. Inertial

    Ian Collins Guest

    Re: fails to link with GCC on mac .. works with VC on Win. What'swrong and how to fix?

    On 08/23/10 04:06 PM, Inertial wrote:
    > I have the following source (cut down from a larger real source code).
    >
    > It compiles and links and runs ok in MS VC2008 with extensions turned
    > off .... under GCC on a mac, it compiles, but linker complains


    how?

    > ===
    > template <class T> class Test {
    > public:
    > class SubClass {
    > public:
    > SubClass() : i(1) {}
    > int i;
    > };
    > static SubClass statics;
    > };
    >
    > template<> Test<int> :: SubClass Test<int> :: statics;
    >
    > int _tmain(int argc, _TCHAR* argv[])
    > {
    > int j = Test<int> :: statics.i;
    > return 0;
    > }
    > ===
    >
    > Any ideas as to why this won't link and how to make this work???


    Use the correct form of main.

    --
    Ian Collins
     
    Ian Collins, Aug 23, 2010
    #2
    1. Advertising

  3. Inertial

    Inertial Guest

    "Ian Collins" wrote in message news:...
    >
    >On 08/23/10 04:06 PM, Inertial wrote:
    >> I have the following source (cut down from a larger real source code).
    >>
    >> It compiles and links and runs ok in MS VC2008 with extensions turned
    >> off .... under GCC on a mac, it compiles, but linker complains

    >
    >how?


    How what?

    >> ===
    >> template <class T> class Test {
    >> public:
    >> class SubClass {
    >> public:
    >> SubClass() : i(1) {}
    >> int i;
    >> };
    >> static SubClass statics;
    >> };
    >>
    >> template<> Test<int> :: SubClass Test<int> :: statics;
    >>
    >> int _tmain(int argc, _TCHAR* argv[])


    sorry .. should be 'main' and 'char' ... not '_tmain' and '_TCHAR' (which
    was from my VC version .. copied wrong one)

    >> {
    >> int j = Test<int> :: statics.i;
    >> return 0;
    >> }
    >> ===
    >>
    >> Any ideas as to why this won't link and how to make this work???

    >
    >Use the correct form of main.


    Not at all helpful.

    I've got a gcc onto my windows PC so can confirm

    Here is correct source...

    ==
    template <class T> class Test {
    public:
    class SubClass {
    public:
    SubClass() : i(1) {}
    int i;
    };
    static SubClass statics;
    };

    template<> Test<int> :: SubClass Test<int> :: statics;

    int main(int argc, char* argv[])
    {
    int j = Test<int> :: statics.i;
    return 0;
    }
    ==

    Here is error message

    C:\Users\Roger\AppData\Local\Temp/ccusaaaa.o:bug.cpp:(.text+0x17): undefined
    reference to `Test<int>::statics'

    So .. any help please on this?
     
    Inertial, Aug 23, 2010
    #3
  4. Inertial

    Inertial Guest

    "Inertial" wrote in message
    news:4c71f941$0$11126$...
    >


    Further .. note that if I do NOT use a class within the class .. it works
    i.e. this works

    ==
    template <class T> class Test {
    public:
    static int i;
    };

    template<> int Test<int>::i = 1;

    int main (int argc, char * const argv[])
    {
    int j = Test<int> :: i;
    return 0;
    }
    ==

    this fails

    ==
    template <class T> class Test {
    public:
    class SubClass {
    public:
    SubClass() : i(1) {}
    int i;
    };
    static SubClass statics;
    };

    template<> Test<int> :: SubClass Test<int> :: statics;

    int main(int argc, char* const argv[])
    {
    int j = Test<int> :: statics.i;
    return 0;
    }
    ==
     
    Inertial, Aug 23, 2010
    #4
  5. Inertial

    Öö Tiib Guest

    Re: fails to link with GCC on mac .. works with VC on Win. What'swrong and how to fix?

    On 23 aug, 07:29, "Inertial" <> wrote:
    > "Ian Collins"  wrote in messagenews:...
    >
    > >On 08/23/10 04:06 PM, Inertial wrote:
    > >> I have the following source (cut down from a larger real source code).

    >
    > >> It compiles and links and runs ok in MS VC2008 with extensions turned
    > >> off .... under GCC on a mac, it compiles, but linker complains

    >
    > >how?

    >
    > How what?
    >
    > >> ===
    > >> template <class T> class Test {
    > >> public:
    > >> class SubClass {
    > >> public:
    > >> SubClass() : i(1) {}
    > >> int i;
    > >> };
    > >> static SubClass statics;
    > >> };

    >
    > >> template<> Test<int> :: SubClass Test<int> :: statics;

    >
    > >> int _tmain(int argc, _TCHAR* argv[])

    >
    > sorry .. should be 'main' and 'char' ... not '_tmain' and '_TCHAR' (which
    > was from my VC version .. copied wrong one)
    >
    > >> {
    > >> int j = Test<int> :: statics.i;
    > >> return 0;
    > >> }
    > >> ===

    >
    > >> Any ideas as to why this won't link and how to make this work???

    >
    > >Use the correct form of main.

    >
    > Not at all helpful.
    >
    > I've got a gcc onto my windows PC so can confirm
    >
    > Here is correct source...
    >
    > ==
    > template <class T> class Test {
    > public:
    >     class SubClass {
    >     public:
    >         SubClass() : i(1) {}
    >         int i;
    >     };
    >     static SubClass statics;
    >
    > };
    >
    > template<> Test<int> :: SubClass Test<int> :: statics;
    >
    > int main(int argc, char* argv[])
    > {
    >     int j = Test<int> :: statics.i;
    >     return 0;}
    >
    > ==
    >
    > Here is error message
    >
    > C:\Users\Roger\AppData\Local\Temp/ccusaaaa.o:bug.cpp:(.text+0x17): undefined
    > reference to `Test<int>::statics'
    >
    > So .. any help please on this?


    Add such line after first "==" of yours and try again:

    struct SubClass { int i; };
     
    Öö Tiib, Aug 23, 2010
    #5
  6. Inertial

    Inertial Guest

    Re: fails to link with GCC on mac .. works with VC on Win. What's wrong and how to fix?

    "Öö Tiib" wrote in message
    news:...
    >Add such line after first "==" of yours and try again:
    >
    > struct SubClass { int i; };


    That will then have TWO SubClass classes defined .. one global and one
    within the Test<T> template class. So not really an answer and doesn't help
     
    Inertial, Aug 23, 2010
    #6
  7. Inertial

    Öö Tiib Guest

    Re: fails to link with GCC on mac .. works with VC on Win. What'swrong and how to fix?

    On 23 aug, 07:45, "Inertial" <> wrote:
    > "Öö Tiib"  wrote in message
    >
    > news:...
    >
    > >Add such line after first "==" of yours and try again:

    >
    > > struct SubClass { int i; };

    >
    > That will then have TWO SubClass classes defined .. one global and one
    > within the Test<T> template class.  So not really an answer and doesn't help


    OK ... then remove template<> before:

    Test<int> :: SubClass Test<int> :: statics;
     
    Öö Tiib, Aug 23, 2010
    #7
  8. Inertial

    Inertial Guest

    Re: fails to link with GCC on mac .. works with VC on Win. What's wrong and how to fix?

    "Öö Tiib" wrote in message
    news:...
    >OK ... then remove template<> before:
    >
    >Test<int> :: SubClass Test<int> :: statics;


    No .. that is required (tho older compilers compile without it).

    Gcc will give a compiler error if it is missing

    error: too few template-parameter-lists

    So again, does not help
     
    Inertial, Aug 23, 2010
    #8
  9. Inertial

    Inertial Guest

    "Inertial" wrote in message
    news:4c71fa63$0$11113$...

    also note that moving the 'Subclass' out of the template class Test does not
    help.

    So this also fails to link with the same message

    ==
    template <class T> class SubClass {
    public:
    SubClass() : i(1) {}
    int i;
    };

    template <class T> class Test {
    public:
    static SubClass<T> statics;
    };

    template<> SubClass<int> Test<int> :: statics;

    int main(int argc, char* const argv[])
    {
    int j = Test<int> :: statics.i;
    return 0;
    }
    ==

    C:\Users\Roger\AppData\Local\Temp/cc0oeaaa.o:bug3.cpp:(.text+0x17):
    undefined reference to `Test<int>::statics'
     
    Inertial, Aug 23, 2010
    #9
  10. Inertial

    Inertial Guest

    However, moving the 'Subclass' and the static out DOES work ..

    ==
    template <class T> class SubClass {
    public:
    SubClass() : i(1) {}
    int i;
    };

    SubClass<int> statics;

    template <class T> class Test {
    public:
    };

    int main(int argc, char* const argv[])
    {
    int j = statics.i;
    return 0;
    }
    ==

    but it still doesn't answer the question as to why the following code fails

    ==
    template <class T> class SubClass {
    public:
    SubClass() : i(1) {}
    int i;
    };

    template <class T> class Test {
    public:
    static SubClass<T> statics;
    };

    template<> SubClass<int> Test<int> :: statics;

    int main(int argc, char* const argv[])
    {
    int j = Test<int> :: statics.i;
    return 0;
    }
    ==

    C:\Users\Roger\AppData\Local\Temp/cc0oeaaa.o:bug3.cpp:(.text+0x17):
    undefined reference to `Test<int>::statics'

    So I'd still like some answer on why the above code gets that error ! :)
     
    Inertial, Aug 23, 2010
    #10
  11. Inertial

    Öö Tiib Guest

    Re: fails to link with GCC on mac .. works with VC on Win. What'swrong and how to fix?

    On 23 aug, 07:54, "Inertial" <> wrote:
    > "Öö Tiib"  wrote in message
    >
    > news:...
    >
    > >OK ... then remove template<> before:

    >
    > >Test<int> :: SubClass Test<int> :: statics;

    >
    > No .. that is required (tho older compilers compile without it).
    >
    > Gcc will give a compiler error if it is missing
    >
    > error: too few template-parameter-lists
    >
    > So again, does not help


    Hmm. I do not really know what is wrong with your strange compiler
    there. The example you posted should compile and link like it was.
     
    Öö Tiib, Aug 23, 2010
    #11
  12. Inertial

    Inertial Guest

    "Inertial" wrote in message
    news:4c7200e3$0$11107$...

    Note that the code above that does 'work' has the problem that if I want
    specialisation other than just Test<int> that I can't (back in my original
    larger project) .. as I would need a different global var for each
    SubClass<T> type.

    So would still like to know if there is some syntax change that can make
    original code compile... ie

    ==
    template <class T> class Test {
    public:
    class SubClass {
    public:
    SubClass() : i(1) {}
    int i;
    };
    static SubClass statics;
    };

    template<> Test<int> :: SubClass Test<int> :: statics;

    int main(int argc, char* const argv[])
    {
    int j = Test<int> :: statics.i;
    return 0;
    }
    ==

    any ideas?
     
    Inertial, Aug 23, 2010
    #12
  13. Inertial

    Inertial Guest

    Re: fails to link with GCC on mac .. works with VC on Win. What's wrong and how to fix?

    "Öö Tiib" wrote in message
    news:...
    >Hmm. I do not really know what is wrong with your strange compiler
    >there. The example you posted should compile and link like it was.


    GCC on mac fails .. GCC on windows fails. Looks like its how GCC behaves ..
    I don't know why GCC should be 'strange'. Its usually pretty good with
    getting things right.
     
    Inertial, Aug 23, 2010
    #13
  14. Inertial

    Öö Tiib Guest

    Re: fails to link with GCC on mac .. works with VC on Win. What'swrong and how to fix?

    On 23 aug, 08:02, "Inertial" <> wrote:
    > However, moving the 'Subclass' and the static out DOES work ..
    >
    > ==
    > template <class T> class SubClass {
    > public:
    >     SubClass() : i(1) {}
    >     int i;
    >
    > };
    >
    > SubClass<int> statics;
    >
    > template <class T> class Test {
    > public:
    >
    > };
    >
    > int main(int argc, char* const argv[])
    > {
    >     int j = statics.i;
    >     return 0;}
    >
    > ==
    >
    > but it still doesn't answer the question as to why the following code fails
    >
    > ==
    > template <class T> class SubClass {
    > public:
    >     SubClass() : i(1) {}
    >     int i;
    >
    > };
    >
    > template <class T> class Test {
    > public:
    >     static SubClass<T> statics;
    >
    > };
    >
    > template<> SubClass<int> Test<int> :: statics;
    >
    > int main(int argc, char* const argv[])
    > {
    >     int j = Test<int> :: statics.i;
    >     return 0;}
    >
    > ==
    >
    > C:\Users\Roger\AppData\Local\Temp/cc0oeaaa.o:bug3.cpp:(.text+0x17):
    > undefined reference to `Test<int>::statics'
    >
    > So I'd still like some answer on why the above code gets that error ! :)


    Maybe it is some strange thing about name mangling? Did you try with g+
    +?
     
    Öö Tiib, Aug 23, 2010
    #14
  15. Inertial

    Öö Tiib Guest

    Re: fails to link with GCC on mac .. works with VC on Win. What'swrong and how to fix?

    On 23 aug, 08:09, "Inertial" <> wrote:
    > "Öö Tiib"  wrote in message
    >
    > news:...
    >
    > >Hmm. I do not really know what is wrong with your strange compiler
    > >there. The example you posted should compile and link like it was.

    >
    > GCC on mac fails .. GCC on windows fails.  Looks like its how GCC behaves ..
    > I don't know why GCC should be 'strange'.  Its usually pretty good with
    > getting things right.


    There must be something utterly wrong. Some sort of bad DEVC++ project
    file, wrongly installed GCC ... really strange.
     
    Öö Tiib, Aug 23, 2010
    #15
  16. Inertial

    Inertial Guest

    Re: fails to link with GCC on mac .. works with VC on Win. What's wrong and how to fix?

    "Öö Tiib" wrote in message
    news:...
    >Maybe it is some strange thing about name mangling? Did you try with g++?


    You mean the g++ that is part of gcc .. yeup.

    Have YOU tried the code? If so, did it work? And if so using what compiler
    and version
     
    Inertial, Aug 23, 2010
    #16
  17. Inertial

    Inertial Guest

    Re: fails to link with GCC on mac .. works with VC on Win. What's wrong and how to fix?

    "Öö Tiib" wrote in message
    news:...
    >
    >On 23 aug, 08:09, "Inertial" <> wrote:
    >> "Öö Tiib" wrote in message
    >>
    >> news:...
    >>
    >> >Hmm. I do not really know what is wrong with your strange compiler
    >> >there. The example you posted should compile and link like it was.

    >>
    >> GCC on mac fails .. GCC on windows fails. Looks like its how GCC behaves
    >> ..
    >> I don't know why GCC should be 'strange'. Its usually pretty good with
    >> getting things right.

    >
    >There must be something utterly wrong. Some sort of bad DEVC++ project
    >file, wrongly installed GCC ... really strange.


    What DEVC++ project file? Just freshly installed a GCC environment .. used
    "c++ bug.cpp" where bug.cpp is the 'bad' code, and get the error. Also used
    the gcc compiler for XCode on a Mac .. same thing. VS2008 C++ compiler
    doesn't complain, however, but its not as standard conforming.

    Have YOU tried to compile the examples that failed (and worked) that I
    posted .. and if so, using which compiler and version?
     
    Inertial, Aug 23, 2010
    #17
  18. Inertial

    Jonathan Lee Guest

    Re: fails to link with GCC on mac .. works with VC on Win. What'swrong and how to fix?

    On Aug 22, 11:29 pm, "Inertial" <> wrote:
    > Here is correct source...
    >
    > ==
    > template <class T> class Test {
    > public:
    >     class SubClass {
    >     public:
    >         SubClass() : i(1) {}
    >         int i;
    >     };
    >     static SubClass statics;
    >
    > };
    >
    > template<> Test<int> :: SubClass Test<int> :: statics;


    I haven't checked the spec, but this appears to be working as
    a declaration instead of definition. I did the following and it
    worked fine...

    template<>
    class Test<int> {
    public:
    class SubClass {

    public:
    SubClass() : i(1) {}
    int i;
    };
    static SubClass statics;
    };

    Test<int>::SubClass Test<int>::statics;

    Also, this worked, but implies a copy constructor

    template<> Test<int>::SubClass Test<int>::statics = SubClass();

    I haven't checked, but I assume the way you wrote it is
    being treated as specialization only, and not definition.

    --Jonathan
     
    Jonathan Lee, Aug 23, 2010
    #18
  19. Inertial

    Geoff Guest

    Re: fails to link with GCC on mac .. works with VC on Win. What's wrong and how to fix?

    On Mon, 23 Aug 2010 15:28:14 +1000, "Inertial" <>
    wrote:

    >"Öö Tiib" wrote in message
    >news:...
    >>Maybe it is some strange thing about name mangling? Did you try with g++?

    >
    >You mean the g++ that is part of gcc .. yeup.
    >
    >Have YOU tried the code? If so, did it work? And if so using what compiler
    >and version


    g++ and gcc are the same under Xcode. I get the same error on OS/X
    10.4 and gcc 4.0.1 under Xcode 2.5.
     
    Geoff, Aug 23, 2010
    #19
  20. Inertial

    Ian Collins Guest

    Re: fails to link with GCC on mac .. works with VC on Win. What'swrong and how to fix?

    On 08/23/10 05:33 PM, Jonathan Lee wrote:
    > On Aug 22, 11:29 pm, "Inertial"<> wrote:
    >> Here is correct source...
    >>
    >> ==
    >> template<class T> class Test {
    >> public:
    >> class SubClass {
    >> public:
    >> SubClass() : i(1) {}
    >> int i;
    >> };
    >> static SubClass statics;
    >>
    >> };
    >>
    >> template<> Test<int> :: SubClass Test<int> :: statics;

    >
    > I haven't checked the spec, but this appears to be working as
    > a declaration instead of definition. I did the following and it
    > worked fine...


    Yes, it looks like the declaration of a specialisation.

    > template<>
    > class Test<int> {
    > public:
    > class SubClass {
    >
    > public:
    > SubClass() : i(1) {}
    > int i;
    > };
    > static SubClass statics;
    > };
    >
    > Test<int>::SubClass Test<int>::statics;


    That is how it should be.

    --
    Ian Collins
     
    Ian Collins, Aug 23, 2010
    #20
    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. Xah Lee
    Replies:
    22
    Views:
    1,144
    Tim Roberts
    Mar 21, 2006
  2. Xah Lee
    Replies:
    23
    Views:
    1,090
    Tim Roberts
    Mar 21, 2006
  3. Krist
    Replies:
    6
    Views:
    756
    Arne Vajhøj
    May 7, 2010
  4. alberta soranzo

    link won't open correctly in email (mac and win)

    alberta soranzo, May 4, 2004, in forum: Javascript
    Replies:
    7
    Views:
    184
    Thomas 'PointedEars' Lahn
    May 28, 2004
  5. Xah Lee
    Replies:
    21
    Views:
    808
    Tim Roberts
    Mar 21, 2006
Loading...

Share This Page