Boost logo

Boost Users :

Subject: Re: [Boost-users] [Exception] Duplicate symbol error when using errinfo_api_function in static library
From: Niall Douglas (s_sourceforge_at_[hidden])
Date: 2013-10-03 12:41:26


On 2 Oct 2013 at 17:21, Emil Dotchevski wrote:

> > *ClassA.o*
> > *0000000000059860 s __ZTIN5boost21errinfo_api_function_E*
> > *
> > *
> > *ClassB.o*
> > *0000000000059870 s __ZTIN5boost21errinfo_api_function_E *
> >
>
> Instantiating a template in two compilation units should not lead to link
> errors. That's why I can't understand why this happens.

Instantiating a template in two compilation units is no different to
defining a non-static function twice. Unless its symbols are marked
as composable (weak on ELF, selectany on PE) the linker will error.
The only portable way of marking symbols as composable in C++ is the
"inline" keyword which is auto-added by a C++ compiler for functions
defined inline in a class definition. Given that most modern C++
compilers don't really take much hinting from inline anymore, inline
now really equals "set this symbol to weak/selectany" i.e. don't link
error if I instantiate this more than once.

My point is that instantiating a template in multiple compilation
units needs all of its member functions to be marked inline in order
to prevent link error. Or, as the OP has found, one can hack around
the problem by overriding symbol visibility and forcing multiple
copies in each compilation unit.

Niall

-- 
Currently unemployed and looking for work.
Work Portfolio: http://careers.stackoverflow.com/nialldouglas/



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net