1. 一些工具安装
$ apt-get install build-essential autoconf libtool pkg-config
$ apt-get install libgflags-dev libgtest-dev
$ apt-get install clang libc++-dev
2. 源码下载
$ git clone -b v1.15.0 https://github.com/grpc/grpc
$ cd grpc
$ git submodule update --init
3. 编译
编译的话,直接进入grpc根目录,然后在控制台make即可
$ make
笔者编译的时候,遇到的一些问题
- 编译protobuf的时候失败, 提示
这个时候,进入protobuf源码文件, 运行autogen.sh脚本,然后再返回到grpc源码根目录,继续编译即可config.status: error: cannot find input file: `Makefile.in'
$ cd ./third_party/protobuf $ ./autogen.sh
- 没有自动编译zlib库
编译grpc库的时候,并没有自动编译zlib库,所有需要自己手动去编译下$ cd ./third_party/zlib $ mdkir .build $ cd .build $ cmake .. $ make
4. 使用
第一次测试使用的话,可以使用example里面的helloworld
- 先根据helloworld.proto生成pb和grpc.pb文件
$ protoc.exe -I=. --grpc_out=../pb_gen --plugin=protoc-gen-grpc=../../.../../YDK/3rd/grpc-1.15.0/bin/linux/grpc_cpp_plugin helloworld.proto
$ protoc.exe -I=. --cpp_out=../pb_gen helloworld.proto
protoc-gen-grpc 后面跟的是grpc_cpp_plugin二进制路径, 然后在pb_gen文件夹会生成4个文件
helloworld.grpc.pb.h
helloworld.grpc.pb.cc
helloworld.pb.cc
helloworld.pb.h
- CMakelist.txt
代码路径
grpc_test
|----bin
|----pb_gen
|----helloworld.grpc.pb.cc
|----helloworld.grpc.pb.h
|----helloworld.pb.cc
|----helloworld.pb.h
|----src
|----client
|-----greeter_client.cc
|----server
|-----greeter_server.cc
|----projects
|----cmake
|----CMakeLists.txt
CMakelist.txt
cmake_minimum_required(VERSION 2.6)
project(grpc_test)
#add_compile_options(-std=c++11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -march=native -O3 -pthread")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ../../../bin/)
# client
set(CLI_HEADER_SRCS
../../pb_gen/helloworld.grpc.pb.h
../../pb_gen/helloworld.pb.h
)
set(CLI_CPP_SRCS
../../pb_gen/helloworld.grpc.pb.cc
../../pb_gen/helloworld.pb.cc
../../src/client/greeter_client.cc
)
include_directories(
../../src
../../pb_gen
../../../../../YDK/3rd/protobuf-3.5.1/include
../../../../../YDK/3rd/grpc-1.15.0/include
../../../../../YDK/
)
add_executable(grpc_test_client ${CLI_HEADER_SRCS} ${CLI_CPP_SRCS})
FIND_LIBRARY(PB_LIB protobuf ../../../../../YDK/3rd/protobuf-3.5.1/lib/linux)
message(STATUS "protobuf lib path:" ${PB_LIB})
if(NOT PB_LIB)
message(FATAL_ERROR "not find the protobuf lib" )
endif(NOT PB_LIB)
FIND_LIBRARY(ADDR_SORT_LIB address_sorting ../../../../../YDK/3rd/grpc-1.15.0/lib/linux)
message(STATUS "address_sorting lib path:" ${ADDR_SORT_LIB})
if(NOT ADDR_SORT_LIB)
message(FATAL_ERROR "not find the address_sorting lib" )
endif(NOT ADDR_SORT_LIB)
FIND_LIBRARY(ARES_LIB ares ../../../../../YDK/3rd/grpc-1.15.0/lib/linux)
message(STATUS "ares lib path:" ${ARES_LIB})
if(NOT ARES_LIB)
message(FATAL_ERROR "not find the ares lib" )
endif(NOT ARES_LIB)
FIND_LIBRARY(BSSL_LIB boringssl ../../../../../YDK/3rd/grpc-1.15.0/lib/linux)
message(STATUS "boringssl lib path:" ${BSSL_LIB})
if(NOT BSSL_LIB)
message(FATAL_ERROR "not find the boringssl lib" )
endif(NOT BSSL_LIB)
FIND_LIBRARY(GPR_LIB gpr ../../../../../YDK/3rd/grpc-1.15.0/lib/linux)
message(STATUS "gpr lib path:" ${GPR_LIB})
if(NOT GPR_LIB)
message(FATAL_ERROR "not find the gpr lib" )
endif(NOT GPR_LIB)
FIND_LIBRARY(GRPC_LIB grpc ../../../../../YDK/3rd/grpc-1.15.0/lib/linux)
message(STATUS "grpc lib path:" ${GRPC_LIB})
if(NOT GRPC_LIB)
message(FATAL_ERROR "not find the grpc lib" )
endif(NOT GRPC_LIB)
FIND_LIBRARY(GRPCPP_LIB grpc++ ../../../../../YDK/3rd/grpc-1.15.0/lib/linux)
message(STATUS "grpc++ lib path:" ${GRPCPP_LIB})
if(NOT GRPCPP_LIB)
message(FATAL_ERROR "not find the grpc++ lib" )
endif(NOT GRPCPP_LIB)
FIND_LIBRARY(ZLIB_LIB z ../../../../../YDK/3rd/grpc-1.15.0/lib/linux)
message(STATUS "zlib lib path:" ${ZLIB_LIB})
if(NOT ZLIB_LIB)
message(FATAL_ERROR "not find the zlib lib" )
endif(NOT ZLIB_LIB)
target_link_libraries(grpc_test_client ${PB_LIB} ${GRPCPP_LIB}
${GRPC_LIB} ${GPR_LIB} ${BSSL_LIB} ${ARES_LIB} ${ADDR_SORT_LIB} ${ZLIB_LIB})
# server
set(SRV_HEADER_SRCS
../../pb_gen/helloworld.grpc.pb.h
../../pb_gen/helloworld.pb.h
)
set(SRV_CPP_SRCS
../../pb_gen/helloworld.grpc.pb.cc
../../pb_gen/helloworld.pb.cc
../../src/server/greeter_server.cc
)
include_directories(
../../src
../../pb_gen
../../../../../YDK/3rd/protobuf-3.5.1/include
../../../../../YDK/3rd/grpc-1.15.0/include
../../../../../YDK/
)
# link dir
# link_directories(../../../../../YDK/3rd/grpc-1.15.0/lib/linux)
add_executable(grpc_test_server ${SRV_HEADER_SRCS} ${SRV_CPP_SRCS})
FIND_LIBRARY(PB_LIB protobuf ../../../../../YDK/3rd/protobuf-3.5.1/lib/linux)
message(STATUS "protobuf lib path:" ${PB_LIB})
if(NOT PB_LIB)
message(FATAL_ERROR "not find the protobuf lib" )
endif(NOT PB_LIB)
target_link_libraries(grpc_test_server ${PB_LIB}
${GRPCPP_LIB} ${GRPC_LIB} ${GPR_LIB} ${BSSL_LIB} ${ARES_LIB} ${ADDR_SORT_LIB} ${ZLIB_LIB})
依赖的库有
libprotobuf.lib
libgrpc++.lib
libgrpc.lib
libgpr.lib
libboringssl.lib
libares.lib
libaddress_sorting.lib
libz.lib
- 编译
$ cd ./projects/cmake
$ mkdir .build
$ cd .build
$ cmake ..
$ make
- 运行
- server
$ ./grpc_test_server Server listening on 0.0.0.0:50051
- client
$ ./grpc_test_client Greeter received: Hello world
- server
网友评论