分享

grpc C++ 编译及使用

 码农书馆 2020-09-14

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

结果如下:

这里写图片描述

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多