在 C++ 中使用 gRPC 时无法解析的外部符号

Tho*_*mas 5 c++ protocol-buffers grpc visual-studio-2017

Win10,Visual Studio 15 2017

这就是我到目前为止所做的:

  • 从 github 克隆 grpc 和子模块
  • cmake --build 在 grpc 上
  • 从我的原型文件生成 pb.cc 和 pb.h 文件
  • grpcprotobuf添加到 VC++ 目录,包括目录
  • zlibgrpcprotobuf添加到 VC++ 目录、库目录
  • zlib.libgpr.libgrpc.libgrpc++.liblibprotobuf.lib 添加到链接器、输入、附加依赖项
  • 添加预处理器定义:_WIN32_WINNT=0x600;调试
  • 将运行时库更改为 /MD

之后我的空项目,包括编译没有​​错误的 pb 文件。

然后我写了一些代码来使用和测试 grpc,但是当我这次尝试编译时,我收到了几个错误,比如

LNK2019 unresolved external symbol _address_sorting_init referenced in function "void __cdecl grpc_resolver_dns_ares_init(void)" (?grpc_resolver_dns_ares_init@@YAXXZ) grpc.lib(dns_resolver_ares.obj)  
LNK2001 unresolved external symbol __imp__htons@4 grpc.lib(socket_utils_windows.obj)    
LNK2019 unresolved external symbol _ares_gethostbyname referenced in function "struct grpc_ares_request * __cdecl grpc_dns_lookup_ares_continue_after_check_localhost_and_ip_literals_locked(char const *,char const *,char const *,struct grpc_pollset_set *,struct grpc_closure *,struct grpc_lb_addresses * *,bool,char * *,struct grpc_combiner *)" (?grpc_dns_lookup_ares_continue_after_check_localhost_and_ip_literals_locked@@YAPAUgrpc_ares_request@@PBD00PAUgrpc_pollset_set@@PAUgrpc_closure@@PAPAUgrpc_lb_addresses@@_NPAPADPAUgrpc_combiner@@@Z)   grpc.lib(grpc_ares_wrapper.obj)
Run Code Online (Sandbox Code Playgroud)

Nic*_*ble 4

The first symbol "address_sorting_init" comes from the library address sorting present at https://github.com/grpc/grpc/tree/master/third_party/address_sorting

The second symbol is from the windows API library "ws2_32", the winsock library from Microsoft.

The third symbol is from the c-ares library: https://c-ares.haxx.se/

所有 3 个库都是在 Windows 下构建 grpc 所必需的,因此您应该将它们添加到您的项目中。