Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresolved External Symbols when using gRPC in C++

Win10, Visual Studio 15 2017

That's what I've done so far:

  • cloned grpc and the submodules from github
  • cmake --build on grpc
  • generated pb.cc and pb.h files from my proto-files
  • added grpc and protobuf to VC++ Directories, Include Directories
  • added zlib, grpc and protobuf to VC++ Directories, Library Directories
  • added zlib.lib, gpr.lib, grpc.lib, grpc++.lib and libprotobuf.lib to Linker, Input, Additional Dependencies
  • added Preprocessor Definitions: _WIN32_WINNT=0x600; NDEBUG
  • changed Runtime Library to /MD

After that my empty project including the pb-files compiled without an error.

Then I wrote some code to use and test grpc but when I try to compile this time, I'm getting several errors like

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)
like image 319
Thomas Avatar asked Oct 24 '18 13:10

Thomas


2 Answers

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/

All 3 libraries are necessary for building grpc under Windows, so you should add them into your project.

like image 91
Nicolas Noble Avatar answered Oct 10 '22 03:10

Nicolas Noble


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. This lib is in the grpc build directory, same as grpc++.lib, grpc.lib and 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.

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

As a summary, suppose your grpc build directory is c:\grpc\.build, here are the libraries required for building a Release version application:

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. To build a debug version with 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.

Please also note the Debug version of protobuf library file's name is libprotobufd.lib, not libprotobuf.lib.

like image 35
JM Yang Avatar answered Oct 10 '22 02:10

JM Yang