小白入門之gRPC helloword

一、安裝gRPC

1.安裝相關工具

sudo apt-get install build-essential autoconf libtool pkg-config OpenSSL
sudo apt-get install cmake
sudo apt-get install libgflags-dev clang-5.0 libc++-dev


點擊yes即可。

Ubuntu14.04 安裝clang-5.0時出錯,因暫時不需要,跳過該工具安裝即可。

2.編譯命令

cd /home/workspace/project/grpc
mkdir -p cmake/build
cd cmake/build
cmake ../..
make -j8

虛擬機執行顯示cmake提示版本過低。因Ubuntu14.04軟件源的問題,sudo apt-get install cmake,默認安裝2.8.12.2 。因此採用離線包的方式安裝新版本cmake.

更新cmake版本

1.解壓並安裝所需的庫
tar zxvf cmake-3.6.3.tar.gz
sudo apt-get install build-essential
sudo apt-get install pkg-config git
sudo apt-get install libgtk2.0-dev libavcodec-dev libavformat-dev libswscale-dev python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libtiff4.dev libswscale-dev libjasper-dev libdc1394-22-dev libgstreamer1.0-dev  libgstreamer-plugins-base1.0-dev

2.進入目錄,執行./bootstrap
出現錯誤:

root@chenwr-pc:/home/workspace/test/face/cmake-3.6.3# ./bootstrap
---------------------------------------------
CMake 3.6.3, Copyright 2000-2016 Kitware, Inc.
C compiler on this system is: cc
---------------------------------------------
Error when bootstrapping CMake:
Cannot find appropriate C++ compiler on this system.
Please specify one using environment variable CXX.
See cmake_bootstrap.log for compilers attempted.
---------------------------------------------
Log of errors: /home/workspace/test/face/cmake-3.6.3/Bootstrap.cmk/cmake_bootstrap.log

解決辦法:
sudo apt-get install g++

3.make -j 8
4.make install
5.cmake --version查看是否安裝成功

root@chenwr-pc:/home/workspace/test/face/cmake-3.6.3# cmake --version
cmake version 3.6.3

CMake suite maintained and supported by Kitware (kitware.com/cmake).

如果出現錯誤:

root@chenwr-VirtualBox:/home/workspace# cmake --version
CMake Error: Could not find CMAKE_ROOT !!!
CMake has most likely not been installed correctly.
Modules directory not found in
/usr/local/bin
Segmentation fault (core dumped)

sudo apt-get remove cmake 卸載之前安裝的舊版本cmake

gRPC編譯成功。

二、編譯helloworld

使用cmake進行編譯或者直接使用make都可以。

進入example中helloworld的目錄,執行make。出現錯誤,protoc: Command not found。

root@chenwr-pc:/home/workspace/project/grpc/examples/cpp/helloworld# make
 DEPENDENCY ERROR

You don't have protoc 3.0.0 installed in your path.
Please install Google protocol buffers 3.0.0 and its compiler.
You can find it here:

   https://github.com/google/protobuf/releases/tag/v3.0.0

Here is what I get when trying to evaluate your version of protoc:

protoc --version
make: protoc: Command not found
make: [system-check] Error 127 (ignored)


make: *** [system-check] Error 1

進入/usr/local/bin與/usr/local/lib 均沒發現protoc與libprotobuf。原來/grpc/third_party/protobuf沒有編譯。

執行如下命令安裝

cd third_party/protobuf
sudo ./autogen.sh
sudo ./configure
make -j8
sudo make install

也可以通過cmake的方式
cd third_party/protobuf
mdir cmake/build
cd cmake/build
cmake -Dprotobuf_BUILD_TESTS=OFF …
make -j8

cmake…會提示錯誤,使用cmake -Dprotobuf_BUILD_TESTS=OFF …進行配置即可。如果想編譯成動態庫,可添加-DBUILD_SHARED_LIBS=ON編譯選項。


編譯成功





安裝完該庫之後繼續編譯helloworld,使用make的方式編譯成功,實際運行效果如下。

或者在helloworld目錄下創建build,不過個人覺得還是cmake的方式比較好用。進階的話可以使用cmake-gui來進行更多配置。
cd build
cmake …
make -j8




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章