grpc C++ 编译及使用

本文详细介绍gRPC源码下载、依赖安装、编译过程及常见错误解决方案,包括protobuf版本冲突处理,并演示如何运行gRPC的示例程序。

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

1 下载源码

git clone https://github.com/grpc/grpc.git
cd grpc
git submodule update --init #跟新第三方源

2 安装
查看 BUILDING.md 先安装依赖项

 $ [sudo] apt-get install build-essential autoconf libtool pkg-config

If you plan to build from source and run tests, install the following as well:

 $ [sudo] apt-get install libgflags-dev libgtest-dev
 $ [sudo] apt-get install clang libc++-dev

再如下操作:

make
sudo make  install prefix=/usr/local/
sudo ldconfig

测试 protobuf

protoc --version

输出如下,已经给安装上了最新的protobuf

libprotoc 3.6.1

遇到错误:1

 /grpc/gens/src/proto/grpc/core/stats.pb.cc:187:13: error: ‘dynamic_init_dummy_src_2fproto_2fgrpc_2fcore_2fstats_2eproto’ defined but not used [-Werror=unused-variable]
 static bool dynamic_init_dummy_src_2fproto_2fgrpc_2fcore_2fstats_2eproto = []()
             ^
cc1plus: all warnings being treated as errors

解决办法
找到Makefile,去掉其中-Werror ,make clean 重新编译。

遇到错误:2
protobuf与grpc 所要求的版本不一致,按要求安装就好。

3 跑示例程序
进入 /examples/cpp/route_guide/:

cd  examples/cpp/route_guide

执行

make route_guide.grpc.pb.cc route_guide.pb.cc

其实执行的语句为:

protoc -I ../../protos --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` ../../protos/route_guide.proto
protoc -I ../../protos --cpp_out=. ../../protos/route_guide.proto

最后make生成可执行文件

make 

先启动server,再启动client

./route_guide_server
./route_guide_client

结果如下:

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值