win10系统 C++环境 安装编译GRPC

第一步 下载源码、更新、cmake编译:

为了依赖的成功安装,采用gitee进行下载与更新。记得需要安装git软件。
安装命令:
在自己指定的目录下,鼠标右键,选择 git Bash Here
打开命令行

git clone -b v1.34.0 https://gitee.com/mirrors/grpc-framework.git grpc

在grpc的目录下修改配置文件:.gitmodules
在这里插入图片描述

复制下面内容替换.gitmodules内容:

[submodule "third_party/zlib"]path = third_party/zlib#url = https://github.com/madler/zliburl = https://gitee.com/mirrors/zlib.git# When using CMake to build, the zlib submodule ends up with a# generated file that makes Git consider the submodule dirty. This# state can be ignored for day-to-day development on gRPC.ignore = dirty
[submodule "third_party/protobuf"]path = third_party/protobuf#url = https://github.com/google/protobuf.giturl = https://gitee.com/local-grpc/protobuf.git
[submodule "third_party/googletest"]path = third_party/googletest#url = https://github.com/google/googletest.giturl = https://gitee.com/local-grpc/googletest.git
[submodule "third_party/benchmark"]path = third_party/benchmark#url = https://github.com/google/benchmarkurl = https://gitee.com/mirrors/google-benchmark.git
[submodule "third_party/boringssl-with-bazel"]path = third_party/boringssl-with-bazel#url = https://github.com/google/boringssl.giturl = https://gitee.com/mirrors/boringssl.git
[submodule "third_party/re2"]path = third_party/re2#url = https://github.com/google/re2.giturl = https://gitee.com/local-grpc/re2.git
[submodule "third_party/cares/cares"]path = third_party/cares/cares#url = https://github.com/c-ares/c-ares.giturl = https://gitee.com/mirrors/c-ares.gitbranch = cares-1_12_0
[submodule "third_party/bloaty"]path = third_party/bloaty#url = https://github.com/google/bloaty.giturl = https://gitee.com/local-grpc/bloaty.git
[submodule "third_party/abseil-cpp"]path = third_party/abseil-cpp#url = https://github.com/abseil/abseil-cpp.giturl = https://gitee.com/mirrors/abseil-cpp.gitbranch = lts_2020_02_25
[submodule "third_party/envoy-api"]path = third_party/envoy-api#url = https://github.com/envoyproxy/data-plane-api.giturl = https://gitee.com/local-grpc/data-plane-api.git
[submodule "third_party/googleapis"]path = third_party/googleapis#url = https://github.com/googleapis/googleapis.giturl = https://gitee.com/mirrors/googleapis.git
[submodule "third_party/protoc-gen-validate"]path = third_party/protoc-gen-validate#url = https://github.com/envoyproxy/protoc-gen-validate.giturl = https://gitee.com/local-grpc/protoc-gen-validate.git
[submodule "third_party/udpa"]path = third_party/udpa#url = https://github.com/cncf/udpa.giturl = https://gitee.com/local-grpc/udpa.git
[submodule "third_party/libuv"]path = third_party/libuv#url = https://github.com/libuv/libuv.giturl = https://gitee.com/mirrors/libuv.git

在grpc目录下,在git 上使用更新命令

cd grpc
git submodule update --init

使用cmake对grpc进行编译。
在这里插入图片描述

编译结束后,使用vs打开目录中的grpc.sln文件
在这里插入图片描述
右键ALL——BUILD,点击重新生成。
在这里插入图片描述

第二步 编写 .proto 文件

新建一个C++控制台项目,新建 demo.proto 文件
文件内容:


syntax = "proto3";package hello;service Greeter {rpc SayHello (HelloRequest) returns (HelloReply) {}
}message HelloRequest {string message = 1;
}message HelloReply {string message = 1;
}

在资源文件中右键添加现有项,将demo.proto 文件添加进来。
demo.proto里面不能有中文或者utf-8的格式

第三步 生成头文件与源文件

在自己新建的控制台项目中,按住Shift + 右键 调处控制台
接下来我们利用grpc编译后生成的proc.exe生成proto的头文件和源文件

D:\cpp_grpc\visualpro\third_party\protobuf\Debug\protoc.exe -I="." --grpc_out="." --plugin=protoc-gen-grpc="D:\cpp_grpc\visualpro\Debug\grpc_cpp_plugin.exe" "demo.proto"
D:\cpp_grpc\visualpro\third_party\protobuf\Debug\protoc.exe --cpp_out=. "demo.proto"

在这里插入图片描述
生成出来的文件:

第四步 配置VS

将这4个都右键添加现有项的方法添加到C++的控制台项目中去。
开始配置VS
右键项目,点击属性,选择c/c++,选择常规,选择附加包含目录

D:\cpp_grpc\grpc\third_party\re2
D:\cpp_grpc\grpc\third_party\address_sorting\include
D:\cpp_grpc\grpc\third_party\abseil-cpp
D:\cpp_grpc\grpc\third_party\protobuf\src
D:\cpp_grpc\grpc\include

在这里插入图片描述

接下来配置库路径, 在链接器常规选项下,点击附加库目录,添加我们需要的库目录。

右键项目,点击属性,选择链接器,选择附加库目录

D:\cpp_grpc\visualpro\third_party\re2\Debug
D:\cpp_grpc\visualpro\third_party\abseil-cpp\absl\types\Debug
D:\cpp_grpc\visualpro\third_party\abseil-cpp\absl\synchronization\Debug
D:\cpp_grpc\visualpro\third_party\abseil-cpp\absl\status\Debug
D:\cpp_grpc\visualpro\third_party\abseil-cpp\absl\random\Debug
D:\cpp_grpc\visualpro\third_party\abseil-cpp\absl\flags\Debug
D:\cpp_grpc\visualpro\third_party\abseil-cpp\absl\debugging\Debug
D:\cpp_grpc\visualpro\third_party\abseil-cpp\absl\container\Debug
D:\cpp_grpc\visualpro\third_party\abseil-cpp\absl\hash\Debug
D:\cpp_grpc\visualpro\third_party\boringssl-with-bazel\Debug
D:\cpp_grpc\visualpro\third_party\abseil-cpp\absl\numeric\Debug
D:\cpp_grpc\visualpro\third_party\abseil-cpp\absl\time\Debug
D:\cpp_grpc\visualpro\third_party\abseil-cpp\absl\base\Debug
D:\cpp_grpc\visualpro\third_party\abseil-cpp\absl\strings\Debug
D:\cpp_grpc\visualpro\third_party\protobuf\Debug
D:\cpp_grpc\visualpro\third_party\zlib\Debug
D:\cpp_grpc\visualpro\Debug
D:\cpp_grpc\visualpro\third_party\cares\cares\lib\Debug

在这里插入图片描述
另外,我们虽然配置了库目录,但还要将要使用的库链接到项目
点击链接器,选择输入选项,点击附加依赖项,添加依赖的库名字

libprotobufd.lib
gpr.lib
grpc.lib
grpc++.lib
grpc++_reflection.lib
address_sorting.lib
ws2_32.lib
cares.lib
zlibstaticd.lib
upb.lib
ssl.lib
crypto.lib
absl_bad_any_cast_impl.lib
absl_bad_optional_access.lib
absl_bad_variant_access.lib
absl_base.lib
absl_city.lib
absl_civil_time.lib
absl_cord.lib
absl_debugging_internal.lib
absl_demangle_internal.lib
absl_examine_stack.lib
absl_exponential_biased.lib
absl_failure_signal_handler.lib
absl_flags.lib
absl_flags_config.lib
absl_flags_internal.lib
absl_flags_marshalling.lib
absl_flags_parse.lib
absl_flags_program_name.lib
absl_flags_usage.lib
absl_flags_usage_internal.lib
absl_graphcycles_internal.lib
absl_hash.lib
absl_hashtablez_sampler.lib
absl_int128.lib
absl_leak_check.lib
absl_leak_check_disable.lib
absl_log_severity.lib
absl_malloc_internal.lib
absl_periodic_sampler.lib
absl_random_distributions.lib
absl_random_internal_distribution_test_util.lib
absl_random_internal_pool_urbg.lib
absl_random_internal_randen.lib
absl_random_internal_randen_hwaes.lib
absl_random_internal_randen_hwaes_impl.lib
absl_random_internal_randen_slow.lib
absl_random_internal_seed_material.lib
absl_random_seed_gen_exception.lib
absl_random_seed_sequences.lib
absl_raw_hash_set.lib
absl_raw_logging_internal.lib
absl_scoped_set_env.lib
absl_spinlock_wait.lib
absl_stacktrace.lib
absl_status.lib
absl_strings.lib
absl_strings_internal.lib
absl_str_format_internal.lib
absl_symbolize.lib
absl_synchronization.lib
absl_throw_delegate.lib
absl_time.lib
absl_time_zone.lib
absl_statusor.lib
re2.lib

在这里插入图片描述

第五步 编写服务端代码:

编写服务端代码:

#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#include "demo.grpc.pb.h"
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using hello::HelloRequest;
using hello::HelloReply;
using hello::Greeter;
// Logic and data behind the server's behavior.
class GreeterServiceImpl final : public Greeter::Service {Status SayHello(ServerContext* context, const HelloRequest* request,HelloReply* reply) override {std::string prefix("llfc grpc server has received :  ");reply->set_message(prefix + request->message());return Status::OK;}
};
void RunServer() {std::string server_address("127.0.0.1:50051");GreeterServiceImpl service;ServerBuilder builder;// Listen on the given address without any authentication mechanism.// 监听给定的地址builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());// Register "service" as the instance through which we'll communicate with// clients. In this case it corresponds to an *synchronous* service.builder.RegisterService(&service);// Finally assemble the server.std::unique_ptr<Server> server(builder.BuildAndStart());std::cout << "Server listening on " << server_address << std::endl;// Wait for the server to shutdown. Note that some other thread must be// responsible for shutting down the server for this call to ever return.server->Wait();
}
int main(int argc, char** argv) {RunServer();return 0;
}

在这里插入图片描述
写完使用x64平台运行一下。

第六步 编写客户端代码:

右键添加新建项目,grpcclient客户端
为了不重新配置一边,找到当前路径
在这里插入图片描述

在这里插入图片描述
直接复制过去,就完成了配置。
还要复制以下这4个文件到客户端,再使用右键添加现有项,将这4个添加进去。
在这里插入图片描述
客户端代码:

#include <string>
#include <iostream>
#include <memory>
#include <grpcpp/grpcpp.h>
#include "demo.grpc.pb.h"
using grpc::ClientContext;
using grpc::Channel;
using grpc::Status;
using hello::HelloReply;
using hello::HelloRequest;
using hello::Greeter;
// static method : Greeter::NewStub
class FCClient {
public:FCClient(std::shared_ptr<Channel> channel):stub_(Greeter::NewStub(channel)) {}std::string SayHello(std::string name) {ClientContext context;HelloReply reply;HelloRequest request;request.set_message(name);Status status = stub_->SayHello(&context, request, &reply);if (status.ok()) {return reply.message();}else {return "failure " + status.error_message();}}
private:std::unique_ptr<Greeter::Stub> stub_;
};
int main(int argc, char* argv[]) {auto channel = grpc::CreateChannel("127.0.0.1:50051", grpc::InsecureChannelCredentials());FCClient client(channel);// block until get result from RPC serverstd::string result = client.SayHello("hello , llfc.club !");printf("get result [%s]\n", result.c_str());return 0;
}

运行效果:
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/143614.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

AVL Cruise 2020.1 安装教程

文章目录 安装包安装破解 安装包 链接&#xff1a;https://pan.baidu.com/s/1GxbeDj_SyvKFyPeTsstvTQ?pwd6666 提取码&#xff1a;6666 安装 安装文件&#xff1a; 双击setup.exe&#xff1a; 一直netx&#xff0c;中间要修改两次路径&#xff0c;第一次是安装位置&#xf…

xorm数据库操作之Join、Union

golang的数据库操作xorm使用起来非常方便&#xff0c;不用再自己写SQl语句&#xff0c;而且xorm自己给我们做了SQL防注入等操作&#xff0c;用起来既方便又安全。此次文章我不会记录xorm的基本操作&#xff0c;我值记录一些特殊用法问题&#xff0c;包括动态创建表单、基于xorm…

探索科技前沿,科东带你深度解读2023上海工博会

科东软件诚邀您亲临现场 感受软件定义控制新趋势 这是一场集结全球创新力量与科技创新成果的璀璨盛宴&#xff0c;也是推动未来科技与产业发展的新型工业盛会&#xff0c;更是一次助力构建数字化、低碳化发展格局的重量级活动。 2023年9月19日&#xff0c;备受瞩目的第23届中国…

133.【MySQL_运维篇】

MySQL_运维 (一)、日志 ⭐1.日志_错误日志 (ERROR-LOG)(1).错误日志_介绍(2).错误日志_示列 2.日志_二进制日志 (BINARY-LOG)(1).二进制日志_介绍(2).二进制日志_作用(3).二进制日志_格式(4).二进制日志_查看 (CMD)(5).二进制日志_删除 3.日志_查询日志 (GENERAL-LOG)(1).开启_…

基于PLE结合卡尔曼滤波的RSSI定位算法matlab仿真

目录 1.算法运行效果图预览 2.算法运行软件版本 3.部分核心程序 4.算法理论概述 5.算法完整程序工程 1.算法运行效果图预览 2.算法运行软件版本 MATLAB2022a 3.部分核心程序 ............................................................... for Num_xb Num_xb2Num_…

现在的国内MBA教育是否同质化太严重?

如今在国内的MBA教育领域可以说是一片欣欣向荣&#xff0c;两百余所高校开设MBA项目招生&#xff0c;而报考市场也随着时代的发展持续升温&#xff0c;但是在这背后也存在一些问题伴随发生&#xff0c;其中就是MBA项目的同质化与跟风化趋势越来越明显&#xff0c;主要有以下几个…

Elasticsearch:什么是向量和向量存储数据库,我们为什么关心?

Elasticsearch 从 7.3 版本开始支持向量搜索。从 8.0 开始支持带有 HNSW 的 ANN 向量搜索。目前 Elasticsearch 已经是全球下载量最多的向量数据库。它允许使用密集向量和向量比较来搜索文档。 矢量搜索在人工智能和机器学习领域有许多重要的应用。 有效存储和检索向量的数据库…

MySQL ——多表连接查询

一、&#xff08;左、右和全&#xff09;连接概念 内连接&#xff1a; 假设A和B表进行连接&#xff0c;使用内连接的话&#xff0c;凡是A表和B表能够匹配上的记录查询出来。A和B两张表没有主付之分&#xff0c;两张表是平等的。 关键字&#xff1a;inner join on 语句&#xf…

Android ConstraintLayout app:layout_constraintHorizontal_weight

Android ConstraintLayout app:layout_constraintHorizontal_weight <?xml version"1.0" encoding"utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android"http://schemas.android.com/apk/res/android"xmlns:…

软件设计模式系列之十一——装饰模式

当谈到设计软件系统时&#xff0c;经常需要考虑如何使系统更加灵活、可扩展和易维护。设计模式是一种被广泛采用的方法&#xff0c;用于解决常见的设计问题&#xff0c;并提供了一套可重用的解决方案。装饰模式&#xff08;Decorator Pattern&#xff09;是一种结构型设计模式&…

crypto:RSA

题目 利用代码跑一下解码 import gmpy2 e 17 p 473398607161 q 4511491 d gmpy2.invert(e,(p-1)*(q-1)) print(d)总结 RSA&#xff08;Rivest-Shamir-Adleman&#xff09;是一种非对称加密算法&#xff0c;常用于数据加密和数字签名。它基于两个大素数的乘积难以分解的数…

python实现命令tree的效果

把所有的文档都传到了git上,但是内容过多找起来不方便,突发奇想如果能在readme中,递归列出所有文件同时添加上对应的地址,这样只需要搜索到对应的文件点击就能跳转过去了… 列出文件总得有个显示格式,所以就按照tree的来了… 用python实现命令tree的效果 首先,这是tree的效果…

ATA-8000系列射频功率放大器——应用场景介绍

ATA-8000系列是一款射频功率放大器。其P1dB输出功率500W&#xff0c;饱和输出功率最大1000W。增益数控可调&#xff0c;一键保存设置&#xff0c;提供了方便简洁的操作选择&#xff0c;可与主流的信号发生器配套使用&#xff0c;实现射频信号的放大。 图&#xff1a;ATA-8000系…

算法 杨辉三角求解 java打印杨辉三角 多路递归打印杨辉三角 递归优化杨辉三角 记忆法优化递归 帕斯卡三角形 算法(十二)

1. 杨辉三角&#xff1a; 是二项式系数在三角形中的一种几何排列&#xff0c;中国南宋数学家杨辉1261年所著的《详解九章算法》一书中出现。在欧洲&#xff0c;帕斯卡&#xff08;1623----1662&#xff09;在1654年发现这一规律&#xff0c;所以这个表又叫做帕斯卡三角形。帕斯…

41. Linux系统配置FTP服务器并在QT中使用QFtp实现文件上传

1. 说明 这篇博客主要记录一些在Linux系统中搭建FTP服务器时踩过的一些坑,以及在使用QFtp上传文件时需要注意的问题。 2. FTP环境搭建 在linux系统中,需要安装vsftpd,可以在终端中输入下面的命令进行安装: sudo apt-get install vsftpd使用上述命令安装后,系统中会有一…

Cannot find module ‘core-js/modules/es6.regexp.constructor‘

npm run dev 之后报如下错误 解决方法&#xff1a;npm install core-js2 如果超时或者下载时间慢可以尝试 用cnpm install core-js2

记一次hyperf框架封装swoole自定义进程

背景 公司准备引入swoole和rabbitmq来处理公司业务。因此&#xff0c;我引入hyperf框架&#xff0c;想用swoole的多进程来实现。 自定义启动服务封装 <?php /*** 进程启动服务【manager】*/ declare(strict_types1);namespace App\Command;use Swoole; use Swoole\Proce…

Android 编译插桩操纵字节码

本文讲解如何编译插桩操纵字节码。 就使用 ASM 来实现简单的编译插桩效果&#xff0c;通过插桩实现在每一个 Activity 打开时输出相应的 log 日志。实现思路 过程主要包含两步&#xff1a; 1、遍历项目中所有的 .class 文件​ 如何找到项目中编译生成的所有 .class 文件&#…

pycharm中恢复原始界面布局_常用快捷键_常用设置

文章目录 1 恢复默认布局1 .1直接点击file→Manage IDE Settings→Restore Default Settings&#xff08;如下图所示&#xff09;&#xff1a;1.2 直接点击Restore and Restart&#xff0c; 然后Pycharm就会自动重启&#xff0c;重启之后的界面就是最原始的界面了 2 改变主题2.…

时序预测 | MATLAB实现NGO-GRU北方苍鹰算法优化门控循环单元时间序列预测

时序预测 | MATLAB实现NGO-GRU北方苍鹰算法优化门控循环单元时间序列预测 目录 时序预测 | MATLAB实现NGO-GRU北方苍鹰算法优化门控循环单元时间序列预测预测效果基本介绍程序设计参考资料 预测效果 基本介绍 MATLAB实现NGO-GRU北方苍鹰算法优化门控循环单元时间序列预测&#…