Put the serialization of a class into a DLL

不羁的心 提交于 2019-12-03 13:55:02

I ran into a similar issue recently, 3 years after this question was asked. I finally found out a workaround to solve it. In the example above.

  • Bar is a subclass of Foo, so it must be registered/exported;
  • serializeFoo.cpp instantiates a GUID template class to register/export Foo;
  • serializeBar.cpp instantiates a GUID template class to register/export Bar;
  • The rules to include all necessary archive types before exporting the class keys are respected;
  • Both translation units are linked together to create a DLL.

I assume in your exe, while you are trying to serialise a Foo* pointer pointing to a Bar object, you got the "unregistered class blahblah" error. This is because Boost.Serialization somehow does not properly generate a GUID for class Bar before the serialize function is called.

I don't know why this happens, but it seems that GUID is generated in a lazy way -- if none of the symbols from the translation unit serializeBar.cpp is used, none of the instantiation/initialization code defined in that translation unit will be performed -- that includes the class registration/exportation of Bar.

To prove that, you can try to use a (dummy) symbol in serializeBar.cpp (e.g. by calling a dummy function implemented in serializeBar.cpp) before calling any serialization function for Foo*. The issue should disappear.

Hope it helps.

the test suite and demo distributed with the serialization library demonstrate exactly this facility. So first make sure that works. Then compare your example to it.

Robert Ramey

"Honestly, IMHO, at some points, Boost Serialization crosses the border of what is reliably possible with pure c++ build model w/o external tools..."

hmmmmmm - so it crosses the boarder as what's possible? You're pretty much correct in spirit though. Great effort was expended to implement everything that was considered necessary for such a package to be acceptable into boost.

RR

Hard to tell... There are many chances for things to go wrong. I recommend to download the test code for boost serialization (www.boost.org/doc/libs/1_48_0/libs/serialization/test/). Have a look at the test cases around A.h, A.cpp and dll_a.cpp (which basically test your scenario) and try to make it work outside of the boost test system: use your build environment, try to modify compiler/linker options to match those of boost test suite for your toolset.

Honestly, IMHO, at some points, Boost Serialization crosses the border of what is reliably possible with pure c++ build model w/o external tools...

Przemyslaw Szeptycki

Use BOOST_CLASS_EXPORT to register all your classes which you would like to serialize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!