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();
註釋代碼

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

那麼一定是你的環境變量搞錯,請着重檢測~/.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
發佈了13 篇原創文章 · 獲贊 4 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章