目的:使用公司提供protobuf进行编译,由于项目依赖的历史版本问题不方便使用新版本的photobuf进行编译。
使用cmake创建win32工程以后,我们进行一个编译发现问题如下
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C2374 “kRepHeaderSize”: 重定义;多次初始化 (编译源文件 F:\share\workstation\ysp\trunk\third_party\protobuf-master\src\google\protobuf\extension_set_heavy.cc) libprotobuf F:\share\workstation\ysp\trunk\third_party\protobuf-master\src\google\protobuf\repeated_field.h 294
错误 C2374 “kRepHeaderSize”: 重定义;多次初始化 (编译源文件 F:\share\workstation\ysp\trunk\third_party\protobuf-master\src\google\protobuf\generated_message_reflection.cc) libprotobuf F:\share\workstation\ysp\trunk\third_party\protobuf-master\src\google\protobuf\repeated_field.h 294
说明“kRepHeaderSize”在该文件内部定义多次,我们对当前文件进行查找发现定义了3次:
那么经过搜索相关问题发现相同的问题也在别人那里出现过
url: Issue with QtWebEngine compilation using VS 2019 | Qt Forum
总结:把相关的const 替换为 constexpr
static constexpr size_t kRepHeaderSize;
template<typename Element>
constexpr size_t RepeatedField<Element>::kRepHeaderSize =
reinterpret_cast<size_t>(&reinterpret_cast<Rep*>(16)->elements[0]) - 16;
static constexpr size_t kRepHeaderSize = sizeof(Rep) - sizeof(void*);
接着继续编译发现报错.
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C2737 “private: static unsigned int const google::protobuf::RepeatedField<int>::kRepHeaderSize”: 必须初始化 constexpr 对象 (编译源文件 F:\share\workstation\ysp\trunk\third_party\protobuf-master\src\google\protobuf\extension_set.cc) libprotobuf F:\share\workstation\ysp\trunk\third_party\protobuf-master\src\google\protobuf\repeated_field.h 253
那么初始化什么值呢?
在protobuf的历史版本中我发现
那我把我对于的进行初始化:
static constexpr size_t kRepHeaderSize = offsetof(Rep, elements);
就能够正常编译通过了
==========================修订1==========================
我当前工程由于photo版本的问题,然后在学习protobuf的过程中,使用上面的版本3.0.0.进行一个demo的测试。发现还是有重定义的问题,但是在公司工程之下没有报错重定义。
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C2374 “google::protobuf::RepeatedField<T>::kRepHeaderSize”: 重定义;多次初始化 myproto F:\share\workstation\ysp\trunk\third_party\protobuf-master\src\google\protobuf\repeated_field.h 294
错误 C2374 “google::protobuf::RepeatedField<T>::kRepHeaderSize”: 重定义;多次初始化 myproto F:\share\workstation\ysp\trunk\third_party\protobuf-master\src\google\protobuf\repeated_field.h 294
但是由于版本问题,我将会将3.0.0的版本切换到3.5.0,所以我这边暂时无法验证,修改的3.0.0版本这样是否能在整个工程下成功编译。