grpc使用记录(一) gRPC编译(mscv/gcc)

1、编译前的准备工作

1、下载源码

git clone https://github.com/grpc/grpc.git

2、下载依赖项,grpc的依赖项都以子模块的方式记录在third_party目录下,所以直接同步子模块即可。

git submodule update --init
# 可以使用 --recursive 选项递归子模块

文件数比较多,完整下载后打包大小有804MB(PS.都是完整克隆)。

文件列表

3、安装go

如果不按照go,则在生成编译脚本的时候会报以下错误:

CMake Error at third_party/boringssl/CMakeLists.txt:38 (message):
  Could not find Go

Windows上直接下载安装包 go1.12.5.windows-amd64 安装即可。

Linux下也可以直接下载安装包安装,或者是使用相应发行版的包管理器安装。

4、安装perl

2、Windows下使用VS2019编译

编译过程很简单,就是编译的东西比较多,速度比较慢。下面记录下编译的具体过程。

2.1、使用cmake生成VS2019解决方案

在生成VS2015解决方案前需要先安装cmake 3.x工具,已经VS开发环境。我这里是使用VS2019进行的编译,但也适用于VS2015/VS2017,这是执行cmake命令行工具的时候指定的参数稍有不同。


2.1.1、生成时使用的基本选项设定

指定编译平台工具集和编译配置类型等

# 下面-G/-A 选项仅适用于VS2019,如果是VS2017或2015等,则需要使用-G "Visual Studio 15 2017 Win64"形式
—G "Visual Studio 16 2019" -A x64 \
-DCMAKE_CONFIGURATION_TYPES=Release # 或者 DCMAKE_BUILD_TYPE=Release

指定go.exe路径

-DGO_EXECUTABLE="C:/Go/bin/go.exe"

指定安装输出路径

-DCMAKE_INSTALL_PREFIX="E:/complier/grpc/install" \
# 下面几个也可以不指定
-DINSTALL_BIN_DIR="E:/complier/grpc/install/grpc/bin" \
-DINSTALL_MAN_DIR="E:/complier/grpc/install/grpc/share/man" \
-DINSTALL_INC_DIR="E:/complier/grpc/install/grpc/include" \
-DINSTALL_PKGCONFIG_DIR="E:/complier/grpc/install/grpc/share/pkgconfig" \

指定使用静态运行时库

-DgRPC_MSVC_STATIC_RUNTIME=1

这里和下面编译第三方库时候都编译使用静态(MT)运行时的是对应的,如果下面编译第三方库都指定动态运行时库(MD),则无需指定该选项。


2.1.2、仅使用上面选项,执行后报如下ZLIB警告。

意思就是因为gRPC_ZLIB_PROVIDERmodule,所以强制设置gRPC_INSTALLFALSE

CMake Warning at cmake/zlib.cmake:32 (message):
  gRPC_INSTALL will be forced to FALSE because gRPC_ZLIB_PROVIDER is "module"
Call Stack (most recent call first):
  CMakeLists.txt:140 (include)

解决这个警告需要使用到zlib库,关于它的编译这里就不再说了,可以查看 VS编译 x64版本zlib库 的内容。

我这里就没有去编译它,直接使用了以前vcpkg安装的。然后指定如下选项继续。

 -DgRPC_ZLIB_PROVIDER=package \
 -DZLIB_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/zlibd.lib" \
 -DZLIB_INCLUDE_DIR="E:/complier/x64-windows-static/include" \
 -DZLIB_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/zlib.lib" 

2.1.3、继续,报如下CARES警告。这个警告可以不用管,也能正常编译通过。
CMake Warning at cmake/cares.cmake:33 (message):
  gRPC_INSTALL will be forced to FALSE because gRPC_CARES_PROVIDER is
  "module"
Call Stack (most recent call first):
  CMakeLists.txt:141 (include)

但我这里还是解决它,先下载 c-ares 编译安装。

# 下载源码(在grpc\third_party\cares\cares目录下也是一样的,可以不用下载)
git clone https://github.com/c-ares/c-ares.git
# 生成VS工程
cd c-ares
# 注意,下面必须指定CARES_MSVC_STATIC_RUNTIME选项,否则后面编译grpc时候会通不过(找不到__imp__xxx)
cmake . -G "Visual Studio 16 2019" -A x64 -DCMAKE_INSTALL_PREFIX=E:/complier/x64-windows-static -DCARES_STATIC=1 -DCARES_SHARED=0 -DCARES_MSVC_STATIC_RUNTIME=1
# 编译
msbuild /maxcpucount:4 /p:Configuration=Release /p:Platform=x64 c-ares.sln
# 安装,上面生成VS工程时候指定了安装在当E:/complier/x64-windows-static目录下
msbuild /maxcpucount:4 /p:Configuration=Release /p:Platform=x64 INSTALL.vcxproj

然后指定下面几个选项后继续。

-DgRPC_CARES_PROVIDER=package \
-Dc-ares_DIR="E:/complier/x64-windows-static/lib/cmake/c-ares" # 这里是指定到c-ares-config.cmake文件的路径

2.1.4、继续,报如下PROTOBUF警告
-- 3.8.0.0
CMake Warning (dev) at third_party/protobuf/cmake/install.cmake:60 (message):
  The file
  "E:/complier/grpc/grpc/third_party/protobuf/src/google/protobuf/stubs/io_win32.h"
  is listed in
  "E:/complier/grpc/grpc/third_party/protobuf/cmake/cmake/extract_includes.bat.in"
  but there not exists.  The file will not be installed.
Call Stack (most recent call first):
  third_party/protobuf/cmake/CMakeLists.txt:231 (include)
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning at cmake/protobuf.cmake:51 (message):
  gRPC_INSTALL will be forced to FALSE because gRPC_PROTOBUF_PROVIDER is
  "module"
Call Stack (most recent call first):
  CMakeLists.txt:142 (include)

解决这个问题的步骤如下:

1、进入grpc/third_party/protobuf目录,使用git命令更新下子模块git submodule update --init。或者在一开始对grpc更新子模块的时候,使用--recursive进行递归更新。

2、生成编译配置,编译安装(参考:protocolbuffers/protobuf/appveyor.bat)

# 参考protobuf目录下的 appveyor.bat 脚本文件
mkdir build_msvc
cd build_msvc
# 指定 protobuf_BUILD_SHARED_LIBS 为0,以便生成静态库
cmake -G "Visual Studio 14 2015 Win64" -Dprotobuf_BUILD_SHARED_LIBS=0 -Dprotobuf_UNICODE=1 -DCMAKE_INSTALL_PREFIX=E:/complier/x64-windows-static ../cmake

# 编译,这里指定生成了Release版本的,默认是生成的Debug版本(实际上我两个版本都编译了)
msbuild ALL_BUILD.vcxproj /maxcpucount:4 /p:Configuration=Release
# 安装
msbuild INSTALL.vcxproj /p:Configuration=Release

3、指定下面几个选项后,继续使用cmake命令生成

-DgRPC_CARES_PROVIDER="package" \
-DProtobuf_INCLUDE_DIR="E:/complier/x64-windows-static/include"  \
-DProtobuf_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotobuf.lib" \
-DProtobuf_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotobufd.lib" \
-DProtobuf_LITE_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotobuf-lite.lib" \
-DProtobuf_LITE_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotobuf-lited.lib" \
-DProtobuf_PROTOC_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotoc.lib" \
-DProtobuf_PROTOC_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotocd.lib" \
-DProtobuf_PROTOC_EXECUTABLE="E:/complier/x64-windows-static/bin/protoc.exe" \
-DProtobuf_SRC_ROOT_FOLDER="third_party/protobuf" \

2.1.5、继续,报SSL警告
CMake Warning at cmake/ssl.cmake:37 (message):
  gRPC_INSTALL will be forced to FALSE because gRPC_SSL_PROVIDER is "module"
Call Stack (most recent call first):
  CMakeLists.txt:143 (include)

这里可以忽略它,不使用SSL即可。也可以编译openssl库,然后指定以下选项后,继续执行cmake。

-DgRPC_SSL_PROVIDER="package" \
-DOPENSSL_INCLUDE_DIR="E:/complier/x64-windows-static/include" \
-DSSL_EAY_DEBUG="E:/complier/x64-windows-static/debug/lib/ssleay32.lib" \
-DLIB_EAY_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/libeay32.lib" \
-DLIB_EAY_RELEASE="E:/complier/x64-windows-static/lib/libeay32.lib" \
-DLIB_EAY_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libeay32.lib" \
-DLIB_EAY_DEBUG="E:/complier/x64-windows-static/debug/lib/libeay32.lib" \
-DSSL_EAY_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/ssleay32.lib" \
-DSSL_EAY_RELEASE="E:/complier/x64-windows-static/lib/ssleay32.lib" \
-DSSL_EAY_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/ssleay32.lib" \

编译openssl也很简单,可以参考 VS2015编译Openssl-1.1.0f 的内容,也可以直接使用vcpkg安装。

我这里没有编译了,直接使用vcpkg安装后,备份出来后使用。

vcpkg install openssl:x64-windows-static
vcpkg export openssl:x64-windows-static --zip --output=openssl.x64-windows-static

2.1.6、VS2015环境下可能遇到的错误

下面两个错误,是我在指定编译器为VS2015的时候遇到的,VS2019并没有遇到。

1、继续,报错误INTTYPES_FORMAT没有设置

CMake Error at third_party/gflags/CMakeLists.txt:286 (message):
  Do not know how to define a 32-bit integer quantity on your system! Neither
  uint32_t, u_int32_t, nor __int32 seem to be available.  Set
  [GFLAGS_]INTTYPES_FORMAT to either C99, BSD, or VC7 and try again.

这里可以打开grpc/third_party/gflags/CMakeLists.txt 查看

gflagsCMakeLists

这里在cmake命令参数中添加一个变量,指定使用C99或者VC7格式。

-DINTTYPES_FORMAT:STRING=C99

2、继续,报benchmark相关错误

-- git Version: v1.4.0-e776aa02-dirty
-- Version: 1.4.0
-- Performing Test HAVE_STD_REGEX -- failed to compile
-- Performing Test HAVE_GNU_POSIX_REGEX -- failed to compile
-- Performing Test HAVE_POSIX_REGEX -- failed to compile
CMake Error at third_party/benchmark/CMakeLists.txt:229 (message):
  Failed to determine the source files for the regular expression backend

我直接打开grpc/third_party/benchmark/CMakeLists.txt做了如下修改,然后继续执行cmake,就通过了

benchmarkCMakeLists


完整的cmake执行命令如下:
cmake —G "Visual Studio 16 2019" -DCMAKE_CONFIGURATION_TYPES=Release  -DGO_EXECUTABLE="C:/Go/bin/go.exe" -DCMAKE_INSTALL_PREFIX="E:/complier/grpc/install" -DgRPC_MSVC_STATIC_RUNTIME=1 -DgRPC_ZLIB_PROVIDER=package -DZLIB_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/zlibd.lib" -DZLIB_INCLUDE_DIR="E:/complier/x64-windows-static/include" -DZLIB_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/zlib.lib" -DgRPC_CARES_PROVIDER=package  -Dc-ares_DIR="E:/complier/x64-windows-static/lib/cmake/c-ares" -DgRPC_PROTOBUF_PROVIDER="package" -DProtobuf_INCLUDE_DIR="E:/complier/x64-windows-static/include"  -DProtobuf_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotobuf.lib" -DProtobuf_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotobufd.lib" -DProtobuf_LITE_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotobuf-lite.lib" -DProtobuf_LITE_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotobuf-lited.lib" -DProtobuf_PROTOC_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libprotoc.lib" -DProtobuf_PROTOC_LIBRARY_DEBUG="E:/complier/x64-windows-static/lib/libprotocd.lib" -DProtobuf_PROTOC_EXECUTABLE="E:/complier/x64-windows-static/bin/protoc.exe" -DProtobuf_SRC_ROOT_FOLDER="third_party/protobuf" -DgRPC_SSL_PROVIDER="package" -DOPENSSL_INCLUDE_DIR="E:/complier/x64-windows-static/include" -DSSL_EAY_DEBUG="E:/complier/x64-windows-static/debug/lib/ssleay32.lib" -DLIB_EAY_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/libeay32.lib" -DLIB_EAY_RELEASE="E:/complier/x64-windows-static/lib/libeay32.lib" -DLIB_EAY_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/libeay32.lib" -DLIB_EAY_DEBUG="E:/complier/x64-windows-static/debug/lib/libeay32.lib" -DSSL_EAY_LIBRARY_DEBUG="E:/complier/x64-windows-static/debug/lib/ssleay32.lib" -DSSL_EAY_RELEASE="E:/complier/x64-windows-static/lib/ssleay32.lib" -DSSL_EAY_LIBRARY_RELEASE="E:/complier/x64-windows-static/lib/ssleay32.lib" -DINTTYPES_FORMAT:STRING=C99  ..\grpc

2.2、使用msbuild工具进行编译

直接使用VS2019命令行工具进入生成的工程目录,执行下面命令即可

msbuild /maxcpucount:4 /p:Configuration=Release ALL_BUILD.vcxproj

编译耗时会比较久。

注意,上面编译第三方库的时候,一定要指定使用使用静态运行时(/MT),否则可能这里编译完链接会出错。或者可以干脆不编译静态库,全部编译为动态库,使用默认的(/MD)参数。

3、linux下编译

这里只做了CentOS7下的编译,如果是其他发行版应该更简单。

 uname -a
Linux localhost.localdomain 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

下面很多操作需要root权限,下面的命令没有使用sudo,需要根据情况添加。

3.1 CentO S下基本编译环境安装

这里主要是安装以下gcc编译器和cmake等工具。

# 安装cmake3、make(默认cmake版本太低)
yum -y install epel-release # 默认情况下是没有cmake3的,必须安装epel(企业版 Linux 附加软件包)源才行
yum install cmake3 make
# 配置SLC源
yum install centos-release-scl-rh centos-release-scl
yum check-update
# 安装devtoolset,因为默认安装gcc版本为4.8.5,版本太低
yum install devtoolset-8-gcc devtoolset-8-gcc-c++
# 安装完成之后执行下面语句,把相关环境变量导入
source /opt/rh/devtoolset-4/enable
# 也可以把上面文件的内容追加到/etc/profile中,就不用每次都要导入了
# cat /opt/rh/devtoolset-4/enable >> /etc/profile

# 安装go,也可直接下载安装包
yum install go-toolset-7-golang
# 启用go,这里和上面一样,也可以直接写到/etc/profile里面去
source /opt/rh/go-toolset-7/enable

devtoolset-8

3.2 编译三方依赖库

1、编译安装zlib

cd grpc/third_party/zlib
# 编译安装
./configure --prefix=/usr
make && make install

2、编译安装c-ares

git clone https://github.com/c-ares/c-ares.git
cd c-ares
# 可以不下载,直接  cd grpc/third_party/cares/cares
cmake3 . -DCMAKE_INSTALL_PREFIX=/usr
make && make install

3、编译安装protobuf

# 进入grpc下源码目录,或者下载 git clone https://github.com/protocolbuffers/protobuf.git
cd grpc/third_party/protobuf
# 同步下子模块
git submodule update --init --recursive
# 创建cmake构建目录
mkdir build && cd build
# 生成Makefile
cmake3 -DCMAKE_INSTALL_PREFIX=/usr ../cmake
# 编译安装
make -j4
make install

4、编译安装openssl

这个也可以不安装的,不影响编译。

# 下载安装包
wget https://www.openssl.org/source/openssl-1.0.2s.tar.gz
tar -xzf openssl-1.0.2s.tar.gz
cd openssl-1.0.2s
# 配置编译参数
./config --prefix=/usr
# 编译安装
make -j4
make install

注意,因为我上面编译的几个库都是安装到/usr目录的,所以下面cmake参数中没有再指定。如果是安装到自定义的目录的,则需要像上面Windows下编译一样,指定相关的路径。

3.3编译grpc

这里没什么直接使用

# 创建cmake构建目录
mkdir build && cd build
# 执行cmake命令,生成Makefile脚本
cmake3 ../grpc -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/home/pure/grpc/install -DgRPC_ZLIB_PROVIDER=package -DgRPC_CARES_PROVIDER=package -DgRPC_PROTOBUF_PROVIDER=package -DgRPC_SSL_PROVIDER=package
# 编译
make -j4

builddone

转载于:https://www.cnblogs.com/oloroso/p/11121922.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 C++ gRPC 通信程序,并使用 CMake 进行编译: 首先,需要安装 gRPC 和 Protobuf。 接着,创建一个名为 `hello.proto` 的 Protobuf 文件,内容如下: ```protobuf syntax = "proto3"; package helloworld; service Greeter { rpc SayHello (HelloRequest) returns (HelloReply) {} } message HelloRequest { string name = 1; } message HelloReply { string message = 1; } ``` 然后,在命令行中进入到包含 `hello.proto` 文件的目录,并执行以下命令: ```bash $ grpc_cpp_plugin --cpp_out=. --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` hello.proto ``` 该命令将生成 `hello.grpc.pb.h` 和 `hello.grpc.pb.cc` 文件。 接着,创建一个名为 `server.cpp` 的服务器端代码,内容如下: ```cpp #include <iostream> #include <memory> #include <string> #include <grpc++/grpc++.h> #include "hello.grpc.pb.h" using grpc::Server; using grpc::ServerBuilder; using grpc::ServerContext; using grpc::Status; using helloworld::Greeter; using helloworld::HelloRequest; using helloworld::HelloReply; // 实现 Greeter 服务类 class GreeterServiceImpl final : public Greeter::Service { Status SayHello(ServerContext* context, const HelloRequest* request, HelloReply* reply) override { std::string prefix("Hello "); reply->set_message(prefix + request->name()); return Status::OK; } }; // 启动服务器 void RunServer() { std::string server_address("0.0.0.0:50051"); GreeterServiceImpl service; // 构建服务器 ServerBuilder builder; builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); builder.RegisterService(&service); std::unique_ptr<Server> server(builder.BuildAndStart()); std::cout << "Server listening on " << server_address << std::endl; // 等待服务器关闭 server->Wait(); } int main(int argc, char** argv) { RunServer(); return 0; } ``` 该代码将创建一个 `GreeterServiceImpl` 类,实现 `Greeter` 服务类,然后启动一个 gRPC 服务器,监听在 `0.0.0.0:50051` 地址。 接着,创建一个名为 `client.cpp` 的客户端代码,内容如下: ```cpp #include <iostream> #include <memory> #include <string> #include <grpc++/grpc++.h> #include "hello.grpc.pb.h" using grpc::Channel; using grpc::ClientContext; using grpc::Status; using helloworld::Greeter; using helloworld::HelloRequest; using helloworld::HelloReply; class GreeterClient { public: GreeterClient(std::shared_ptr<Channel> channel) : stub_(Greeter::NewStub(channel)) {} std::string SayHello(const std::string& user) { HelloRequest request; request.set_name(user); HelloReply reply; ClientContext context; // 调用远程服务 Status status = stub_->SayHello(&context, request, &reply); if (status.ok()) { return reply.message(); } else { return "RPC failed"; } } private: std::unique_ptr<Greeter::Stub> stub_; }; int main(int argc, char** argv) { GreeterClient greeter(grpc::CreateChannel( "localhost:50051", grpc::InsecureChannelCredentials())); std::string user("world"); std::string reply = greeter.SayHello(user); std::cout << "Greeter received: " << reply << std::endl; return 0; } ``` 该代码将创建一个 `GreeterClient` 类,用于调用远程服务。它将连接到 `localhost:50051` 地址,并调用 `SayHello` 方法,将 `world` 作为参数传递给远程服务。 最后,创建一个名为 `CMakeLists.txt` 的 CMake 文件,内容如下: ```cmake cmake_minimum_required(VERSION 3.0.0) project(grpc-example VERSION 0.1.0) set(CMAKE_CXX_STANDARD 11) add_executable(server server.cpp hello.grpc.pb.cc hello.pb.cc) add_executable(client client.cpp hello.grpc.pb.cc hello.pb.cc) find_package(Protobuf REQUIRED) find_package(gRPC REQUIRED) target_include_directories(server PUBLIC ${Protobuf_INCLUDE_DIRS}) target_link_libraries(server ${Protobuf_LIBRARIES} gRPC::grpc++) target_include_directories(client PUBLIC ${Protobuf_INCLUDE_DIRS}) target_link_libraries(client ${Protobuf_LIBRARIES} gRPC::grpc++) ``` 该文件将编译 `server.cpp` 和 `client.cpp`,并链接 Protobuf 和 gRPC 库。 在命令行中进入到包含 `CMakeLists.txt` 文件的目录,并执行以下命令: ```bash $ mkdir build $ cd build $ cmake .. $ make ``` 该命令将编译并链接代码,生成 `server` 和 `client` 可执行文件。 现在,可以在命令行中运行 `./server` 启动服务器,然后在另一个终端窗口中运行 `./client` 调用远程服务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值