这个文件是由一个新版本的协议生成的,它与你的协议缓冲区头文件不兼容.请更新您的标题



我想从我的grpc客户端创建一个。so文件,以便我可以从另一个函数运行它。当我添加

一行时
add_library(client_lib client.cc)

在我的CMakeLists.txt结束时,我得到这个错误:虽然没有那行它可以正常工作。

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.
......

我在linux 20.4上使用c++作为我的编程语言这是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;
}

这是我从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,

和also from

protoc --version
libprotoc 3.21.6

add

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

在CMakeLists.txt的末尾,问题解决了。

最新更新