sama
sama

Reputation: 341

This file was generated by a newer version of protoc which is incompatible with your Protocol Buffer headers. Please update your headers

I want to create a .so file from my grpc client so that I could run it from another function. when I add the line

add_library(client_lib client.cc)

at the end of my CMakeLists.txt I get this error: while it is working correctly without that line.

cmake/build/stringreverse.grpc.pb.h:7,
             from /home/client.cc:3:
/cmake/build/stringreverse.pb.h:12:2: error: #error This file was generated by a 
 newer version of protoc which is
12 | #error This file was generated by a newer version of protoc which is
  /cmake/build/stringreverse.pb.h:13:2: error: #error incompatible with your Protocol 
 Buffer headers. Please update
13 | #error incompatible with your Protocol Buffer headers. Please update
  |   /cmake/build/stringreverse.pb.h:14:2: error: #error your headers.
  ......

I am using c++ as my programming language on linux 20.4 this is cmakeLists.txt:

# Minimum CMake required
 cmake_minimum_required(VERSION 3.15)

 # Project
 project(stringreverse)

 # Protobuf
 set(protobuf_MODULE_COMPATIBLE TRUE)
find_package(Protobuf CONFIG REQUIRED)
message(STATUS "Using protobuf ${protobuf_VERSION}")

 # Protobuf-compiler
 set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)

# gRPC
find_package(gRPC CONFIG REQUIRED)
message(STATUS "Using gRPC ${gRPC_VERSION}")
set(_GRPC_GRPCPP gRPC::grpc++)
set(_GRPC_CPP_PLUGIN_EXECUTABLE 
$<TARGET_FILE:gRPC::grpc_cpp_plugin>)

# Proto file
get_filename_component(hw_proto "stringreverse.proto" ABSOLUTE)
get_filename_component(hw_proto_path "${hw_proto}" PATH)

# Generated sources
set(hw_proto_srcs 
"${CMAKE_CURRENT_BINARY_DIR}/stringreverse.pb.cc")
set(hw_proto_hdrs 
"${CMAKE_CURRENT_BINARY_DIR}/stringreverse.pb.h")
 set(hw_grpc_srcs 
 "${CMAKE_CURRENT_BINARY_DIR}/stringreverse.grpc.pb.cc")
 set(hw_grpc_hdrs 
"${CMAKE_CURRENT_BINARY_DIR}/stringreverse.grpc.pb.h")
add_custom_command(
    OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" 
 "${hw_grpc_srcs}" 
 "${hw_grpc_hdrs}"
  COMMAND ${_PROTOBUF_PROTOC}
  ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}"
    --cpp_out "${CMAKE_CURRENT_BINARY_DIR}"
    -I "${hw_proto_path}"
    --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
    "${hw_proto}"
  DEPENDS "${hw_proto}")

# Include generated *.pb.h files
include_directories("${CMAKE_CURRENT_BINARY_DIR}")
include_directories("/home/sama/grpc/include")

# Targets (client|server)
foreach(_target
  client server)
  add_executable(${_target} "${_target}.cc"
    ${hw_proto_srcs}
    ${hw_grpc_srcs})
  target_link_libraries(${_target}
    ${_REFLECTION}
    ${_GRPC_GRPCPP}
    ${_PROTOBUF_LIBPROTOBUF})
endforeach()
 add_library(client_lib client.cc)

Client.cc:

#include <grpcpp/grpcpp.h>
#include <string>
#include "stringreverse.grpc.pb.h"

using grpc::Channel;
using grpc::ClientContext;
 using grpc::Status;

 using stringreverse::StringReply;
 using stringreverse::StringRequest;
 using stringreverse::StringReverse;
 class StringReverseClient {
     public:
        StringReverseClient(std::shared_ptr<Channel> channel)
         : stub_(StringReverse::NewStub(channel)) {}

  // Assembles client payload, sends it to the server, and 
  returns 
 its response
 std::string sendRequest(std::string a) {
// Data to be sent to server
StringRequest request;
request.set_original(a);

// Container for server response
StringReply reply;

// Context can be used to send meta data to server or modify RPC 
  behaviour
ClientContext context;

// Actual Remote Procedure Call
Status status = stub_->sendRequest(&context, request, &reply);

// Returns results based on RPC status
if (status.ok()) {
  return reply.reversed();
} else {
  std::cout << status.error_code() << ": " << 
status.error_message()
            << std::endl;
  return "RPC Failed";
  }
}

private:
  std::unique_ptr<StringReverse::Stub> stub_;
};

void RunClient() {
  std::string target_address("0.0.0.0:50051");
   // Instantiates the client
    StringReverseClient client(
  // Channel from which RPCs are made - endpoint is the 
target_address
  grpc::CreateChannel(target_address,
                      // Indicate when channel is not 
authenticated
                      grpc::InsecureChannelCredentials()));

  std::string response;
  std::string a = "grpc is cool!";

  // RPC is created and response is stored
  response = client.sendRequest(a);

  // Prints results
  std::cout << "Original string: " << a << std::endl;
  std::cout << "Reversed string: " << response << std::endl;
}

int main(int argc, char* argv[]) {
 RunClient();

 return 0;
 }

this what I get from grep

grep GOOGLE_PROTOBUF_VERSION /usr/include/google/protobuf/stubs/common.h 
#define GOOGLE_PROTOBUF_VERSION 3012004
#define GOOGLE_PROTOBUF_VERSION_SUFFIX ""
GOOGLE_PROTOBUF_VERSION, GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION,

and also from

protoc --version
libprotoc 3.21.6

Upvotes: 2

Views: 2639

Answers (1)

sama
sama

Reputation: 341

add

target_link_libraries(client_lib
    ${_REFLECTION}
    ${_GRPC_GRPCPP}
    ${_PROTOBUF_LIBPROTOBUF})

at the end of CMakeLists.txt and problem solved.

Upvotes: 1

Related Questions