• grpc|protobuf的安装、编译、运行笔记(C++)


    一、下载grpc源码

    如果你的电脑/服务器可以做代理,然后稳定链接上 GitHub 那么完全可以按照 GitHub 的官方文档来操作,我这里采用 Gitee 镜像来操作

    git clone https://gitee.com/jiangxy__loey/grpc.git
    
    • 1

    在这里插入图片描述

    二、下载依赖库

    进入grpc目录,然后里面有一个 .gitmodules 文件,我们将其替换成 gitee 的链接,首先清空内容

    > .gitmodules
    
    • 1

    然后再将新内容填入:

    [submodule "third_party/zlib"]
    	path = third_party/zlib
    	#url = https://github.com/madler/zlib
    	url = https://gitee.com/jiangxy__loey/zlib.git
    	# When using CMake to build, the zlib submodule ends up with a
    	# generated file that makes Git consider the submodule dirty. This
    	# state can be ignored for day-to-day development on gRPC.
    	ignore = dirty
    [submodule "third_party/protobuf"]
    	path = third_party/protobuf
    	url = https://gitee.com/jiangxy__loey/protobuf.git
    	#url = https://github.com/google/protobuf.git
    [submodule "third_party/gflags"]
    	path = third_party/gflags
    	#url = https://github.com/gflags/gflags.git
    	url = https://gitee.com/jiangxy__loey/gflags.git
    [submodule "third_party/benchmark"]
    	path = third_party/benchmark
    	#url = https://github.com/google/benchmark
    	url = https://gitee.com/jiangxy__loey/benchmark.git
    [submodule "third_party/boringssl-with-bazel"]
    	path = third_party/boringssl-with-bazel
    	#url = https://github.com/google/boringssl.git
    	url = https://gitee.com/jiangxy__loey/boringssl-with-bazel.git
    [submodule "third_party/re2"]
    	path = third_party/re2
    	#url = git://github.com/google/re2.git
    	url = https://gitee.com/hejuncheng1/re2.git
    [submodule "third_party/cares/cares"]
    	path = third_party/cares/cares
    	#url = https://github.com/c-ares/c-ares.git
    	url = https://gitee.com/jiangxy__loey/cares.git
    	#branch = cares-1_12_0
    [submodule "third_party/bloaty"]
    	path = third_party/bloaty
    	#url = https://github.com/google/bloaty.git
    	url = https://gitee.com/jiangxy__loey/bloaty.git
    [submodule "third_party/abseil-cpp"]
    	path = third_party/abseil-cpp
    	#url = https://github.com/abseil/abseil-cpp.git
    	url = https://gitee.com/jiangxy__loey/abseil-cpp.git
    	branch = lts_2020_02_25
    [submodule "third_party/envoy-api"]
    	path = third_party/envoy-api
    	#url = https://github.com/envoyproxy/data-plane-api.git
    	url = https://gitee.com/jiangxy__loey/envoy-api.git
    [submodule "third_party/googleapis"]
    	path = third_party/googleapis
    	#url = https://github.com/googleapis/googleapis.git
    	url = https://gitee.com/jiangxy__loey/googleapis.git
    [submodule "third_party/protoc-gen-validate"]
    	path = third_party/protoc-gen-validate
    	#url = https://github.com/envoyproxy/protoc-gen-validate.git
    	url = https://gitee.com/jiangxy__loey/protoc-gen-validate.git
    [submodule "third_party/udpa"]
    	path = third_party/udpa
    	#url = https://github.com/cncf/udpa.git
    	url = https://gitee.com/jiangxy__loey/udpa.git
    [submodule "third_party/libuv"]
    	path = third_party/libuv
    	#url = https://github.com/libuv/libuv.git
    	url = https://gitee.com/jiangxy__loey/libuv.git
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    然后下载:

    git submodule update --init
    
    • 1

    在这里插入图片描述

    当然如果如果说你的电脑不能科学上网或者上面的操作报错了,说明电脑网络有问题,可以直接下载这个继续操作:
    链接:https://pan.baidu.com/s/1sPFO5jXKBJnbNQwiWCLQEw?pwd=wmak
    提取码:wmak

    四、安装工具和环境依赖

    sudo apt-get install pkg-config
    sudo apt-get install autoconf automake libtool make g++ unzip
    sudo apt-get install libgflags-dev libgtest-dev
    sudo apt-get install clang libc++-dev
    
    • 1
    • 2
    • 3
    • 4

    五、编译protobuf

    进入grpc/third_party/protobuf 目录,然后给 autogen.sh 文件加上执行权限:

    chmod +x autogen.sh
    
    • 1

    因为是在 gitee 下载的文件,所以文本格式为 PC,我们需要将其转化为 unix,否则编译会报错,我们通过借助dos2unix工具转换

    • 下载工具
    apt install dos2unix
    
    • 1

    在这里插入图片描述

    • grpc/third_party/protobuf 目录下对所有文件进行格式转换
    dos2unix  *
    
    • 1

    在这里插入图片描述

    此时格式替换完成,我们就可以开始编译 protobuf 了,执行 autogen.sh 脚本即可

    ./autogen.sh
    
    • 1

    在这里插入图片描述

    假设新建库的输出路径 /usr/local
    设置库的输出路径

    ./configure --prefix=/usr/local
    
    • 1

    开始编译(可能会编的有点久,看个人机器)

    make
    make check
    sudo make install
    sudo ldconfig  # 使得新安装的动态库能被加载
    
    protoc --version
    显示3.19.4
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    注意,在这一步可能会遇到找不到gawk 的情况
    我们只需要apt安装即可

    apt install gawk
    
    • 1

    编译完成后此时我们进入/home/mangata/grpc_learn/grpc/protobuf,然后输出一下当前的目录结构:

    在这里插入图片描述

    到这里, protobuf 编译完成,接下来就是编译 grpc

    六、编译grpc

    我们回到 grpc/ 这个路径,然后执行下面的操作即可完成

    mkdir -p cmake/build
    cd cmake/build
    cmake ../..
    make
    sudo make install
    
    • 1
    • 2
    • 3
    • 4
    • 5

    sudo make install 报错了,那就去 /usr/local/share/man/ 目录将man1 删掉,并且创建一个man1 的文件夹
    在这里插入图片描述
    在这里插入图片描述

    七、测试

    进入grpc/examples/cpp/helloworld 目录,然后创建build 文件夹

    cd grpc/examples/cpp/helloworld/
    mkdir build
    cd build/
    cmake ..
    make
    
    • 1
    • 2
    • 3
    • 4
    • 5

    进入 build 文件夹cd build ,然后cmake .. ,最后输入 make

    在这里插入图片描述

    在这里插入图片描述

    启动服务和客户端

    # 启动服务端,监听在50051端口
    ./greeter_server
    Server listening on 0.0.0.0:50051
    # 启动客户端,服务端返回Hello world
    ./greeter_client 
    Greeter received: Hello world
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述
    看到回应 Hello world 就完成了,接下来就是学习 grpc 的四种模式了,下期再见~

  • 相关阅读:
    实战!工作中常用的设计模式
    Gnuplot:安装与使用备忘
    计算机的前世今生
    CentOS密码重置
    多路查找树
    新变化新营销 这些知识点你得 Get!(文末有 PPT 福利首次放送)
    java计算机毕业设计智慧校园系统后端源码+mysql数据库+系统+lw文档+部署
    macos知名的清理软件 cleanmymac和腾讯柠檬哪个好 cleanmymacx有必要买吗
    java-net-php-python-ssm电影影评网站计算机毕业设计程序
    网页标签在html中的显示+单标记换行操作
  • 原文地址:https://blog.csdn.net/m0_46201544/article/details/126811237