Search code examples
c++serializationboost

multiple definition boost serialization


I use a library (https://github.com/tipaldi/flirtlib) that uses boost serialization on derived polymorphic classes. My problem is that I need to include the header in different places and this causes the linking error : multiple definition of boost::archive::detail::extra_detail::init_guid

In the .h of the class the boost export macro is : BOOST_CLASS_EXPORT(BetaGrid)

The solution I found suggests to replace this macro with BOOST_CLASS_EXPORT_KEY(BetaGrid) in the .h and BOOST_CLASS_EXPORT_IMPLEMENT(BetaGrid) in the .cpp. Using this solution, the linking works but I get runtime error saying that the derived class is not registered.

I tried many other Macro combinations but nothing I tried works.

This issue with the boost serialization is here since many years now...


Solution

  • My crystal ball says that you missed the documentation that states you have to include the headers for all archive types that you are going to be using at the point of exporting macros.

    From the docs: Exporting Class Serialization

    BOOST_CLASS_EXPORT in the same source module that includes any of the archive class headers will instantiate code required to serialize polymorphic pointers of the indicated type to the all those archive classes. If no archive class headers are included, then no code will be instantiated.