cpp|grpc c++ CentOS 安装踩坑日记

为什么要总结

  • 坑,太坑了,对CentOS及其不友好
  • 文档比较少,国内相关文档不够完善
  • Demo代码竟然有错误
下载代码
git clone https://github.com/grpc/grpc.git cd grpc git submodule update --init #会拉取比较多的代码

重要提醒,此时grpc目录压缩包已经近900MB了,如果你的网络环境不够强大,可能需要等很久的时间
我提供的地址 链接:百度网盘
提取码:0e0q
除了百度网盘,我没有其它可选的
注意:这个代码里面有错误,2019年12月初,代码库中就有错误了,但是到现在2020年1月13日,依然没有被处理,在你编译这份代码的时候,请注意,修改代码
编译安装 以下的编译步骤我在5台CentOS7的机器安装均无编译错误
前置软件需求
yum install -y gcc-c++ autoconf libtool yum groupinstall -y "Development Tools"

编译protobuf
cd third_party/protobuf/#上级目录为grpc ./autogen.sh ./configure make && sudo make install #安装grpc protobuf支持 ldconfig #重要 刷新动态库的缓存

安装grpc对应的库 修改错误代码
src/core/lib/surface/init.cc 在grpc目录下
注释159行的grpc_tracer_init();
cpp|grpc c++ CentOS 安装踩坑日记
文章图片

make && sudo make install #上级目录 grpc

运行demo 前置条件 需要将以下代码拷贝到~/.bash_profile文件下面,GRPCROOT请根据自己的grpc的目录进行设置
export GRPCROOT=/home/luojie/software/grpc #注意这个目录是我的grpc的目录,你需要修改成为你的目录 export PATH=$PATH:$GRPCROOT/bins/opt:$GRPCROOT/bins/opt/protobuf export CPATH=$GRPCROOT/include:$GRPCROOT/third_party/protobuf/src export LIBRARY_PATH=$GRPCROOT/libs/opt:$GRPCROOT/libs/opt/protobuf export PKG_CONFIG_PATH=$GRPCROOT/libs/opt/pkgconfig:$GRPCROOT/third_party/protobuf export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$GRPCROOT/libs/opt:/usr/local/lib

让环境变量生效
. ~/.bash_profile

编译demo
cd examples/cpp/helloworld # 上级目录是grpc make

输出应该如下:
protoc -I ../../protos --cpp_out=. ../../protos/helloworld.proto g++ -std=c++11 `pkg-config --cflags protobuf grpc`-c -o helloworld.pb.o helloworld.pb.cc protoc -I ../../protos --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` ../../protos/helloworld.proto g++ -std=c++11 `pkg-config --cflags protobuf grpc`-c -o helloworld.grpc.pb.o helloworld.grpc.pb.cc g++ -std=c++11 `pkg-config --cflags protobuf grpc`-c -o greeter_client.o greeter_client.cc g++ helloworld.pb.o helloworld.grpc.pb.o greeter_client.o -L/usr/local/lib `pkg-config --libs protobuf grpc++` -pthread -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed -ldl -o greeter_client g++ -std=c++11 `pkg-config --cflags protobuf grpc`-c -o greeter_server.o greeter_server.cc g++ helloworld.pb.o helloworld.grpc.pb.o greeter_server.o -L/usr/local/lib `pkg-config --libs protobuf grpc++` -pthread -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed -ldl -o greeter_server g++ -std=c++11 `pkg-config --cflags protobuf grpc`-c -o greeter_async_client.o greeter_async_client.cc g++ helloworld.pb.o helloworld.grpc.pb.o greeter_async_client.o -L/usr/local/lib `pkg-config --libs protobuf grpc++` -pthread -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed -ldl -o greeter_async_client g++ -std=c++11 `pkg-config --cflags protobuf grpc`-c -o greeter_async_client2.o greeter_async_client2.cc g++ helloworld.pb.o helloworld.grpc.pb.o greeter_async_client2.o -L/usr/local/lib `pkg-config --libs protobuf grpc++` -pthread -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed -ldl -o greeter_async_client2 g++ -std=c++11 `pkg-config --cflags protobuf grpc`-c -o greeter_async_server.o greeter_async_server.cc g++ helloworld.pb.o helloworld.grpc.pb.o greeter_async_server.o -L/usr/local/lib `pkg-config --libs protobuf grpc++` -pthread -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed -ldl -o greeter_async_server

如题是下面的样子
protoc -I ../../protos --cpp_out=. ../../protos/helloworld.proto g++ -std=c++11 `pkg-config --cflags protobuf grpc`-c -o helloworld.pb.o helloworld.pb.cc Package grpc was not found in the pkg-config search path. Perhaps you should add the directory containing `grpc.pc' to the PKG_CONFIG_PATH environment variable No package 'grpc' found protoc -I ../../protos --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` ../../protos/helloworld.proto g++ -std=c++11 `pkg-config --cflags protobuf grpc`-c -o helloworld.grpc.pb.o helloworld.grpc.pb.cc Package grpc was not found in the pkg-config search path. Perhaps you should add the directory containing `grpc.pc' to the PKG_CONFIG_PATH environment variable No package 'grpc' found g++ -std=c++11 `pkg-config --cflags protobuf grpc`-c -o greeter_client.o greeter_client.cc Package grpc was not found in the pkg-config search path. Perhaps you should add the directory containing `grpc.pc' to the PKG_CONFIG_PATH environment variable No package 'grpc' found g++ helloworld.pb.o helloworld.grpc.pb.o greeter_client.o -L/usr/local/lib `pkg-config --libs protobuf grpc++` -pthread -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed -ldl -o greeter_client Package grpc++ was not found in the pkg-config search path. Perhaps you should add the directory containing `grpc++.pc' to the PKG_CONFIG_PATH environment variable No package 'grpc++' found /usr/bin/ld: greeter_client.o: undefined reference to symbol '_ZN9grpc_impl13ClientContextD1Ev' /usr/local/lib/libgrpc++.so.1: error adding symbols: DSO missing from command line collect2: error: ld returned 1 exit status make: *** [greeter_client] Error 1

【cpp|grpc c++ CentOS 安装踩坑日记】那么一定是你的环境变量搞错,请着重检测~/.bash_profile中的配置的正确性
运行
./greeter_server & [1] 30649 [luojie@VM_0_14_redhat helloworld]$ Server listening on 0.0.0.0:50051[luojie@VM_0_14_redhat helloworld]$ ./greeter_client Greeter received: Hello world

    推荐阅读