USCiLab / cereal

A C++11 library for serialization
BSD 3-Clause "New" or "Revised" License
4.17k stars 748 forks source link

Polymorphism in static lib #688

Open WaelDarwich opened 3 years ago

WaelDarwich commented 3 years ago

The attached simple example has two classes, parent in static library and child in application

Unfortunately CEREAL_REGISTER_POLYMORPHIC_RELATION(Parent, Child) does not trigger registration for child class, and using CEREAL_FORCE_DYNAMIC_INIT/CEREAL_REGISTER_DYNAMIC_INIT as explained here did not help, it always ends with error: Trying to save an unregistered polymorphic type (class Child). Make sure your type is registered with CEREAL_REGISTER_TYPE and that the archive you are using was included (and registered with CEREAL_REGISTER_ARCHIVE) prior to calling CEREAL_REGISTER_TYPE. If your type is already registered and you still see this error, you may need to use CEREAL_REGISTER_DYNAMIC_INIT.

The only way to get child class type registered is to include its header in the parent, which would not work in real life scenario

See attached example using version 1.3.0: Polymorphism.zip

CPP-Team-Ltd commented 3 years ago

Surprisingly, including archives/json.hpp in child class source fixed serialization, but only with static library, not dynamic one!

dimateos commented 5 months ago

In "Foo.h", including the Archive type before the CEREAL_REGISTER_TYPE macro seems to work for me:

#pragma once

#include "cereal/archives/json.hpp" // <-- before parent.h
#include "Parent.h" 

But seems like so far also only for static libs