CMake使用gRPC(Protobuf) 的c++ demo

gRPC的命令参数里,

1. 如果要用pacakge,需要--proto_path的参数,
例如helloworld.proto的绝对路径是
/home/user/grpc_demo_ws/grpc_demo/hello_world/proto/helloworld.proto
在helloworld.proto里面的pacakge是
package grpc_demo.hello_world.proto;
那--proto_path的值就是/home/user/grpc_demo_ws
如果要生成带包名的DescriptorTable,--proto_path也是必须的


2. 如果pb.cc/pb.h/grpc.pb.cc/grpc.pb.h想要和proto放在同一个目录
--grpc_out 和 --cpp_out 也都是填入/home/user/grpc_demo_ws
最后实际生成的位置是加上package路径的/home/user/grpc_demo_ws/grpc_demo/hello_world/proto/

下载代码

https://download.csdn.net/download/howard789/87966195

client向server发送一条数据包含自己的名字(helloworld.proto),还有另外两个proto的数据结构(catInfo.proto和dogInfo.proto),server收到后,转成一条string发回给client

目录结构

CMakeLists.txt

cmake_minimum_required(VERSION 3.19)
project(grpc_demo)set(CMAKE_CXX_STANDARD 17)
# 加载 gRPC ---------------------------------
include(cmake/common.cmake)
MESSAGE("LIB _REFLECTION :" ${_REFLECTION})
MESSAGE("LIB _GRPC_GRPCPP:" ${_GRPC_GRPCPP})
MESSAGE("LIB _PROTOBUF_LIBPROTOBUF:" ${_PROTOBUF_LIBPROTOBUF})
include_directories(/home/hao/.local/include)
# --------------------------------------------
include_directories(${CMAKE_SOURCE_DIR})
include_directories(${CMAKE_SOURCE_DIR}/grpc_demo)#---------------------------------------------------------------------
set(PROTO_WS_DIR ${CMAKE_SOURCE_DIR})
MESSAGE("PROTO_WS_DIR:" ${PROTO_WS_DIR})##  Cat_PROTO--------------------------------------------
set(PROTO_ABSOLUTE_PATH ${PROTO_WS_DIR}/grpc_demo/cat/proto/cat.proto)
set(Regenerate_Proto ON)
include(cmake/common_generate_files.cmake)##  Dog_PROTO--------------------------------------------
set(PROTO_ABSOLUTE_PATH ${PROTO_WS_DIR}/grpc_demo/dog/proto/dog.proto)
set(Regenerate_Proto ON)
include(cmake/common_generate_files.cmake)##  HelloWorld_PROTO--------------------------------------------
set(PROTO_ABSOLUTE_PATH ${PROTO_WS_DIR}/grpc_demo/hello_world/proto/helloworld.proto)
set(Regenerate_Proto ON)
include(cmake/common_generate_files.cmake)find_package(Threads REQUIRED)add_executable(greeter_client grpc_demo/hello_world/greeter_client.cc)
target_link_libraries(greeter_clienthelloworld_grpc_protodog_grpc_protocat_grpc_protoabsl::flagsabsl::flags_parse${_REFLECTION}${_GRPC_GRPCPP}${_PROTOBUF_LIBPROTOBUF})add_executable(greeter_server grpc_demo/hello_world/greeter_server.cc)
target_link_libraries(greeter_serverhelloworld_grpc_protodog_grpc_protocat_grpc_protoabsl::flagsabsl::flags_parse${_REFLECTION}${_GRPC_GRPCPP}${_PROTOBUF_LIBPROTOBUF})

common.cmake

# Copyright 2018 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# cmake build file for C++ route_guide example.
# Assumes protobuf and gRPC have been installed using cmake.
# See cmake_externalproject/CMakeLists.txt for all-in-one cmake build
# that automatically builds all the dependencies before building route_guide.cmake_minimum_required(VERSION 3.8)if(MSVC)add_definitions(-D_WIN32_WINNT=0x600)
endif()find_package(Threads REQUIRED)if(GRPC_AS_SUBMODULE)# One way to build a projects that uses gRPC is to just include the# entire gRPC project tree via "add_subdirectory".# This approach is very simple to use, but the are some potential# disadvantages:# * it includes gRPC's CMakeLists.txt directly into your build script#   without and that can make gRPC's internal setting interfere with your#   own build.# * depending on what's installed on your system, the contents of submodules#   in gRPC's third_party/* might need to be available (and there might be#   additional prerequisites required to build them). Consider using#   the gRPC_*_PROVIDER options to fine-tune the expected behavior.## A more robust approach to add dependency on gRPC is using# cmake's ExternalProject_Add (see cmake_externalproject/CMakeLists.txt).# Include the gRPC's cmake build (normally grpc source code would live# in a git submodule called "third_party/grpc", but this example lives in# the same repository as gRPC sources, so we just look a few directories up)add_subdirectory(../../.. ${CMAKE_CURRENT_BINARY_DIR}/grpc EXCLUDE_FROM_ALL)message(STATUS "Using gRPC via add_subdirectory.")# After using add_subdirectory, we can now use the grpc targets directly from# this build.set(_PROTOBUF_LIBPROTOBUF libprotobuf)set(_REFLECTION grpc++_reflection)if(CMAKE_CROSSCOMPILING)find_program(_PROTOBUF_PROTOC protoc)else()set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)endif()set(_GRPC_GRPCPP grpc++)if(CMAKE_CROSSCOMPILING)find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)else()set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)endif()
elseif(GRPC_FETCHCONTENT)# Another way is to use CMake's FetchContent module to clone gRPC at# configure time. This makes gRPC's source code available to your project,# similar to a git submodule.message(STATUS "Using gRPC via add_subdirectory (FetchContent).")include(FetchContent)FetchContent_Declare(grpcGIT_REPOSITORY https://github.com/grpc/grpc.git# when using gRPC, you will actually set this to an existing tag, such as# v1.25.0, v1.26.0 etc..# For the purpose of testing, we override the tag used to the commit# that's currently under test.GIT_TAG        vGRPC_TAG_VERSION_OF_YOUR_CHOICE)FetchContent_MakeAvailable(grpc)# Since FetchContent uses add_subdirectory under the hood, we can use# the grpc targets directly from this build.set(_PROTOBUF_LIBPROTOBUF libprotobuf)set(_REFLECTION grpc++_reflection)set(_PROTOBUF_PROTOC $<TARGET_FILE:protoc>)set(_GRPC_GRPCPP grpc++)if(CMAKE_CROSSCOMPILING)find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)else()set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)endif()
else()# This branch assumes that gRPC and all its dependencies are already installed# on this system, so they can be located by find_package().message(STATUS "gRPC and all its dependencies should  be able to located by find_package().")# Find Protobuf installation# Looks for protobuf-config.cmake file installed by Protobuf's cmake installation.option(protobuf_MODULE_COMPATIBLE TRUE)find_package(Protobuf CONFIG REQUIRED)message(STATUS "Using protobuf ${Protobuf_VERSION}")set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)set(_REFLECTION gRPC::grpc++_reflection)message(STATUS "CMAKE_CROSSCOMPILING: ${CMAKE_CROSSCOMPILING}")if(CMAKE_CROSSCOMPILING)find_program(_PROTOBUF_PROTOC protoc)else()set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)endif()# Find gRPC installation# Looks for gRPCConfig.cmake file installed by gRPC's cmake installation.find_package(gRPC CONFIG REQUIRED)message(STATUS "Using gRPC ${gRPC_VERSION}")set(_GRPC_GRPCPP gRPC::grpc++)if(CMAKE_CROSSCOMPILING)find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)else()set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:gRPC::grpc_cpp_plugin>)endif()endif()

common_generate_files.cmake

#需要的参数
#PROTO_WS_DIR #工作目录的路径,就是proto档案的package上一级目录。这里是/home/user/grpc_demo_ws
#PROTO_ABSOLUTE_PATH #proto档案的绝对路径 例如/home/user/grpc_demo_ws/grpc_demo/hello_world/proto/helloworld.proto
#Regenerate_Proto #是否重新生成档案 pb.cc pb.hget_filename_component(PROTO_NAME "${PROTO_ABSOLUTE_PATH}" NAME_WE)
get_filename_component(PROTO_SOURCE_DIR "${PROTO_ABSOLUTE_PATH}" PATH)
file(RELATIVE_PATH PROTO_PKG_DIR ${PROTO_WS_DIR} ${PROTO_SOURCE_DIR})# Proto file
MESSAGE("PROTO_ABSOLUTE_PATH:" ${PROTO_ABSOLUTE_PATH})
MESSAGE("PROTO_WS_DIR:" ${PROTO_WS_DIR})
MESSAGE("PROTO_PKG_DIR:" ${PROTO_PKG_DIR})
MESSAGE("PROTO_NAME:" ${PROTO_NAME})# Generated sources
set(_proto_srcs "${PROTO_WS_DIR}/${PROTO_PKG_DIR}/${PROTO_NAME}.pb.cc") #输出档案的绝对路径
set(_proto_hdrs "${PROTO_WS_DIR}/${PROTO_PKG_DIR}/${PROTO_NAME}.pb.h")
set(_grpc_srcs "${PROTO_WS_DIR}/${PROTO_PKG_DIR}/${PROTO_NAME}.grpc.pb.cc")
set(_grpc_hdrs "${PROTO_WS_DIR}/${PROTO_PKG_DIR}/${PROTO_NAME}.grpc.pb.h")if(Regenerate_Proto)MESSAGE("Regenerate_Proto" ${PROTO_NAME}  " = ON")add_custom_command(OUTPUT "${_proto_srcs}" "${_proto_hdrs}" "${_grpc_srcs}" "${_grpc_hdrs}"   #输出档案的绝对路径COMMAND ${_PROTOBUF_PROTOC}ARGS--proto_path "${PROTO_WS_DIR}"--grpc_out "${PROTO_WS_DIR}"--cpp_out "${PROTO_WS_DIR}"-I "${PROTO_WS_DIR}/${PROTO_PKG_DIR}"--plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}""${PROTO_ABSOLUTE_PATH}"DEPENDS "${PROTO_ABSOLUTE_PATH}")
endif()# Include generated *.pb.h files
include_directories("${PROTO_WS_DIR}/${PROTO_PKG_DIR}")set(LIB_NAME ${PROTO_NAME}_grpc_proto)
MESSAGE(" ********************************* ${PROTO_NAME} 生成的 LIB_NAME名称 = ${LIB_NAME}")
# hw_grpc_proto
add_library(${LIB_NAME}${_grpc_srcs}${_grpc_hdrs}${_proto_srcs}${_proto_hdrs})
target_link_libraries(${LIB_NAME}${_REFLECTION}${_GRPC_GRPCPP}${_PROTOBUF_LIBPROTOBUF})

cat.proto

syntax = "proto3";package grpc_demo.cat.proto;message CatInfo{string name = 1;bool isMale = 2;
}

dog.proto


syntax = "proto3";package grpc_demo.dog.proto;message DogInfo{string name = 1;int32 age = 2;
}

helloworld.proto

// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.syntax = "proto3";option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";package grpc_demo.hello_world.proto;import "grpc_demo/cat/proto/cat.proto";
import "grpc_demo/dog/proto/dog.proto";// The greeting service definition.
service Greeter {// Sends a greetingrpc SayHello (HelloRequest) returns (HelloReply) {}rpc SayHelloStreamReply (HelloRequest) returns (stream HelloReply) {}
}// The request message containing the user's name.
message HelloRequest {string name = 1;grpc_demo.cat.proto.CatInfo catInfo=2;grpc_demo.dog.proto.DogInfo dogInfo=3;}// The response message containing the greetings
message HelloReply {string message = 1;}

greeter_client.cc


#include <iostream>
#include <memory>
#include <string>
#include "absl/flags/flag.h"
#include "absl/flags/parse.h"
#include <grpcpp/grpcpp.h>
#include "helloworld.grpc.pb.h"ABSL_FLAG(std::string, target, "localhost:50051", "Server address");using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using grpc_demo::hello_world::proto::Greeter;
using grpc_demo::hello_world::proto::HelloReply;
using grpc_demo::hello_world::proto::HelloRequest;class GreeterClient {public:GreeterClient(std::shared_ptr<Channel> channel): stub_(Greeter::NewStub(channel)) {}// Assembles the client's payload, sends it and presents the response back// from the server.std::string SayHello(const std::string& user,grpc_demo::cat::proto::CatInfo* catinfo,grpc_demo::dog::proto::DogInfo* doginfo) {// Data we are sending to the server.HelloRequest request;request.set_name(user);request.set_allocated_catinfo(catinfo);request.set_allocated_doginfo(doginfo);// Container for the data we expect from the server.HelloReply reply;// Context for the client. It could be used to convey extra information to// the server and/or tweak certain RPC behaviors.ClientContext context;// The actual RPC.Status status = stub_->SayHello(&context, request, &reply);// Act upon its status.if (status.ok()) {return reply.message();} else {std::cout << status.error_code() << ": " << status.error_message()<< std::endl;return "RPC failed";}}private:std::unique_ptr<Greeter::Stub> stub_;
};int main(int argc, char** argv) {absl::ParseCommandLine(argc, argv);// Instantiate the client. It requires a channel, out of which the actual RPCs// are created. This channel models a connection to an endpoint specified by// the argument "--target=" which is the only expected argument.std::string target_str = absl::GetFlag(FLAGS_target);// We indicate that the channel isn't authenticated (use of// InsecureChannelCredentials()).GreeterClient greeter(grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials()));std::string user("铲屎官John");grpc_demo::cat::proto::CatInfo* catinfo=new grpc_demo::cat::proto::CatInfo();catinfo->set_ismale(false);catinfo->set_name("mimi");grpc_demo::dog::proto::DogInfo* doginfo=new grpc_demo::dog::proto::DogInfo();doginfo->set_name("happy");doginfo->set_age(12);std::string reply = greeter.SayHello(user,catinfo,doginfo);std::cout << "client收到server回复: " << reply << std::endl;return 0;
}

greeter_server.cc

/*** Copyright 2015 gRPC authors.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**     http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.**/#include <iostream>
#include <memory>
#include <string>#include "absl/flags/flag.h"
#include "absl/flags/parse.h"
#include "absl/strings/str_format.h"#include <grpcpp/ext/proto_server_reflection_plugin.h>
#include <grpcpp/grpcpp.h>
#include <grpcpp/health_check_service_interface.h>#ifdef BAZEL_BUILD
#include "examples/protos/helloworld.grpc.pb.h"
#else
#include "helloworld.grpc.pb.h"
#endifusing grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using grpc_demo::hello_world::proto::Greeter;
using grpc_demo::hello_world::proto::HelloReply;
using grpc_demo::hello_world::proto::HelloRequest;ABSL_FLAG(uint16_t, port, 50051, "Server port for the service");// Logic and data behind the server's behavior.
class GreeterServiceImpl final : public Greeter::Service {Status SayHello(ServerContext* context, const HelloRequest* request,HelloReply* reply) override {std::cout << "收到client的消息 "<< std::endl;std::string prefix("你好 ");reply->set_message(prefix + request->name()+",Server收到 CatInfo:"+ request->catinfo().name()+"," + std::to_string(request->catinfo().ismale())+",DogInfo:"+ request->doginfo().name()+"," + std::to_string(request->doginfo().age()));return Status::OK;}
};void RunServer(uint16_t port) {std::string server_address = absl::StrFormat("0.0.0.0:%d", port);GreeterServiceImpl service;grpc::EnableDefaultHealthCheckService(true);grpc::reflection::InitProtoReflectionServerBuilderPlugin();ServerBuilder builder;// Listen on the given address without any authentication mechanism.builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());// Register "service" as the instance through which we'll communicate with// clients. In this case it corresponds to an *synchronous* service.builder.RegisterService(&service);// Finally assemble the server.std::unique_ptr<Server> server(builder.BuildAndStart());std::cout << "Server listening on " << server_address << std::endl;// Wait for the server to shutdown. Note that some other thread must be// responsible for shutting down the server for this call to ever return.server->Wait();
}int main(int argc, char** argv) {absl::ParseCommandLine(argc, argv);RunServer(absl::GetFlag(FLAGS_port));return 0;
}

其余*.pb.cc *.pb.h 是用cmake生成的

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/4754.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

模仿QQ之右键菜单

参考&#xff1a;QT多级菜单 - 知乎 (zhihu.com) 运行效果图&#xff1a; 关键代码&#xff1a; void personMenu::contextMenuEvent(QContextMenuEvent *event) {//我完全可以写出一个代码生成器来把这些代码生成出来。parentnew QMenu(this);parent->addAction(QIcon(…

【玩转循环】探索Python中的无限可能性

前言 循环可能是每个编程语言中使用比较多的语法了&#xff0c;如果能合理利用好循环&#xff0c;就会出现意想不到的结果&#xff0c;大大地减少代码量&#xff0c;让机器做那些简单枯燥的循环过程&#xff0c;今天我将为大家分享 python 中的循环语法使用。&#x1f697;&am…

【NOSQL】redis哨兵模式、集群搭建

目录 一、redis高可用一、Redis主从复制1.1主从复制的作用1.2主从复制流程 二、搭建Rdeis主从复制2.1安装redis2.1.1环境准备2.1.2安装redis2.1.3设置环境变量2.1.4定义systemd服务管理脚本 2.2修改 Redis 配置文件&#xff08;Master节点操作2.3修改 Redis 配置文件&#xff0…

SpringBoot项目-双人对战五子棋实验报告

简单五子棋Web项目报告 课 程 Web应用程序设计 项目名称 简单双人五子棋对战 成绩 专业班级 XXX 组别 无 学号 XXX 指导教师 XXX 姓 名 XXX 同组人姓名 无 完成日期 XXX 功能描述 1.用户的注册及登录功能 玩家可以在完成游戏账户的注册&#xff0c…

CentOS Stream 10 计划已启动,即将进入初始化阶段

导读 但初始化的 CentOS Stream 10 暂时不会对外开放&#xff0c;也不会有太多活跃信息。从 Fedora 周期的规律来看&#xff0c;至少还有六个月的时间&#xff0c;CentOS Stream 10 才会正式启动开发流程&#xff0c;接受来自外部的贡献。周五的公告指出&#xff1a;你会看到 C…

STM32文档

一、寄存器缩写 二、存储器和总线构架 DMA&#xff0c;全称为&#xff1a;Direct Memory Access&#xff0c;即直接存储器访问 简而言之&#xff0c;DMA就是将一个内存里的数据搬运到另一个内存里&#xff0c;此过程无需CPU直接控制输出 系统架构存储器组织存储器映射&#xf…

TypeScript 【type】关键字的进阶使用方式

导语&#xff1a; 在前面章节中&#xff0c;我们了解到 TS 中 type 这个关键字&#xff0c;常常被用作于&#xff0c;定义 类型别名&#xff0c;用来简化或复用复杂联合类型的时候使用。同时也了解到 为对象定义约束接口类型 的时候所使用的是 Interfaces。 其实对于前面&#…

从架构角度看网络安全:数字化时代企业如何构建防御体系?

导语 | 数字化时代&#xff0c;网络安全已经成为企业发展的重中之重&#xff0c;通过体系化的安全建设&#xff0c;企业可以从容应对愈加复杂的网络安全挑战。今天&#xff0c;我们特邀了腾讯云 TVP、赛博英杰科技董事长 谭晓生老师&#xff0c;他将从资深技术专家视角解读如何…

【软考网络管理员】2023年软考网管初级常见知识考点(22)- 交换机配置及命令

涉及知识点 华为交换机配置命令大全&#xff0c;交换机设备参数设置 &#xff0c; 交换机路由器的基本配置&#xff0c;交换机命令汇总 &#xff0c; VLAN 配置 &#xff0c; GVRP 配置 &#xff0c; STP 配置&#xff0c;软考网络管理员常考知识点&#xff0c;软考网络管理员…

STM32外设系列—BH1750

文章目录 一、BH1750简介二、BH1750原理图三、BH1750数据手册3.1 指令集3.2 IIC通信读/写 四、BH1750程序设计4.1 IIC程序4.2 BH1750初始化程序4.3 读取BH1750测量结果4.4 获取光照强度4.5 相关宏定义 五、应用实例六、拓展应用6.1 实时调节LED亮度6.2 实时调整颜色阈值 一、BH…

【C#】云LIS检验医学信息系统源码

系统基于云计算为医院机构改善患者服务质量提供了强有力的支持&#xff0c;“以患者为核心”这一理念得到了充分实现&#xff0c;基于云计算的高效区域医疗信息服务&#xff0c;会为医疗行业带来跨时代的变革。 一、使用技术框架说明 开发语言&#xff1a;C# 6.0、JavaScript …

css 背景颜色级别高于背景图

<div class"bg-parent"> <img :src"employeeImg" class"bg-url" /> <div class"bg"> <el-icon class"plus-icon"> <Plus /> </el-icon> </div> </div> .bg-parent{ //父级…