在 C++ Cereal 库中使用 CEREAL_REGISTER_DYNAMIC_INIT 正确发布

Issue correctly using CEREAL_REGISTER_DYNAMIC_INIT in C++ Cereal library

我已经开始使用 lib 文件并希望正确使用 CEREAL_REGISTER_DYNAMIC_INIT。我不知道我是否需要来使用它,但我注意到我的一种谷物化类型没有在单独的 DLL 中正确拾取的问题,我认为这可能会有所帮助。

在 accountActions.h 我在文件末尾有以下内容:

CEREAL_FORCE_DYNAMIC_INIT(mv_clientactions);

在 accountActions.cpp 文件顶部附近有以下内容:

#include "clientActions.h"

#include "cereal/cereal.hpp"
#include "cereal/types/base_class.hpp"
#include "cereal/types/polymorphic.hpp"
#include "cereal/archives/adapters.hpp"

#include "cereal/archives/portable_binary.hpp"
#include "cereal/archives/json.hpp"

CEREAL_REGISTER_TYPE(CreatePlayer);
CEREAL_REGISTER_TYPE(LoginRequest);
CEREAL_REGISTER_TYPE(FindMatchRequest);
CEREAL_REGISTER_TYPE(ExpectedPlayersNoted);
CEREAL_REGISTER_DYNAMIC_INIT(mv_accountactions);

假设 mv_accountactions 只是一个完全组成的字符串。我没有任何以它命名的库或 dll,但我想它是用来 link 这两个单元在一起的?文档很少,我可能使用不正确。

我得到的错误是:

1> c:\git\bindstone\source\game\networklayer\accountactions.cpp(13): error C2084: 函数 'void cereal::detail::dynamic_init_dummy_mv_accountactions(void)' 已有主体 1> c:\git\bindstone\source\game\networklayer\accountactions.h(127): 注意:见前面'dynamic_init_dummy_mv_accountactions'

的定义

我已经仔细检查过,并没有在其他任何地方使用 mv_accountactions...我不知道是什么原因造成的,也不知道如何解决。我想知道我是否需要 CEREAL_REGISTER_DYNAMIC_INIT,或者是否有安全的方法来使用它以防万一我转移到 DLL 而我只是滥用它。

非常感谢您的建议。

问题在这里打开:https://github.com/USCiLab/cereal/issues/523

我似乎已经能够通过用之前丢失的 CEREAL_DLL_EXPORT

定义 CEREAL_FORCE_DYNAMIC_INIT 来解决这个问题

之前(不适用于 VS 2017):

//! Forces dynamic initialization of polymorphic support in a
//! previously registered source file
/*! @sa CEREAL_REGISTER_DYNAMIC_INIT

    See CEREAL_REGISTER_DYNAMIC_INIT for detailed explanation
    of how this macro should be used.  The name used should
    match that for CEREAL_REGISTER_DYNAMIC_INIT. */
#define CEREAL_FORCE_DYNAMIC_INIT(LibName)              \
  namespace cereal {                                    \
  namespace detail {                                    \
    void dynamic_init_dummy_##LibName();                \
  } /* end detail */                                    \
  namespace {                                           \
    void dynamic_init_##LibName()                       \
    {                                                   \
      ::cereal::detail::dynamic_init_dummy_##LibName(); \
    }                                                   \
  } } /* end namespaces */

之后(固定):

//! Forces dynamic initialization of polymorphic support in a
//! previously registered source file
/*! @sa CEREAL_REGISTER_DYNAMIC_INIT

    See CEREAL_REGISTER_DYNAMIC_INIT for detailed explanation
    of how this macro should be used.  The name used should
    match that for CEREAL_REGISTER_DYNAMIC_INIT. */
#define CEREAL_FORCE_DYNAMIC_INIT(LibName)              \
  namespace cereal {                                    \
  namespace detail {                                    \
    void CEREAL_DLL_EXPORT dynamic_init_dummy_##LibName();                \
  } /* end detail */                                    \
  namespace {                                           \
    void dynamic_init_##LibName()                       \
    {                                                   \
      ::cereal::detail::dynamic_init_dummy_##LibName(); \
    }                                                   \
  } } /* end namespaces */