microsoft / vcpkg

C++ Library Manager for Windows, Linux, and MacOS
MIT License
23.17k stars 6.39k forks source link

User-wide integration breaks linking of C++/CLI projects in Visual Studio 2019 #10404

Closed tzehetner closed 3 years ago

tzehetner commented 4 years ago

Environment

To Reproduce

  1. .\vcpkg.exe install grpc:x86-windows
  2. Open Visual Studio 2019
  3. Create a new project > "CLR Class Library (.NET Core)" (C++/CLI support must be installed via the Visual Studio installer, if project type is not available)
  4. Include the std::array header file anywhere in the project: #include <array>
  5. Build the project.

Expected behavior Project builds.

Actual behavior Build fails with linker errors regarding grpc package and Winsock DLL, although the project doesn't use any of those libraries.

Failure logs

1>------ Build started: Project: ClassLibrary1, Configuration: Debug Win32 ------
1>AssemblyInfo.obj : MSIL module encountered; incremental linking is disabled for MSIL; performing full link
1>grpc.lib(iomgr_windows.cc.obj) : error LNK2019: unresolved external symbol __imp__WSAStartup@8 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@0 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@12 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@12
1>grpc.lib(tcp_client_windows.cc.obj) : error LNK2001: unresolved external symbol __imp__bind@12
1>grpc.lib(socket_windows.cc.obj) : error LNK2019: unresolved external symbol __imp__closesocket@4 referenced in function "void __cdecl grpc_winsocket_shutdown(struct grpc_winsocket *)" (?grpc_winsocket_shutdown@@YAXPAUgrpc_winsocket@@@Z)
1>grpc.lib(tcp_server_windows.cc.obj) : error LNK2001: unresolved external symbol __imp__closesocket@4
1>grpc.lib(tcp_client_windows.cc.obj) : error LNK2001: unresolved external symbol __imp__closesocket@4
1>grpc.lib(socket_windows.cc.obj) : error LNK2019: unresolved external symbol __imp__socket@12 referenced in function "void __cdecl probe_ipv6_once(void)" (?probe_ipv6_once@@YAXXZ)
1>grpc.lib(resolve_address_windows.cc.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
1>grpc.lib(tcp_windows.cc.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
1>grpc.lib(socket_windows.cc.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
1>grpc.lib(iocp_windows.cc.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
1>grpc.lib(tcp_server_windows.cc.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
1>grpc.lib(tcp_client_windows.cc.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
1>grpc.lib(socket_windows.cc.obj) : error LNK2019: unresolved external symbol __imp__WSAIoctl@36 referenced in function "void __cdecl grpc_winsocket_shutdown(struct grpc_winsocket *)" (?grpc_winsocket_shutdown@@YAXPAUgrpc_winsocket@@@Z)
1>grpc.lib(tcp_server_windows.cc.obj) : error LNK2001: unresolved external symbol __imp__WSAIoctl@36
1>grpc.lib(tcp_client_windows.cc.obj) : error LNK2001: unresolved external symbol __imp__WSAIoctl@36
1>grpc.lib(tcp_windows.cc.obj) : error LNK2001: unresolved external symbol __imp__WSAIoctl@36
1>grpc.lib(socket_windows.cc.obj) : error LNK2019: unresolved external symbol __imp__WSASocketA@24 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@24
1>grpc.lib(tcp_client_windows.cc.obj) : error LNK2001: unresolved external symbol __imp__WSASocketA@24
1>grpc.lib(iocp_windows.cc.obj) : error LNK2019: unresolved external symbol __imp__WSAGetOverlappedResult@20 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@20
1>grpc.lib(tcp_client_windows.cc.obj) : error LNK2001: unresolved external symbol __imp__WSAGetOverlappedResult@20
1>grpc.lib(tcp_server_windows.cc.obj) : error LNK2019: unresolved external symbol __imp__getpeername@12 referenced in function "void __cdecl on_accept(void *,struct grpc_error *)" (?on_accept@@YAXPAXPAUgrpc_error@@@Z)
1>grpc.lib(tcp_server_windows.cc.obj) : error LNK2019: unresolved external symbol __imp__getsockname@12 referenced in function "struct grpc_error * __cdecl prepare_socket(unsigned int,struct grpc_resolved_address const *,int *)" (?prepare_socket@@YAPAUgrpc_error@@IPBUgrpc_resolved_address@@PAH@Z)
1>grpc.lib(tcp_server_windows.cc.obj) : error LNK2019: unresolved external symbol __imp__listen@8 referenced in function "struct grpc_error * __cdecl prepare_socket(unsigned int,struct grpc_resolved_address const *,int *)" (?prepare_socket@@YAPAUgrpc_error@@IPBUgrpc_resolved_address@@PAH@Z)
1>grpc.lib(tcp_server_windows.cc.obj) : error LNK2019: unresolved external symbol __imp__setsockopt@20 referenced in function "void __cdecl on_accept(void *,struct grpc_error *)" (?on_accept@@YAXPAXPAUgrpc_error@@@Z)
1>grpc.lib(tcp_windows.cc.obj) : error LNK2001: unresolved external symbol __imp__setsockopt@20
1>grpc.lib(resolve_address_windows.cc.obj) : error LNK2019: unresolved external symbol __imp__getaddrinfo@16 referenced in function "struct grpc_error * __cdecl windows_blocking_resolve_address(char const *,char const *,struct grpc_resolved_addresses * *)" (?windows_blocking_resolve_address@@YAPAUgrpc_error@@PBD0PAPAUgrpc_resolved_addresses@@@Z)
1>grpc.lib(resolve_address_windows.cc.obj) : error LNK2019: unresolved external symbol __imp__freeaddrinfo@4 referenced in function "struct grpc_error * __cdecl windows_blocking_resolve_address(char const *,char const *,struct grpc_resolved_addresses * *)" (?windows_blocking_resolve_address@@YAPAUgrpc_error@@PBD0PAPAUgrpc_resolved_addresses@@@Z)
1>grpc.lib(tcp_windows.cc.obj) : error LNK2019: unresolved external symbol __imp__WSARecv@28 referenced in function "void __cdecl on_read(void *,struct grpc_error *)" (?on_read@@YAXPAXPAUgrpc_error@@@Z)
1>grpc.lib(tcp_windows.cc.obj) : error LNK2019: unresolved external symbol __imp__WSASend@28 referenced in function "void __cdecl on_write(void *,struct grpc_error *)" (?on_write@@YAXPAXPAUgrpc_error@@@Z)
1>grpc.lib(socket_utils_windows.cc.obj) : error LNK2019: unresolved external symbol __imp__htonl@4 referenced in function "unsigned int __cdecl grpc_htonl(unsigned int)" (?grpc_htonl@@YAII@Z)
1>grpc.lib(socket_utils_windows.cc.obj) : error LNK2019: unresolved external symbol __imp__htons@4 referenced in function "unsigned short __cdecl grpc_htons(unsigned short)" (?grpc_htons@@YAGG@Z)
1>grpc.lib(socket_utils_windows.cc.obj) : error LNK2019: unresolved external symbol __imp__ntohl@4 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@4 referenced in function "unsigned short __cdecl grpc_ntohs(unsigned short)" (?grpc_ntohs@@YAGG@Z)
1>grpc.lib(socket_utils_windows.cc.obj) : error LNK2019: unresolved external symbol __imp__inet_pton@12 referenced in function "int __cdecl grpc_inet_pton(int,char const *,void *)" (?grpc_inet_pton@@YAHHPBDPAX@Z)
1>grpc.lib(socket_utils_windows.cc.obj) : error LNK2019: unresolved external symbol _inet_ntop@16 referenced in function "char const * __cdecl grpc_inet_ntop(int,void const *,char *,unsigned int)" (?grpc_inet_ntop@@YAPBDHPBXPADI@Z)
1>C:\Users\Jack\Entwicklung\ClassLibrary1\Debug\ClassLibrary1.dll : fatal error LNK1120: 23 unresolved externals
1>Done building project "ClassLibrary1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Additional context If for example the vector header is included instead of the array header, the project builds without problems.

JackBoosY commented 4 years ago

These not found symbols are in the library ws2_32.lib provided by Windows. Since vcpkg does not actively add these system libraries to the import library list, you need to manually add it to the list.

Thanks.

tzehetner commented 4 years ago

Thanks for your reply.

Of course I understand, that I could add them, but it's an empty project that doesn't use anything from ws2_32.lib.

The thing is, that activating user-wide integration breaks compilation of C++/CLI projects, that don't even use packages from vcpkg. The empty C++/CLI project doesn't reference the grpc package and therefore shouldn't require ws2_32.lib.

To me this looks like a bug. It makes user-wide integration useless, because when I activate it, I cannot build certain projects.

And it's weird, that including the <array> header influences the situation.

Or did I miss something? I am not really an MSVC and C++/CLI expert.

JackBoosY commented 4 years ago

@strega-nil Could you please have a look?

Thanks.

x74696c commented 4 years ago

I can confirm tzehetner's issue: Even a minimal "hello world" project with compiler option "/clr" (C++/CLI) fails with error LNK2001 whenever vcpkg is installed. Same project builds fine after "vcpkg integrate remove". I am very interested in a fix!

BillyONeal commented 4 years ago

integrate install effectively adds all the .libs you install with vcpkg to the linker command line; under normal operation the linker doesn't care about symbols in unreferenced .objs from libs.

If you add

<VcpkgAutoLink>false</VcpkgAutoLink>

to a <PropertyGroup> in the affected project does the problem go away? (I'm suggesting that is the ultimate fix, just trying to confirm that this is the cause)

strega-nil commented 3 years ago

We probably want to turn off VcpkgAutoLink by default when in a C++/CLI project. However, I'm not really sure how to do that... I'll look into this.

tzehetner commented 3 years ago

Wanted to let you know, that I upgraded to the latest version of Vcpkg today, and the problem disappeard. I did not add <VcpkgAutoLink>false</VcpkgAutoLink>.

strega-nil commented 3 years ago

That is weird, thanks for updating us @tzehetner

PhoebeHui commented 3 years ago

This issue could repro locally, thanks @lken274 for sharing a workaround https://github.com/microsoft/vcpkg/issues/18021#issuecomment-845753087.

PhoebeHui commented 3 years ago

I can't repro this issue now, It seems to be fixed. Please reopen this issue if you encounter the issue again.