linux grpc,grpc linux下的编译使用-Go语言中文社区
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的時候失敗, 提示
config.status: error: cannot find input file: `Makefile.in'
這個時候,進入protobuf源碼文件, 運行autogen.sh腳本,然后再返回到grpc源碼根目錄,繼續(xù)編譯即可
$ cd ./third_party/protobuf
$ ./autogen.sh
沒有自動編譯zlib庫
編譯grpc庫的時候,并沒有自動編譯zlib庫,所有需要自己手動去編譯下
$ cd ./third_party/zlib
$ mdkir .build
$ cd .build
$ cmake ..
$ make
4. 使用
第一次測試使用的話,可以使用example里面的helloworld
先根據(jù)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
總結(jié)
以上是生活随笔為你收集整理的linux grpc,grpc linux下的编译使用-Go语言中文社区的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android studio 测试工具,
- 下一篇: 全国最佳医院排名