在 Windows 上链接 gRPC for VisualC++

use*_*412 4 visual-studio visual-c++ grpc

我正在尝试在 Visual C++ 项目中使用 gRPC。

到目前为止我有:

1) 构建gRPC:vcpkg2 vcpkg install grpc:x64-windows ) 将vcpgk库与 Visual Studio 集成:vcpkg integrate install

到目前为止,一切都很好——智能感知自动完成命名空间等。

我的客户端cpp文件如下所示:

#include "pch.h"
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp\grpcpp.h>
#include "GRPCServerInterface.grpc.pb.h"
#include "FileFormat.pb.h"

using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using namespace GRPCServerInterface;

int main()
{
    std::cout << "Hello World!\n";

    // prepare send message & payload
    IsFormatSupportedInput msg;
    msg.set_fileextension(".asp");

    // prepare reply
    IsFormatSupportedOutput rpl;

    // connect
    FileHandler::Stub ClientStub = FileHandler::Stub(grpc::CreateChannel("localhost:50051", grpc::InsecureChannelCredentials()));

    ClientContext context;

    // execute rpc
    Status status = ClientStub.IsFormatSupported(&context, msg, &rpl);

    // handle result
    if (status.ok()) 
    {
        std::cout << "Format supported says:" << std::endl << "\t formats read: " << rpl.readsupportedformats() << std::endl << "\t formats write: " << rpl.writesupportedformats() << std::endl;
    }
    else
    {
        std::cout << status.error_code() << ": " << status.error_message() << std::endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

所有消息和proto文件都会退出并正常工作,因为我已经在 python 和 c# 项目中使用它们。

构建时,Visual Studio 会生成大量 125 个错误,所有这些错误都在我从未接触过的文件中。

中GRPCServerInterface.pb.h,有identifier GOOGLE_DCHECK is undefined

所有其他错误都member abc may not be initialized在 grpc 包含的各个头文件中,例如 member "google::protobuf::Any::kIndexInFileMessages" may not be initialized在 file 中any.pb.h。type.pb.h和中还有更多descriptor.pbp.h。

最后但并非最不重要的一点是,我收到提示添加#iclude "pch.h"到自动生成的 protobuf 类grpcserverinterface.grpc.pb.cc并grpcserverinterface.pb.cc- 添加它会发生一些变化,但基本上所有错误仍然是undefined symbol和member may not be initialized。而且我真的不想每次都修改自动生成的代码。

我缺少什么?或者尝试在 Visual Studio 中使用 grpc 只是徒劳的尝试,我应该转向像 bazel 这样的构建框架吗?

use*_*412 5

解决了!

解决方法分两步:

1)我禁用了整个项目的预编译头 - 这使得问题#include "pch.h消失了。您可能只对 protobuf 文件禁用它,因为它可以在每个文件的基础上完成。

2)最后列出的错误之一是unresolved external symbol __imp_WSASocketA,这最终导致我提出这个问题Unresolved external symbol LNK2019。我只是将其包含#pragma comment(lib, "Ws2_32.lib")在一个源文件中,现在一切都运行得非常完美。