protobuf 报错解决: error: #error This file was generated by an older version of protoc which is

此文首发于我的Jekyll博客:zhang0peter的个人博客


早上在用protobufgrpc时遇到了报错:

g++ -Wno-expansion-to-defined -Wno-implicit-fallthrough -std=c++11 `pkg-config --cflags protobuf grpc`  -c -o helloworld.grpc.pb.o helloworld.grpc.pb.cc
In file included from helloworld.grpc.pb.cc:5:0:
helloworld.pb.h:17:2: error: #error This file was generated by an older version of protoc which is
 #error This file was generated by an older version of protoc which is
  ^~~~~
helloworld.pb.h:18:2: error: #error incompatible with your Protocol Buffer headers. Please
 #error incompatible with your Protocol Buffer headers.  Please
  ^~~~~
helloworld.pb.h:19:2: error: #error regenerate this file with a newer version of protoc.
 #error regenerate this file with a newer version of protoc.
  ^~~~~
In file included from helloworld.grpc.pb.cc:5:0:
helloworld.pb.h: In member function ‘const string& helloworld::HelloRequest::name() const’:
helloworld.pb.h:230:87: error: no matching function for call to ‘google::protobuf::internal::ArenaStringPtr::GetNoArena(const string*) const’
   return name_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
....
helloworld.grpc.pb.cc:35:127: error: expected primary-expression before ‘const’
 ncResponseReader< ::helloworld::HelloReply>* Greeter::Stub::AsyncSayHelloRaw(::grpc::ClientContext* context, const ::helloworld::HelloRequest& request, ::grpc::CompletionQueue* cq) {
                                                                                                              ^~~~~
helloworld.grpc.pb.cc:35:193: error: expected primary-expression before ‘*’ token
 yncSayHelloRaw(::grpc::ClientContext* context, const ::helloworld::HelloRequest& request, ::grpc::CompletionQueue* cq) {
                                                                                                                  ^
helloworld.grpc.pb.cc:35:195: error: ‘cq’ was not declared in this scope
 yncSayHelloRaw(::grpc::ClientContext* context, const ::helloworld::HelloRequest& request, ::grpc::CompletionQueue* cq) {
                                                                                                                    ^~
helloworld.grpc.pb.cc:35:197: error: expression list treated as compound expression in initializer [-fpermissive]
 yncSayHelloRaw(::grpc::ClientContext* context, const ::helloworld::HelloRequest& request, ::grpc::CompletionQueue* cq) {
                                                                                                                      ^
helloworld.grpc.pb.cc: In constructor ‘helloworld::Greeter::Service::Service()’:
helloworld.grpc.pb.cc:40:17: error: expected type-specifier before ‘::’ token
   AddMethod(new ::grpc::RpcServiceMethod(
                 ^~
<builtin>: recipe for target 'helloworld.grpc.pb.o' failed
make: *** [helloworld.grpc.pb.o] Error 1

这个报错的原因是系统上存在多个版本的protoc

我的系统上安装了2个版本的protoc

一个通过apt安装的:

apt install libprotobuf-dev protobuf-compiler-grpc

另一个是编译源码安装的,版本不同造成了冲突。

解决方法是卸载:

apt remove libprotobuf-dev protobuf-compiler-grpc
apt autoremove

如果还有问题,查看系统中是否存在其他的包产生冲突。

实在解决不了就重装系统。


关于grpcprotobuf的安装和使用,参考这篇博客:grpc与protobuf最佳实践