多定义提升序列化

multiple definition boost serialization

我使用的库 (https://github.com/tipaldi/flirtlib) 在派生的多态 classes 上使用 boost 序列化。我的问题是我需要在不同的地方包含 header,这会导致链接错误: boost::archive::detail::extra_detail::init_guid

的多重定义

在 class 的 .h 中,boost 导出宏是:BOOST_CLASS_EXPORT(BetaGrid)

我找到的解决方案建议用 .h 中的 BOOST_CLASS_EXPORT_KEY(BetaGrid) 和 .cpp 中的 BOOST_CLASS_EXPORT_IMPLEMENT(BetaGrid) 替换此宏。使用此解决方案,链接有效,但我收到运行时错误,指出派生的 class 未注册。

我尝试了很多其他的宏组合,但没有任何效果。

boost 序列化的这个问题已经存在很多年了...

我的 crystal 球说你错过了说明你必须包含 headers 的文档,你将在导出宏时使用的所有存档类型。

来自文档: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.