[cfe-users] Linking problem with implicit instantiation of constructor/destructor

David Blaikie via cfe-users cfe-users at lists.llvm.org
Fri Apr 17 10:52:22 PDT 2020


I don't believe this code is valid according to C++. I believe it would
require an explicit instantiation of the ctor/dtor somewhere to make that
code valid - though I don't have chapter and verse on the spec at hand just
now to back that up.

On Fri, Apr 17, 2020 at 6:54 AM Jaroslav Zeman via cfe-users <
cfe-users at lists.llvm.org> wrote:

> Hello,
>
> I've run into wieird problem when trying to compile and link our programs
> with
> clang++ on linux instead of g++. It occurs, when:
>  - template class member definitions are separated from the definition of
> the
> class
> - no explicit instantiation is done
> - member definitions are available only in some of the units, where the
> template is being used.
> (Yeah, our code is a mess)
>
> This is simple expamle:
> // ----- template.h --------
> template< typename T >
> struct Template {
>   Template();
>   ~Template();
> };
>
> void doSomething(Template<int>& t);
>
> // ----- template.cpp --------
> #include "template.h"
>
> template< typename T >
> Template< T >::Template() { }
>
> template< typename T>
> Template< T >::~Template() { }
>
> void doSomething(Template<int>& t) {
>   Template<int> new_t;
>   t = new_t;
> }
>
> // ----- main.cpp --------
> #include "template.h"
>
> int main(int argc_, char** argv_) {
>
>   Template<int> t;
>   doSomething(t);
>
>   return 0;
> }
> // ----- end of code
>
> When compiled with clang++:
> $ clang++ -o test main.cpp template.cpp
> /usr/bin/ld: /tmp/main-e2fa2c.o: in function `main':
> main.cpp:(.text+0x2f): undefined reference to `Template<int>::Template()'
> /usr/bin/ld: main.cpp:(.text+0x4d): undefined reference to
> `Template<int>::~Template()'
> /usr/bin/ld: main.cpp:(.text+0x82): undefined reference to
> `Template<int>::~Template()'
>
> reading the object files using nm tool shows, the symbol for destructor
> instantiated in template.cpp is _ZN8TemplateIiED2Ev, but the main.o
> requires
> symbol _ZN8TemplateIiED1Ev. Notice the difference in one digit: D2 vs D1.
>
> So symbol ...D1... is required, but only ...D2... is available. The D1
> version
> is generated by clang only for explicit instantiation. g++ generates both
> D1
> and D2 for any type of instantiation.
>
> Can this be considered a bug of clang++? Or does this behaior have some
> purpose?
>
> Regards,
> JZ
>
>
> _______________________________________________
> cfe-users mailing list
> cfe-users at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20200417/c7d296b5/attachment.html>


More information about the cfe-users mailing list