先贴 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}/pkgconfigbigendian: 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()。
然后编译,就没问题了!