编译器C2491错误:解决方案需要数据丢失?

时间:2020-10-23 16:27:47

标签: c++ visual-studio dll

使用VS2017,我从此行生成的自动生成的.cpp / .h文件中创建了一个项目(我添加此文件是为了说明如何达到这一点):

protoc -I=$SRC_DIR --cpp_out=dllexport_decl=MY_EXPORT_MACRO:$DST_DIR $SRC_DIR\gameinformation.proto

此行正确生成了文件,没有任何错误或警告,我的问题不涉及文件的生成,而是涉及它们对.dll的编译。我正在专门寻找一种方法来修复后来遇到的错误。

我已经在.h文件中定义了宏(我添加了这个宏,以防它链接到我要处理的错误):

#if BUILD_GAMEINFORMATION
#define MY_EXPORT_MACRO __declspec( dllexport )
#else
#pragma comment( 'lib', "gameinformation.lib" )
#define MY_EXPORT_MACRO __declspec( dllimport )
#endif

现在,生成的代码(.cpp)的一部分如下:

const ::PROTOBUF_NAMESPACE_ID::uint32 TableStruct_gameinformation_2eproto::offsets[] PROTOBUF_SECTION_VARIABLE( protodesc_cold ) = {
    PROTOBUF_FIELD_OFFSET(::GameInfo::GameInformation, _has_bits_ ),
    PROTOBUF_FIELD_OFFSET(::GameInfo::GameInformation, _internal_metadata_ ),
    ~0u, // no _extensions_
    ~0u, // no _oneof_case_
    ~0u, // no _weak_field_map_
    PROTOBUF_FIELD_OFFSET(::GameInfo::GameInformation, gamemessage_ ),
    PROTOBUF_FIELD_OFFSET(::GameInfo::GameInformation, eacmessagebuffer_ ),
    PROTOBUF_FIELD_OFFSET(::GameInfo::GameInformation, eacmessagelength_ ),
    0,
    1,
    2,
};

这是整个项目中对该变量的唯一定义/赋值。

当我尝试编译项目时,这部分代码将引发以下错误:

Error   C2491   'TableStruct_gameinformation_2eproto::offsets': definition of dllimport static data member not allowed

这是我要解决的错误。

我用谷歌搜索,根据Microsoft docs,我需要摆脱价值分配,即只保留这么多:

const ::PROTOBUF_NAMESPACE_ID::uint32 TableStruct_gameinformation_2eproto::offsets[] PROTOBUF_SECTION_VARIABLE( protodesc_cold )

对我来说,这似乎不是一个可靠的解决方案。该文件是自动生成的,我很确定那些值很重要。

如果我必须采用Microsoft解决方案,那么丢失这些值是否重要?还有其他我可以放的地方吗? 还是有其他选择?

0 个答案:

没有答案