grpc安裝與第一個程序HelloWorld

安裝

全部採用源碼方式安裝

  • 安裝cmake ,一般centos系統默認未2.8.12.2 但是實際運行程序最小要求3.5.1
[root@VM_117_243_centos ~/grpc/examples/cpp/helloworld]# cmake .
CMake Error at CMakeLists.txt:20 (cmake_minimum_required):
  CMake 3.5.1 or higher is required.  You are running version 2.8.12.2

這裏我們可以安裝3.13,以及更高的版本。當然也可以不這麼做,但是你會遇到很多麻煩事,具體詳情可以看這個issue(https://github.com/grpc/grpc/issues/22231)。其中這段話非常重要:

You need cmake 3.13 or newer to install everything that gRPC needs from gRPC installation. 
Otherwise, you need to install all dependencies by yourselves. You can use this script as a reference.

cmake的安裝方法如下,採用源碼方式安裝:

  1. 首先卸載原有cmake (yum remove cmake)
[root@VM_117_243_centos ~/grpc/examples/cpp/helloworld]# yum remove cmake
Loaded plugins: fastestmirror
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
Resolving Dependencies
--> Running transaction check
---> Package cmake.x86_64 0:2.8.12.2-2.tl2 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================================================================================================================
 Package                                   Arch                                       Version                                            Repository                                   Size
===========================================================================================================================================================================================
Removing:
 cmake                                     x86_64                                     2.8.12.2-2.tl2                                     @tlinux                                      27 M

Transaction Summary
===========================================================================================================================================================================================
Remove  1 Package

Installed size: 27 M
Is this ok [y/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : cmake-2.8.12.2-2.tl2.x86_64                                                                                                                                             1/1 
  Verifying  : cmake-2.8.12.2-2.tl2.x86_64                                                                                                                                             1/1 

Removed:
  cmake.x86_64 0:2.8.12.2-2.tl2                                                                                                                                                            

Complete!
[root@VM_117_243_centos ~/grpc/examples/cpp/helloworld]# cmake --version
-bash: /usr/bin/cmake: No such file or directory
[root@VM_117_243_centos ~/grpc/examples/cpp/helloworld]# 
  1. wget https://github.com/Kitware/CMake/releases/download/v3.17.2/cmake-3.17.2.tar.gz
  2. tar -xvzf cmake-3.17.2.tar.gz
  3. ./bootstrap
  4. gmake -j4 (-j4採用多線程編譯,加快速度)
  5. gmake install
  6. source /etc/profile (這一步如果不做,cmake --verison會找不到cmake)
  • 安裝protobuf
  1. git clone https://github.com/protocolbuffers/protobuf.git
  2. git submodule update --init --recursive.
  3. ./autogen.sh
  4. ./configure
  5. make -j4
  6. make check
  7. make install
  • 安裝grpc
  1. git clone https://github.com/grpc/grpc.git --recurse-submodules
  2. cd grpc
  3. make
  4. make install #安裝完成
  5. cd example/cpp/helloworld
  6. cmake .
    之後編譯,出現這個錯誤:
CMake Error at CMakeLists.txt:103 (find_package):
  Could not find a package configuration file provided by "Protobuf" with any
  of the following names:

    ProtobufConfig.cmake
    protobuf-config.cmake

解決方法:
參考:https://groups.google.com/forum/#!topic/protobuf/BoWqjv1e84s

1.enter the "protobuf/cmake/" dir.
2.mkdir build. protobuf/cmake/build
3.cmake ..
4.make && make install

但是有出現了另外一個問題,grpc又報這個錯誤了。。。。。:

-- Found ZLIB: /usr/lib64/libz.so (found version "1.2.7") 
-- Using protobuf 
CMake Error at CMakeLists.txt:116 (find_package):
  Could not find a package configuration file provided by "gRPC" with any of
  the following names:

    gRPCConfig.cmake
    grpc-config.cmake

  Add the installation prefix of "gRPC" to CMAKE_PREFIX_PATH or set
  "gRPC_DIR" to a directory containing one of the above files.  If "gRPC"
  provides a separate development package or SDK, be sure it has been
  installed.

同樣的解決方法:

在grpc/cmake/build目錄下
cmake ../../
make -j4
make install
  • 運行第一個程序helloworld
  1. cd grpc/example/cpp/helloword
  2. mkdir -p cmake/build
  3. cd cmake/build
  4. cmake …/…
  5. make -j4
[root@VM_117_243_centos ~/grpc/examples/cpp/helloworld/cmake/build]# ./greeter_server 
Server listening on 0.0.0.0:50051

[root@VM_117_243_centos ~/grpc/examples/cpp/helloworld/cmake/build]# ./greeter_client 
Greeter received: Hello world
[root@VM_117_243_centos ~/grpc/examples/cpp/helloworld/cmake/build]#
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章