protobuf-c编译错误

先贴 protobuf-c的源码地址GitHub - protobuf-c/protobuf-c: Protocol Buffers implementation in C

其他编译步骤参考官方说明,我的编译的出错信息如下:

protoc-c/c_message.cc:157:65: error: ‘const class google::protobuf::OneofDescriptor’ has no member named ‘file’
  157 |     vars["foneofname"] = FullNameToC(oneof->full_name(), oneof->file());
      |                                                                 ^~~~
protoc-c/c_message.cc:208:65: error: ‘const class google::protobuf::OneofDescriptor’ has no member named ‘file’
  208 |     vars["foneofname"] = FullNameToC(oneof->full_name(), oneof->file());
      |                                                                 ^~~~
protoc-c/c_message.cc:248:69: error: ‘const class google::protobuf::OneofDescriptor’ has no member named ‘file’
  248 |     vars["foneofname"] = FullNameToUpper(oneof->full_name(), oneof->file());
      |                                                                     ^~~~
 

我的编译环境是

protobuf-c 1.4.0

        CC:                     gcc
        CFLAGS:                 -fPIC
        CXX:                    g++ -std=c++11
        CXXFLAGS:               -g -O2
        LDFLAGS:                
        LIBS:                   

        prefix:                 /home/xxxxx/protobuf_c_x86
        sysconfdir:             ${prefix}/etc
        libdir:                 ${exec_prefix}/lib
        includedir:             ${prefix}/include
        pkgconfigdir:           ${libdir}/pkgconfig

        bigendian:              no
        protobuf version:       libprotoc 3.3.0
 

今天编译protobuf-c,出现以上错误,按照官方的安装,包括protobuf,但是还是出这个错误,以为是我配置的问题,没有找到原因。按照错误提示去源码中查找

此处的调用是调用到了protobuf源码中的descriptor.h(这个文件在哪请自行find)中的OneofDescriptor类,如下图源码中的片段

// Describes a oneof defined in a message type.
class LIBPROTOBUF_EXPORT OneofDescriptor {
 public:
  const string& name() const;       // Name of this oneof.
  const string& full_name() const;  // Fully-qualified name of the oneof.

  // Index of this oneof within the message's oneof array.
  int index() const;

  // The Descriptor for the message containing this oneof.
  const Descriptor* containing_type() const;

  // The number of (non-extension) fields which are members of this oneof.
  int field_count() const;
  // Get a member of this oneof, in the order in which they were declared in the
  // .proto file.  Does not include extensions.
  const FieldDescriptor* field(int index) const;

  const OneofOptions& options() const;

  // See Descriptor::CopyTo().
  void CopyTo(OneofDescriptorProto* proto) const;

  // See Descriptor::DebugString().
  string DebugString() const;

  // See Descriptor::DebugStringWithOptions().
  string DebugStringWithOptions(const DebugStringOptions& options) const;

提示的file()方法在这个OneofDescriptor 中没有,其他相关Descriptor类中有这个方法,返回去再看下protobuf-c源码中出错函数的定义。它需要的是一个

const FileDescriptor *file

这个参数,ok问题找到了,那就是返回一个FileDescriptor *就行了。再看OneofDescriptor 的定义。哦豁,没有,接着看。

发现containing_type()成员函数的返回值是Descriptor,去看下定义

class LIBPROTOBUF_EXPORT Descriptor {
 public:
  // The name of the message type, not including its scope.
  const string& name() const;

  // The fully-qualified name of the message type, scope delimited by
  // periods.  For example, message type "Foo" which is declared in package
  // "bar" has full name "bar.Foo".  If a type "Baz" is nested within
  // Foo, Baz's full_name is "bar.Foo.Baz".  To get only the part that
  // comes after the last '.', use name().
  const string& full_name() const;

  // Index of this descriptor within the file or containing type's message
  // type array.
  int index() const;

  // The .proto file in which this message type was defined.  Never NULL.
  const FileDescriptor* file() const;

他有file()函数的定义,且函数的返回值类型是FileDescriptor*,这不就对了么!

下面就是修改前后对比

左边是源码,右边是我修改后的。

oneof->file() 改成oneof->containing_type()->file()。

然后编译,就没问题了!

  • 17
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值