简体   繁体   English

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

[英]Unresolved External Symbols when using gRPC in C++

Win10, Visual Studio 15 2017 Win10,Visual Studio 15 2017

That's what I've done so far:这就是我到目前为止所做的:

  • cloned grpc and the submodules from github从 github 克隆 grpc 和子模块
  • cmake --build on grpc cmake --build在 grpc 上构建
  • generated pb.cc and pb.h files from my proto-files从我的原型文件生成 pb.cc 和 pb.h 文件
  • added grpc and protobuf to VC++ Directories, Include Directoriesgrpcprotobuf添加到 VC++ 目录,包括目录
  • added zlib , grpc and protobuf to VC++ Directories, Library Directorieszlibgrpcprotobuf添加到 VC++ 目录、库目录
  • added zlib.lib , gpr.lib , grpc.lib , grpc++.lib and libprotobuf.lib to Linker, Input, Additional Dependencieszlib.libgpr.libgrpc.libgrpc++.liblibprotobuf.lib 添加到链接器、输入、附加依赖项
  • added Preprocessor Definitions: _WIN32_WINNT=0x600;添加预处理器定义:_WIN32_WINNT=0x600; NDEBUG调试
  • changed Runtime Library to /MD将运行时库更改为 /MD

After that my empty project including the pb-files compiled without an error.之后我的空项目,包括编译没有​​错误的 pb 文件。

Then I wrote some code to use and test grpc but when I try to compile this time, I'm getting several errors like然后我写了一些代码来使用和测试 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)

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第一个符号“address_sorting_init”来自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.第二个符号来自 Windows API 库“ws2_32”,微软的 winsock 库。

The third symbol is from the c-ares library: https://c-ares.haxx.se/第三个符号来自 c-ares 库: https : //c-ares.haxx.se/

All 3 libraries are necessary for building grpc under Windows, so you should add them into your project.所有 3 个库都是在 Windows 下构建 grpc 所必需的,因此您应该将它们添加到您的项目中。

The first error:第一个错误:

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)  

can be resolved by adding the address_sorting.lib.可以通过添加address_sorting.lib 来解决。 This lib is in the grpc build directory, same as grpc++.lib, grpc.lib and gpr.lib.这个lib在grpc build目录下,与grpc++.lib、grpc.lib和gpr.lib相同。

The second error:第二个错误:

LNK2001 unresolved external symbol __imp__htons@4 grpc.lib(socket_utils_windows.obj)

can be resolved by adding ws2_32.lib as per Nicolas suggested.可以通过按照 Nicolas 的建议添加 ws2_32.lib 来解决。

The third error:第三个错误:

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)

can be resolved by adding the cares.lib.可以通过添加 cares.lib 来解决。 You should be able to find this lib file under the third_party\\cares\\cares\\lib directory inside the grpc build directory, no need to get it from elsewhere.你应该可以在grpc build目录下的third_party\\cares\\cares\\lib目录下找到这个lib文件,不需要从其他地方获取。

As a summary, suppose your grpc build directory is c:\\grpc\\.build, here are the libraries required for building a Release version application:总而言之,假设您的 grpc 构建目录是 c:\\grpc\\.build,以下是构建 Release 版本应用程序所需的库:

c:\grpc\.build\grpc++.lib
c:\grpc\.build\gpr.lib
c:\grpc\.build\grpc.lib
c:\grpc\.build\address_sorting.lib
c:\grpc\.build\third_party\protobuf\libprotobuf.lib
c:\grpc\.build\third_party\zlib.lib
c:\grpc\.build\third_party\cares\cares\lib\cares.lib
ws2_32.lib

If you want to build a Debug version of your application, please build a Debug version of grpc first, and then reference the libraries from the grpc debug version.如果要构建应用程序的调试版本,请先构建grpc 的调试版本,然后从grpc 调试版本中引用库。 To build a debug version with Ninja:要使用 Ninja 构建调试版本:

> md .debug
> cd .debug
> call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" x64
> cmake .. -GNinja -DCMAKE_BUILD_TYPE=Debug
> cmake --build .

Please change above directory of vcvarsall.bat according to your Visual Studio installation.请根据您的 Visual Studio 安装更改 vcvarsall.bat 的上述目录。

Please also note the Debug version of protobuf library file's name is libprotobufd.lib, not libprotobuf.lib.另请注意 protobuf 库文件的 Debug 版本名称是 libprotobufd.lib,而不是 libprotobuf.lib。

In my case I had to add all these libs to resolve all unresolved external symbols issues就我而言,我必须添加所有这些库来解决所有未解决的外部符号问题

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_dynamic_annotations.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_registry.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

With these libraries linked it works for me (in VS2015 and Win10):链接这些库后,它对我有用(在 VS2015 和 Win10 中):

grpc++, grpc, gpr, libprotobuf, zlibstatic, ssl, crypto

The last 2 are in the third_party/boringssl directory.最后 2 个在 third_party/boringssl 目录中。

I hit this using conan to build grpc.我使用柯南来构建grpc。 I found it much easier change the grpc conanfile to use CMake install to collect all the necessary libraries.我发现更改 grpc conanfile 以使用CMake install收集所有必要的库要容易得多。 The GRPC authors maintain the install target for this purpose. GRPC 作者为此目的维护install目标。

Conan tools' collect_libs ( docs ) also means you don't have to explicitly list them all yourself;柯南工具的collect_libs ( docs ) 也意味着您不必自己明确列出它们; they get generated into the props and then visual studio uses them from there.它们被生成到道具中,然后视觉工作室从那里使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM