尽管启用了 AutoLink (vcpkg),但 VisualStudio for grpc 中仍然存在链接器错误

Still linker errors in VisualStudio for grpc despite AutoLink (vcpkg) enabled

我 运行 按照

中的建议
  1. vcpkg install grpc:x64-windows
  2. vcpkg install protobuf protobuf:x64-windows
  3. vcpkg install protobuf[zlib] protobuf[zlib]:x64-windows
  4. vcpkg integrate install

因此,这些软件包现在已安装在我的计算机上:

vcpkg list
abseil:x64-windows                                 2021-03-24#1     an open-source collection designed to augment th...
c-ares:x64-windows                                 1.17.1#2         A C library for asynchronous DNS requests
grpc:x64-windows                                   1.37.0#3         An RPC library and framework
grpc[codegen]:x64-windows                                           Build code generator machinery
openssl:x64-windows                                1.1.1k#8         OpenSSL is an open source project that provides ...
protobuf:x64-windows                               3.15.8#4         Protocol Buffers - Google's data interchange format
protobuf:x86-windows                               3.15.8#4         Protocol Buffers - Google's data interchange format
protobuf[zlib]:x64-windows                                          ZLib based features like Gzip streams
protobuf[zlib]:x86-windows                                          ZLib based features like Gzip streams
re2:x64-windows                                    2020-10-01       RE2 is a fast, safe, thread-friendly alternative...
upb:x64-windows                                    2020-12-19#1     μpb (often written 'upb') is a small protobuf i...
zlib:x64-windows                                   1.2.11#11        A compression library
zlib:x86-windows                                   1.2.11#11        A compression library

这是我的代码:

#include <grpcpp/grpcpp.h>

using grpc::ServerBuilder;

int main(int argc, char** argv) {
    ServerBuilder builder;
    return 0;
}

我没有在属性中配置任何路径(例如附加依赖项),因为我正在使用 vcpkg(我想这就是使用 vcpkg 的意义所在)。这是 vcpkg:

的配置页面

由于 27 个未解决的外部问题,我的项目无法在 Visul Studio 2019 中构建...

1>grpc.lib(iomgr_windows.cc.obj) : error LNK2019: unresolved external symbol __imp_WSAStartup referenced in function "void __cdecl winsock_init(void)" (?winsock_init@@YAXXZ)
1>grpc.lib(iomgr_windows.cc.obj) : error LNK2019: unresolved external symbol __imp_WSACleanup referenced in function "void __cdecl winsock_shutdown(void)" (?winsock_shutdown@@YAXXZ)
1>grpc.lib(socket_windows.cc.obj) : error LNK2019: unresolved external symbol __imp_bind referenced in function "void __cdecl probe_ipv6_once(void)" (?probe_ipv6_once@@YAXXZ)
1>grpc.lib(tcp_server_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_bind
1>grpc.lib(tcp_client_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_bind
1>grpc.lib(grpc_ares_ev_driver_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_bind
1>grpc.lib(socket_windows.cc.obj) : error LNK2019: unresolved external symbol __imp_closesocket referenced in function "void __cdecl grpc_winsocket_shutdown(struct grpc_winsocket *)" (?grpc_winsocket_shutdown@@YAXPEAUgrpc_winsocket@@@Z)
1>grpc.lib(tcp_server_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_closesocket
1>grpc.lib(tcp_client_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_closesocket
1>address_sorting.lib(address_sorting_windows.c.obj) : error LNK2001: unresolved external symbol __imp_closesocket
1>grpc.lib(socket_windows.cc.obj) : error LNK2019: unresolved external symbol __imp_socket referenced in function "void __cdecl probe_ipv6_once(void)" (?probe_ipv6_once@@YAXXZ)
1>address_sorting.lib(address_sorting_windows.c.obj) : error LNK2001: unresolved external symbol __imp_socket
1>grpc.lib(resolve_address_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_WSAGetLastError
1>grpc.lib(tcp_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_WSAGetLastError
1>grpc.lib(grpc_ares_ev_driver_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_WSAGetLastError
1>grpc.lib(socket_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_WSAGetLastError
1>grpc.lib(iocp_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_WSAGetLastError
1>grpc.lib(tcp_server_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_WSAGetLastError
1>grpc.lib(tcp_client_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_WSAGetLastError
1>grpc.lib(grpc_ares_ev_driver_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_WSAIoctl
1>grpc.lib(socket_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_WSAIoctl
1>grpc.lib(tcp_server_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_WSAIoctl
1>grpc.lib(tcp_client_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_WSAIoctl
1>grpc.lib(tcp_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_WSAIoctl
1>grpc.lib(socket_windows.cc.obj) : error LNK2019: unresolved external symbol __imp_WSASocketA referenced in function "void __cdecl grpc_wsa_socket_flags_init(void)" (?grpc_wsa_socket_flags_init@@YAXXZ)
1>grpc.lib(tcp_server_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_WSASocketA
1>grpc.lib(tcp_client_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_WSASocketA
1>grpc.lib(grpc_ares_ev_driver_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_WSASocketA
1>grpc.lib(iocp_windows.cc.obj) : error LNK2019: unresolved external symbol __imp_WSAGetOverlappedResult referenced in function "enum grpc_iocp_work_status __cdecl grpc_iocp_work(__int64)" (?grpc_iocp_work@@YA?AW4grpc_iocp_work_status@@_J@Z)
1>grpc.lib(tcp_server_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_WSAGetOverlappedResult
1>grpc.lib(tcp_client_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_WSAGetOverlappedResult
1>grpc.lib(grpc_ares_ev_driver_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_WSAGetOverlappedResult
1>grpc.lib(tcp_server_windows.cc.obj) : error LNK2019: unresolved external symbol __imp_getpeername referenced in function "void __cdecl on_accept(void *,struct grpc_error *)" (?on_accept@@YAXPEAXPEAUgrpc_error@@@Z)
1>grpc.lib(tcp_server_windows.cc.obj) : error LNK2019: unresolved external symbol __imp_getsockname referenced in function "struct grpc_error * __cdecl prepare_socket(unsigned __int64,struct grpc_resolved_address const *,int *)" (?prepare_socket@@YAPEAUgrpc_error@@_KPEBUgrpc_resolved_address@@PEAH@Z)
1>grpc.lib(tcp_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_getsockname
1>address_sorting.lib(address_sorting_windows.c.obj) : error LNK2001: unresolved external symbol __imp_getsockname
1>grpc.lib(tcp_server_windows.cc.obj) : error LNK2019: unresolved external symbol __imp_listen referenced in function "struct grpc_error * __cdecl prepare_socket(unsigned __int64,struct grpc_resolved_address const *,int *)" (?prepare_socket@@YAPEAUgrpc_error@@_KPEBUgrpc_resolved_address@@PEAH@Z)
1>grpc.lib(tcp_server_windows.cc.obj) : error LNK2019: unresolved external symbol __imp_setsockopt referenced in function "void __cdecl on_accept(void *,struct grpc_error *)" (?on_accept@@YAXPEAXPEAUgrpc_error@@@Z)
1>grpc.lib(tcp_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_setsockopt
1>grpc.lib(resolve_address_windows.cc.obj) : error LNK2019: unresolved external symbol __imp_getaddrinfo referenced in function "struct grpc_error * __cdecl windows_blocking_resolve_address(char const *,char const *,struct grpc_resolved_addresses * *)" (?windows_blocking_resolve_address@@YAPEAUgrpc_error@@PEBD0PEAPEAUgrpc_resolved_addresses@@@Z)
1>grpc.lib(resolve_address_windows.cc.obj) : error LNK2019: unresolved external symbol __imp_freeaddrinfo referenced in function "struct grpc_error * __cdecl windows_blocking_resolve_address(char const *,char const *,struct grpc_resolved_addresses * *)" (?windows_blocking_resolve_address@@YAPEAUgrpc_error@@PEBD0PEAPEAUgrpc_resolved_addresses@@@Z)
1>grpc.lib(tcp_windows.cc.obj) : error LNK2019: unresolved external symbol __imp_WSARecv referenced in function "void __cdecl on_read(void *,struct grpc_error *)" (?on_read@@YAXPEAXPEAUgrpc_error@@@Z)
1>grpc.lib(tcp_windows.cc.obj) : error LNK2019: unresolved external symbol __imp_WSASend referenced in function "void __cdecl on_write(void *,struct grpc_error *)" (?on_write@@YAXPEAXPEAUgrpc_error@@@Z)
1>grpc.lib(grpc_ares_ev_driver_windows.cc.obj) : error LNK2001: unresolved external symbol __imp_WSASend
1>grpc.lib(socket_utils_windows.cc.obj) : error LNK2019: unresolved external symbol __imp_htonl referenced in function "unsigned int __cdecl grpc_htonl(unsigned int)" (?grpc_htonl@@YAII@Z)
1>address_sorting.lib(address_sorting.c.obj) : error LNK2001: unresolved external symbol __imp_htonl
1>grpc.lib(socket_utils_windows.cc.obj) : error LNK2019: unresolved external symbol __imp_htons referenced in function "unsigned short __cdecl grpc_htons(unsigned short)" (?grpc_htons@@YAGG@Z)
1>grpc.lib(grpc_ares_wrapper.cc.obj) : error LNK2001: unresolved external symbol __imp_htons
1>grpc.lib(parse_address.cc.obj) : error LNK2001: unresolved external symbol __imp_htons
1>grpc.lib(socket_utils_windows.cc.obj) : error LNK2019: unresolved external symbol __imp_ntohl referenced in function "unsigned int __cdecl grpc_ntohl(unsigned int)" (?grpc_ntohl@@YAII@Z)
1>grpc.lib(socket_utils_windows.cc.obj) : error LNK2019: unresolved external symbol __imp_ntohs referenced in function "unsigned short __cdecl grpc_ntohs(unsigned short)" (?grpc_ntohs@@YAGG@Z)
1>grpc.lib(grpc_ares_wrapper.cc.obj) : error LNK2001: unresolved external symbol __imp_ntohs
1>grpc.lib(socket_utils_windows.cc.obj) : error LNK2019: unresolved external symbol __imp_inet_pton referenced in function "int __cdecl grpc_inet_pton(int,char const *,void *)" (?grpc_inet_pton@@YAHHPEBDPEAX@Z)
1>grpc.lib(socket_utils_windows.cc.obj) : error LNK2019: unresolved external symbol inet_ntop referenced in function "char const * __cdecl grpc_inet_ntop(int,void const *,char *,unsigned __int64)" (?grpc_inet_ntop@@YAPEBDHPEBXPEAD_K@Z)
1>grpc.lib(ssl_transport_security.cc.obj) : error LNK2001: unresolved external symbol inet_ntop
1>grpc.lib(grpc_ares_ev_driver_windows.cc.obj) : error LNK2019: unresolved external symbol __imp_WSASetLastError referenced in function "public: __cdecl grpc_core::WSAErrorContext::~WSAErrorContext(void)" (??1WSAErrorContext@grpc_core@@QEAA@XZ)
1>grpc.lib(grpc_ares_ev_driver_windows.cc.obj) : error LNK2019: unresolved external symbol __imp_WSAConnect referenced in function "public: int __cdecl grpc_core::GrpcPolledFdWindows::ConnectUDP(class grpc_core::WSAErrorContext *,struct sockaddr const *,int)" (?ConnectUDP@GrpcPolledFdWindows@grpc_core@@QEAAHPEAVWSAErrorContext@2@PEBUsockaddr@@H@Z)
1>grpc.lib(grpc_ares_ev_driver_windows.cc.obj) : error LNK2019: unresolved external symbol __imp_WSARecvFrom referenced in function "public: void __cdecl grpc_core::GrpcPolledFdWindows::ContinueRegisterForOnReadableLocked(void)" (?ContinueRegisterForOnReadableLocked@GrpcPolledFdWindows@grpc_core@@QEAAXXZ)
1>address_sorting.lib(address_sorting_windows.c.obj) : error LNK2019: unresolved external symbol __imp_connect referenced in function address_sorting_create_source_addr_factory_for_current_platform

我在调试和 x64 配置中构建。

我错过了什么?我以为 vcpkg 会让生活更轻松?

It seems to be a problem of the grpc package.

已接受答案的第二部分完全解决了我的问题。

我将 #pragma comment(lib, "Ws2_32.lib") 添加到我的 cpp 文件中,问题就消失了。

也可以这样修复(我截图自Unresolved external symbol LNK2019):