gRPC c++

本文详细介绍了如何安装gRPC和Protocol Buffers,并使用CMake进行项目集成。通过步骤化的指导,读者可以了解到从源码安装到项目配置的全过程,包括依赖项的设置和编译选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备工作

Install grpc

$ git clone -b $(curl -L https://grpc.io/release) https://github.com/grpc/grpc
$ cd grpc
$ git submodule update --init
$ make && sudo make install

Install protobuf

$ cd third_party/protobuf
$ ./configure
$ make && sudo make install
grpcdemo
├── cmake
│   ├── FindGRPC.cmake
│   └── FindProtobuf.cmake
├── CMakeLists.txt
├── protos
│   └── helloworld.proto
└── src
    ├── greeter_client.cc
    └── greeter_server.cc

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

project(HelloWorld)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

# Find Protobuf
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
find_package(Protobuf REQUIRED)
set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)
message(STATUS "Using protobuf ${protobuf_VERSION}")

# Find gRPC
find_package(GRPC REQUIRED)
set(_GRPC_GRPCPP_REFLECTION gRPC::grpc++_reflection)
message(STATUS "Using gRPC ${gRPC_VERSION}")

# Proto file
message(STATUS "CMAKE_CURRENT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}")
set(hw_proto ${CMAKE_CURRENT_SOURCE_DIR}/protos/helloworld.proto)

set(PROTO_SRC_DIR ${CMAKE_CURRENT_BINARY_DIR}/proto-src)
file(MAKE_DIRECTORY ${PROTO_SRC_DIR})

# Include generated *.pb.h files
include_directories(${PROTO_SRC_DIR})

# Generated sources
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_SRC_DIR} ${hw_proto})
grpc_generate_cpp(GRPC_SRCS GRPC_HDRS ${PROTO_SRC_DIR} ${hw_proto})

# Targets greeter_(client|server)
foreach(_target
  greeter_client greeter_server)
  add_executable(${_target} "${CMAKE_CURRENT_LIST_DIR}/src/${_target}.cc"
    ${PROTO_SRCS}
    ${GRPC_SRCS})
  target_link_libraries(${_target}
    ${_GRPC_GRPCPP_REFLECTION}
    ${_PROTOBUF_LIBPROTOBUF})
endforeach()

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)

demo 地址:https://github.com/alandtsang/cppdemo/tree/master/src/grpcdemo

参考

https://github.com/grpc/grpc/blob/master/BUILDING.md

https://github.com/grpc/grpc/tree/master/examples/cpp/helloworld

https://cmake.org/cmake/help/v3.0/command/find_package.html

https://github.com/IvanSafonov/grpc-cmake-example

### gRPC C++ 使用教程 #### 添加 gRPC 作为依赖项 为了将 gRPC 添加到 C++ 项目中,可以参考通用指南《开始使用 gRPC C++》,其中提供了详细的指导[^1]。 #### 编译安装 gRPC 通过 CMake 进行编译和安装是推荐的方式之一。这不仅简化了构建过程,还确保了环境配置的一致性和可靠性。 #### 基础概念与架构介绍 了解 gRPC 的核心概念、体系结构及其生命周期对于掌握其工作原理至关重要。这些基础知识有助于开发者更好地理解如何设计高效的微服务通信接口[^2]。 #### 客户端异步调用入门 针对希望探索更高级特性的开发人员而言,学习如何在 C++ 中实现 gRPC 的异步操作是非常有价值的技能。以官方提供的 `Greeter` 示例为基础的学习路径能够帮助快速上手[^3]。 ```cpp // helloworld.proto 文件中的简单 Hello Request 和 Response 消息定义 syntax = "proto3"; package helloworld; service Greeter { rpc SayHello (HelloRequest) returns (HelloReply); } message HelloRequest { string name = 1; } message HelloReply { string message = 1; } // cpp/helloworld/greeter_client.cc 中的部分代码片段展示了一个基本同步请求的例子 #include <iostream> #include "helloworld.grpc.pb.h" using grpc::Channel; using grpc::ClientContext; using grpc::Status; using helloworld::Greeter; using helloworld::HelloRequest; using helloworld::HelloReply; class GreeterClient { public: explicit GreeterClient(std::shared_ptr<Channel> channel) : stub_(Greeter::NewStub(channel)) {} std::string SayHello(const std::string& user) { HelloRequest request; request.set_name(user); HelloReply reply; ClientContext context; Status status = stub_->SayHello(&context, request, &reply); 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_; }; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值