mac 下安装grpc (包含 c++)

一、准备编译环境

安装各种依赖库,详见:Pre-requisites

shell 代码

brew install autoconf automake libtool shtool gflags

二. 安装protobuf3

shell代码

git clone https://github.com/google/protobuf.git  
cd protobuf  
git checkout v3.5.0  
sh ./autogen.sh  
./configure --prefix=/usr/local/protobuf/    
make 
sudo make install

解释:

1、最后一条 shell 命令加上 sudo,它要在 /usr/local/protobuf/ 下面创建目录,这是需要最高权限的

2、验证 protobuf3 是否安装成功:

cd /usr/local/protobuf/bin/
./protoc --version

出现结果:libprotoc 3.5.0

3、在文件 ~/.bash_profile 中,为程序 protoc 添加环境变量,如下:

export PATH=$PATH:/usr/local/protobuf/bin/

然后执行

source ~/.bash_profile

否则后续编译官方的 helloworl小程序,会找不到程序 protoc

三、安装grpc

shell 代码

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

解释:

1、当使用git clone下来的工程中带有submodule时,初始的时候,submodule的内容并不会自动下载下来的,所以需要执行 git submodule update --init

2、git clone -b 分支名  仓库地址是克隆某分支到本地目录,命令 curl -L https://grpc.io/release 的结果是 v1.27.0,所以就是下载分支 v1.27.0 到本地目录

3、编译成功后会在/usr/local/bin/ 生成grpc各语言插件,如grpc_cpp_plugin,grpc_php_plugin等

四、编译 helloworld 小程序

1、使用自带 Makefile

a、修改 CPPFLAGS += `pkg-config --cflags protobuf grpc` 为

CPPFLAGS += -I/usr/local/include -I/usr/local/protobuf/include -L/usr/local/lib -L/usr/local/protobuf/lib -lprotobuf -lgrpc

b、修改 LDFLAGS += -L/usr/local/lib `pkg-config --libs protobuf grpc++` 为

LDFLAGS += -L/usr/local/lib -L/usr/local/protobuf/lib -lprotobuf -lgrpc -lgrpc++

原因是,系统是识别不了 pkg-config ,然后直接 make 就生成可执行程序了

2、解释:

很多开源代码在makefile中会使用pkg-config,pkg-config是linux下获取系统库/模块信息的命令,如下命令,就是查看grpc头文件路径及依赖库名称

shell 代码:

pkg-config --libs --cflags grpc

执行结果:

-I/usr/local/include -L/usr/local/lib -lgrpc

所以解决办法:

a、替换对应的 pkg-config 命令为相应的库和头文件

b、在 mac 上安装pkg-config

2、手动编译

是以生成 客户端程序 greeter_client 和服务端程序 greeter_server 为例

shell 代码如下:

#common:

#生成 helloworld.pb.cc  和  helloworld.pb.h
protoc -I ../../protos --cpp_out=. ../../protos/helloworld.proto

#生成 helloworld.pb.o
g++ -std=c++11 -I/usr/local/include -I/usr/local/protobuf/include   -c -o helloworld.pb.o helloworld.pb.cc

#生成 helloworld.grpc.pb.cc 和 helloworld.grpc.pb.h
protoc -I ../../protos --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` ../../protos/helloworld.proto

#生成 helloworld.grpc.pb.o
g++ -std=c++11 -I/usr/local/include -I/usr/local/protobuf/include   -c -o helloworld.grpc.pb.o helloworld.grpc.pb.cc


#Client:

#生成 greeter_client.o
g++ -std=c++11 -I/usr/local/include -I/usr/local/protobuf/include   -c -o greeter_client.o greeter_client.cc

#生成客户端程序 greeter_client
g++ helloworld.pb.o helloworld.grpc.pb.o greeter_client.o -L/usr/local/lib -L/usr/local/protobuf/lib -lprotobuf -lgrpc -lgrpc++ -pthread -lgrpc++_reflection -ldl -o greeter_client

#Server

#生成 greeter_server.o
g++ -std=c++11 -I/usr/local/include -I/usr/local/protobuf/include   -c -o greeter_server.o greeter_server.cc

#生成服务端程序 greeter_server
g++ helloworld.pb.o helloworld.grpc.pb.o greeter_server.o -L/usr/local/lib -L/usr/local/protobuf/lib -lprotobuf -lgrpc -lgrpc++ -pthread -lgrpc++_reflection -ldl -o greeter_server

参考文章:

https://www.cnblogs.com/freedommovie/p/11114721.html

https://www.iteye.com/blog/tcspecial-2437380

发布了12 篇原创文章 · 获赞 2 · 访问量 1988

猜你喜欢

转载自blog.csdn.net/weileshenghuo1/article/details/104428017