在谷物库中正确使用CEREAL_REGISTER_DYNAMIC_INIT C++问题

Issue correctly using CEREAL_REGISTER_DYNAMIC_INIT in C++ Cereal library

本文关键字:REGISTER CEREAL DYNAMIC INIT 问题 C++      更新时间:2023-10-16

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

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

CEREAL_FORCE_DYNAMIC_INIT(mv_clientactions);

在帐户操作中.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命名,但认为它是用来将这两个单元链接在一起的吗?文档很少,我可能使用不正确。

我得到的错误是这样的:

1>c:\git\bindstone\source\gameetworklayer\accountactions.cpp(13(:错误 C2084:函数"void Cereal::d etail::d ynamic_init_dummy_mv_accountactions(void("已经有一个主体 1>c:\git\bindstone\source\gameetworklayer\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 */