使用VS2019编译protobuf报错“kRepHeaderSize”: 重定义

目的:使用公司提供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的历史版本中我发现

url:src/google/protobuf/repeated_field.h - external/github.com/google/protobuf - Git at Google (googlesource.com)

那我把我对于的进行初始化:

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版本这样是否能在整个工程下成功编译。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值