ubuntu安装muduo出现ByteSize问题和解决方法

1.2023年安装muduo,出现ByteSize错误

安装muduo的时候,执行./build.sh指令,发现编译到了85%的时候,提示如下错误:

 error: ‘int google::protobuf::MessageLite::ByteSize() const’ is deprecated: Please use ByteSizeLong() instead [-Werror=deprecated-declarations]
  115 |   int byte_size = message.ByteSize();
      |                   ~~~~~~~~~~~~~~~~^~
In file included from /usr/local/include/google/protobuf/generated_enum_util.h:38,
                 from /usr/local/include/google/protobuf/generated_enum_reflection.h:47,
                 from /usr/local/include/google/protobuf/generated_message_reflection.h:49,
                 from /usr/local/include/google/protobuf/message.h:124,
                 from /home/ubuntu/mypacket/muduo-master/muduo/net/protorpc/google-inl.h:39,
                 from /home/ubuntu/mypacket/muduo-master/muduo/net/protobuf/ProtobufCodecLite.cc:15:
/usr/local/include/google/protobuf/message_lite.h:382:7: note: declared here
  382 |   int ByteSize() const { return internal::ToIntSize(ByteSizeLong()); }
      |       ^~~~~~~~
/home/ubuntu/mypacket/muduo-master/muduo/net/protobuf/ProtobufCodecLite.cc:122:57: error: ‘int google::protobuf::MessageLite::ByteSize() const’ is deprecated: Please use ByteSizeLong() instead [-Werror=deprecated-declarations]
  122 |     ByteSizeConsistencyError(byte_size, message.ByteSize(), static_cast<int>(end - start));
      |                                         ~~~~~~~~~~~~~~~~^~
In file included from /usr/local/include/google/protobuf/generated_enum_util.h:38,
                 from /usr/local/include/google/protobuf/generated_enum_reflection.h:47,
                 from /usr/local/include/google/protobuf/generated_message_reflection.h:49,
                 from /usr/local/include/google/protobuf/message.h:124,
                 from /home/ubuntu/mypacket/muduo-master/muduo/net/protorpc/google-inl.h:39,
                 from /home/ubuntu/mypacket/muduo-master/muduo/net/protobuf/ProtobufCodecLite.cc:15:
/usr/local/include/google/protobuf/message_lite.h:382:7: note: declared here
  382 |   int ByteSize() const { return internal::ToIntSize(ByteSizeLong()); }
      |       ^~~~~~~~

 2. 错误的解决方法

我以为是boost和muduo版本不兼容,需要重新安装,我把它们全部卸载了,反复安装,发现一直报这个错误。

我检查错误信息,里面提示说是在muduo的/home/ubuntu/mypacket/muduo-master/muduo/net/protobuf/ProtobufCodecLite.cc源文件中使用ByteSize方法,而ProtobufCondecLite.cc引入了google/protobuf/message.h头文件,但是ByteSize方法在google/protobuf/message.h这个头文件中找不到,message.h中已经把ByteSize方法抛弃了,建议使用ByteSizeLong方法,而且还提示了新版的protobuf把ByteSize方法移植到了message_lite.h中。

我直呼好家伙,然后我把/home/ubuntu/mypacket/muduo-master/muduo/net/protobuf/ProtobufCodecLite.cc中引入的google/protobuf/message.h注释掉,并且改成google/protobuf/message.lite.h,重新执行./build.sh命令,发现还是会报错,错误信息还是如上所示。

注意:/home/ubuntu/mypacket/muduo-master/是我在ubuntu中解压的muduo的文件夹。

3. 正确的解决方法

上面的错误信息不是说,建议使用ByteSizeLong方法吗。

所以我将第2步引入的message_lite.h删掉,重新使用message.h,然后将home/ubuntu/mypacket/muduo-master/muduo/net/protobuf/ProtobufCodecLite.cc中使用到的两处ByteSize改成ByteSizeLong。

这会引发一个问题,就是后续使用到的ByteSize返回值,变成了ByteSizeLong返回值,前者是int类型,后者是size_t(long unsigned int)类型,精度不匹配,由小精度变成大精度,后续指针读取字节时会产生越界问题,所以将ByteSize改成ByteSizeLong之后,需要强制类型转换。怎么强制类型转换呢?使用static_cast,因为这个强制类型转换就是用于基类类型相互转换,用它最合适不过了,当然了使用C的强制类型转换也可以。

具体为:

//115行
int byte_size = static_cast<int>(message.ByteSizeLong());

 

//123行
ByteSizeConsistencyError(byte_size, static_cast<int>(message.ByteSizeLong()), static_cast<int>(end - start));

 修改成功之后,重新执行./build.sh,即可编译成功。

4. 发生错误原因

我想是muduo和protobuf版本不匹配问题,muduo的net下的protobuf更新日期是3年前,但是第三方库protobuf还在不断更新,其调整过源码结构和方法,导致muduo使用新版的protobuf的时候,会因为protobuf的微调导致编译失败。如果发生编译问题,按照错误信息提示,静下心来,还是可以解决的。

5. 安装muduo的具体步骤

这个步骤我直接参考别人的,在编译过程中,如果发生如上的编译错误,按照我的步骤,即可圆满解决。

 安装muduo参考链接:

https://blog.csdn.net/QIANGWEIYUAN/article/details/89023980

 如果觉得文章不错,请帮我点赞!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值