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

这篇具有很好参考价值的文章主要介绍了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
win10系统 C++环境 安装编译GRPC,C++ 基础,c++,开发语言

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

[submodule "third_party/zlib"]
    path = third_party/zlib
    #url = https://github.com/madler/zlib
    url = 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.git
    url = https://gitee.com/local-grpc/protobuf.git
[submodule "third_party/googletest"]
    path = third_party/googletest
    #url = https://github.com/google/googletest.git
    url = https://gitee.com/local-grpc/googletest.git
[submodule "third_party/benchmark"]
    path = third_party/benchmark
    #url = https://github.com/google/benchmark
    url = 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.git
    url = https://gitee.com/mirrors/boringssl.git
[submodule "third_party/re2"]
    path = third_party/re2
    #url = https://github.com/google/re2.git
    url = 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.git
    url = https://gitee.com/mirrors/c-ares.git
    branch = cares-1_12_0
[submodule "third_party/bloaty"]
    path = third_party/bloaty
    #url = https://github.com/google/bloaty.git
    url = https://gitee.com/local-grpc/bloaty.git
[submodule "third_party/abseil-cpp"]
    path = third_party/abseil-cpp
    #url = https://github.com/abseil/abseil-cpp.git
    url = https://gitee.com/mirrors/abseil-cpp.git
    branch = lts_2020_02_25
[submodule "third_party/envoy-api"]
    path = third_party/envoy-api
    #url = https://github.com/envoyproxy/data-plane-api.git
    url = https://gitee.com/local-grpc/data-plane-api.git
[submodule "third_party/googleapis"]
    path = third_party/googleapis
    #url = https://github.com/googleapis/googleapis.git
    url = 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.git
    url = https://gitee.com/local-grpc/protoc-gen-validate.git
[submodule "third_party/udpa"]
    path = third_party/udpa
    #url = https://github.com/cncf/udpa.git
    url = https://gitee.com/local-grpc/udpa.git
[submodule "third_party/libuv"]
    path = third_party/libuv
    #url = https://github.com/libuv/libuv.git
    url = https://gitee.com/mirrors/libuv.git

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

cd grpc
git submodule update --init

使用cmake对grpc进行编译。
win10系统 C++环境 安装编译GRPC,C++ 基础,c++,开发语言

编译结束后,使用vs打开目录中的grpc.sln文件
win10系统 C++环境 安装编译GRPC,C++ 基础,c++,开发语言
右键ALL——BUILD,点击重新生成。
win10系统 C++环境 安装编译GRPC,C++ 基础,c++,开发语言

第二步 编写 .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"

win10系统 C++环境 安装编译GRPC,C++ 基础,c++,开发语言
生成出来的文件:
win10系统 C++环境 安装编译GRPC,C++ 基础,c++,开发语言

第四步 配置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

win10系统 C++环境 安装编译GRPC,C++ 基础,c++,开发语言

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

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

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

win10系统 C++环境 安装编译GRPC,C++ 基础,c++,开发语言
另外,我们虽然配置了库目录,但还要将要使用的库链接到项目
点击链接器,选择输入选项,点击附加依赖项,添加依赖的库名字

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

win10系统 C++环境 安装编译GRPC,C++ 基础,c++,开发语言

第五步 编写服务端代码:

编写服务端代码:

#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;
}

win10系统 C++环境 安装编译GRPC,C++ 基础,c++,开发语言
写完使用x64平台运行一下。

第六步 编写客户端代码:

右键添加新建项目,grpcclient客户端
为了不重新配置一边,找到当前路径
win10系统 C++环境 安装编译GRPC,C++ 基础,c++,开发语言

win10系统 C++环境 安装编译GRPC,C++ 基础,c++,开发语言
直接复制过去,就完成了配置。
还要复制以下这4个文件到客户端,再使用右键添加现有项,将这4个添加进去。
win10系统 C++环境 安装编译GRPC,C++ 基础,c++,开发语言
客户端代码:

#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 server
    std::string result = client.SayHello("hello , llfc.club !");
    printf("get result [%s]\n", result.c_str());
    return 0;
}

运行效果:
win10系统 C++环境 安装编译GRPC,C++ 基础,c++,开发语言文章来源地址https://www.toymoban.com/news/detail-730882.html

到了这里,关于win10系统 C++环境 安装编译GRPC的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Ubuntu【系统环境下】【编译安装OpenCV】【C++调用系统opencv库】

    本人需要用C++写代码,调用OpenCV库,且要求OpenCV版本号大于4.1.0 由于使用的是18.04的版本,所以apt安装OpenCV的版本始终是3.2.0,非常拉胯!!! 所以只能重新编译安装OpenCV 查看当前C++调用的OpenCV代码 apt 安装 OpenCV sudo apt install libopencv-dev apt 安装 OpenCV 并与系统python环境关联

    2024年02月11日
    浏览(36)
  • VMware Workstation安装银河麒麟V10系统,配置gcc交叉编译环境(鲲鹏服务器)

           在一种计算机环境中运行的编译程序,能编译出在另外一种环境下运行的代码,我们就称这种编译器支持交叉编译。这个编译过程就叫交叉编译。简单地说,就是在一个平台上生成另一个平台上的可执行代码。       要进行交叉编译,我们需要在主机平台上安装对应

    2024年02月04日
    浏览(105)
  • Win10 OpenCV编译安装CUDA版本

    Win10 + Microsoft Visual Studio Community 2017 + CUDA11.3 + CUDNN8.2 + RTX GeForce 3090 + OpenCV4.5.3 前往官网下载Visual Studio Installer即可,做如下勾选,安装即可 完成后,查看环境变量,将MSVC编译器地址加入环境变量 前往官网下载CUDA和对应的CUDNN,切记一定要对应CUDNN和CUDA版本,根据提示一步一

    2024年02月06日
    浏览(38)
  • win10下wsl2使用记录(系统迁移到D盘、配置国内源、安装conda环境、配置pip源、安装pytorch-gpu环境、安装paddle-gpu环境)

    安装好后环境测试效果如下,支持命令nvidia-smi,不支持命令nvcc,usr/local目录下没有cuda文件夹。 系统迁移到非C盘 wsl安装的系统默认在c盘,为节省c盘空间进行迁移。 1、输出 wsl -l 查看要迁移的系统名称 2、执行导出命令: wsl --export Ubuntu-20.04 ./Ubuntu-20.04.tar ,以下命令将系统

    2024年02月20日
    浏览(45)
  • windows10系统下安装opencv4.7.0+VSCode+(C++)环境搭建

    windows10系统下安装opencv4.7.0+VSCode+(C++)环境搭建 1.VScode最新版 2.Opencv:opencv-4.7.0、opencv_contrib-4.7.0(扩展库,可自选是否安装) 3.MinGW-w64:选择GCCWindows版本 4.c-make工具:最新版,应选择二进制版本 注意 : (1)以上安装包目录文件中不应包含空格空格和其他非法字符,否则后面会

    2023年04月10日
    浏览(42)
  • Win10编译安装openssl 1.1.1和 GPG

    参考: openssl/NOTES-WINDOWS.md at master · openssl/openssl · GitHub 安装Strawberry Perl Strawberry Perl for Windows 1.2 下载openssl源码 下载 1.1.1u版本 /source/index.html 1.3 安装NASM 下载NASM ,stable版本即可 https://www.nasm.us/ 1.4 安装Microsoft Visual C compiler 因为这里本地机器装过 Visual Studio 社区版, 所以需要

    2024年02月04日
    浏览(29)
  • 在win10上,配置 Rust 开发环境(使用 mingw64编译器) 和 idea 配置 Rust 插件

    2.1、编译器 mingw 与 visual studio 之间的选择 Rust 底层是依赖C/C++的 编译器,所以需要先安装C/C++编译环境。 Windows上C/C++ 的 编译器 有两种: 微软 的 Visual Studio (msvc) GNU 的 Mingw (gnu): 官网地址:https://www.mingw-w64.org/ Rust 默认使用的是 Visual Studio, 使用默认选项就能安装上,

    2024年02月02日
    浏览(43)
  • win10、win11下WSL2环境安装

    微软官方wsl手册 微软官方wsl手册pdf 打开win10的设置,搜索windows功能,打开启用或者关闭Windows功能(win11可能搜不到,需要在控制面板里面搜索) 勾选以下2个地方,并重启,也有可能是中文名字“虚拟机平台” 下载WSL2升级包,并点击安装 WSL2升级包 以管理打开Powershell,并执行

    2023年04月17日
    浏览(30)
  • Win10安装Java 配置环境变量

    学习java开发首先需要安装jdk,并设置环境变量。 接下来就来介绍一下如何在 windows 10 系统中配置java环境变量 https://download.oracle.com/java/17/latest/jdk-17_windows-x64_bin.exe 1. 双击安装包打开 【JDK 安装对话框】 2. 更改安装目录至D盘 1. 右键点击 【我的电脑 - 属性】,打开 【设置】 面

    2024年02月11日
    浏览(43)
  • jdk11下载、安装及环境配置详解(win10环境)

    1.1、官网下载网址 https://www.oracle.com/java/technologies/downloads/#java11-windows 1.2、官网下载步骤 点击官网下载地址后,选择对应的系统环境,下载即可,如下图: 下载好安装包后,双击.exe程序,弹出如下窗口,点击【下一步】 更改安装目录,点击【下一步】,如下图: 等待安装,

    2024年01月15日
    浏览(58)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包