libzmq-static linking fails on Windows (msvc14)
envgap__zeromq__libzmq-2884
01 / FAILURE SIGNATURE
As reported upstream
2>libzmq-v140-mt-sgd-4_2_4.lib(zmq.obj) : error LNK2019: unresolved external symbol __imp_WSAStartup referenced in function zmq_ctx_new
Not a benchmark task.
- The project already builds and runs before the fix, so there is nothing to repair.
02 / ENVIRONMENT RECIPE
- Base commit
2540e1a60987cc9dd91890309d393e878bf254c9- Manifest
CMakeLists.txt- Reproduce
Awaiting issue-specific recipe- Run under trace
Awaiting a meaningful runtime command
03 / ORIGINAL ISSUE TEXT
zeromq/libzmq #2884 · read the original issue
# Issue description
Statically link a target to libzmq on Windows results in linking errors, compilation is successful on Unix systems. (Tested on Ubuntu 16.04 and OSX 10.13.2).
My knowledge of the msvc build system is quite limited, please let me know I'm missing something!
# Environment
* libzmq version: current master (020b0be)
* OS: Win10 with msvc14
# Minimal test code / Steps to reproduce the issue
1. Build ZeroMQ from source with `BUILD_STATIC` set to `ON` - output targets are `libzmq `and `libzmq-static`. Build also the INSTALL.
2. Use ZeroMQ in an another CMake project. For example, with the following dummy files. The code doesn't have any use, but it should still compile.
CMakeLists.txt
```cmake
cmake_minimum_required(VERSION 3.5)
find_package(Threads REQUIRED)
find_package(ZeroMQ REQUIRED)
include_directories(include ${ZeroMQ_INCLUDE_DIR})
add_executable(zeromqTest-bin include/test.hpp src/test.cpp)
target_link_libraries(zeromqTest-bin libzmq) # WORKS
#target_link_libraries(zeromqTest-bin libzmq-static ${CMAKE_THREAD_LIBS_INIT}) # DOESN'T WORK
```
include/test.hpp
```c++
#pragma once
#include <zmq.h>
class ClassTest {
public:
ClassTest() = default;
~ClassTest() { zmq_close(socket_); }
private:
void* socket_;
};
```
src/test.cpp
```c++
#include <test.hpp>
int main() {
ClassTest test;
}
```
Run CMake on this project and build it.
# What's the actual result?
With line 7 uncommented and line 8 commented:
```cmake
target_link_libraries(zeromqTest-bin libzmq) # WORKS
#target_link_libraries(zeromqTest-bin libzmq-static ${CMAKE_THREAD_LIBS_INIT) # DOESN'T WORK
```
compilation works as expected.
With line 8 uncommented and line 7 commented
```cmake
#target_link_libraries(zeromqTest-bin libzmq) # WORKS
target_link_libraries(zeromqTest-bin libzmq-static ${CMAKE_THREAD_LIBS_INIT) # DOESN'T WORK
```
compilation fails with the following:
```console
1>------ Build started: Project: ZERO_CHECK, Configuration: Debug x64 ------
2>------ Build started: Project: zeromqTest-bin, Configuration: Debug x64 ------
2> test.cpp
2>libzmq-v140-mt-sgd-4_2_4.lib(zmq.obj) : error LNK2019: unresolved external symbol __imp_WSAStartup referenced in function zmq_ctx_new
2>libzmq-v140-mt-sgd-4_2_4.lib(zmq.obj) : error LNK2019: unresolved external symbol __imp_WSACleanup referenced in function zmq_ctx_term
2>libzmq-v140-mt-sgd-4_2_4.lib(udp_engine.obj) : error LNK2001: unresolved external symbol __imp_WSAGetLastError
2>libzmq-v140-mt-sgd-4_2_4.lib(ip.obj) : error LNK2001: unresolved external symbol __imp_WSAGetLastError
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp.obj) : error LNK2001: unresolved external symbol __imp_WSAGetLastError
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp_connecter.obj) : error LNK2001: unresolved external symbol __imp_WSAGetLastError
2>libzmq-v140-mt-sgd-4_2_4.lib(socks_connecter.obj) : error LNK2001: unresolved external symbol __imp_WSAGetLastError
2>libzmq-v140-mt-sgd-4_2_4.lib(err.obj) : error LNK2001: unresolved external symbol __imp_WSAGetLastError
2>libzmq-v140-mt-sgd-4_2_4.lib(socket_poller.obj) : error LNK2001: unresolved external symbol __imp_WSAGetLastError
2>libzmq-v140-mt-sgd-4_2_4.lib(signaler.obj) : error LNK2001: unresolved external symbol __imp_WSAGetLastError
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp_listener.obj) : error LNK2001: unresolved external symbol __imp_WSAGetLastError
2>libzmq-v140-mt-sgd-4_2_4.lib(socket_poller.obj) : error LNK2019: unresolved external symbol __WSAFDIsSet referenced in function "public: int __cdecl zmq::socket_poller_t::wait(struct zmq::socket_poller_t::event_t *,int,long)" (?wait@socket_poller_t@zmq@@QEAAHPEAUevent_t@12@HJ@Z)
2>libzmq-v140-mt-sgd-4_2_4.lib(select.obj) : error LNK2001: unresolved external symbol __WSAFDIsSet
2>libzmq-v140-mt-sgd-4_2_4.lib(socket_poller.obj) : error LNK2019: unresolved external symbol __imp_select referenced in function "public: int __cdecl zmq::socket_poller_t::wait(struct zmq::socket_poller_t::event_t *,int,long)" (?wait@socket_poller_t@zmq@@QEAAHPEAUevent_t@12@HJ@Z)
2>libzmq-v140-mt-sgd-4_2_4.lib(select.obj) : error LNK2001: unresolved external symbol __imp_select
2>libzmq-v140-mt-sgd-4_2_4.lib(signaler.obj) : error LNK2001: unresolved external symbol __imp_select
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp_address.obj) : error LNK2019: unresolved external symbol __imp_htonl referenced in function "protected: int __cdecl zmq::tcp_address_t::resolve_interface(char const *,bool,bool)" (?resolve_interface@tcp_address_t@zmq@@IEAAHPEBD_N1@Z)
2>libzmq-v140-mt-sgd-4_2_4.lib(signaler.obj) : error LNK2001: unresolved external symbol __imp_htonl
2>libzmq-v140-mt-sgd-4_2_4.lib(udp_address.obj) : error LNK2001: unresolved external symbol __imp_htonl
2>libzmq-v140-mt-sgd-4_2_4.lib(udp_engine.obj) : error LNK2019: unresolved external symbol __imp_htons referenced in function "void * __cdecl std::_Allocate(unsigned __int64,unsigned __int64,bool)" (?_Allocate@std@@YAPEAX_K0_N@Z)
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp_address.obj) : error LNK2001: unresolved external symbol __imp_htons
2>libzmq-v140-mt-sgd-4_2_4.lib(signaler.obj) : error LNK2001: unresolved external symbol __imp_htons
2>libzmq-v140-mt-sgd-4_2_4.lib(udp_address.obj) : error LNK2001: unresolved external symbol __imp_htons
2>libzmq-v140-mt-sgd-4_2_4.lib(stream_engine.obj) : error LNK2001: unresolved external symbol __imp_htons
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp_address.obj) : error LNK2019: unresolved external symbol __imp_ntohs referenced in function "public: virtual int __cdecl zmq::tcp_address_t::to_string(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?to_string@tcp_address_t@zmq@@UEAAHAEAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
2>libzmq-v140-mt-sgd-4_2_4.lib(stream_engine.obj) : error LNK2001: unresolved external symbol __imp_ntohs
2>libzmq-v140-mt-sgd-4_2_4.lib(udp_engine.obj) : error LNK2001: unresolved external symbol __imp_ntohs
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp_address.obj) : error LNK2019: unresolved external symbol GetAdaptersAddresses referenced in function "protected: int __cdecl zmq::tcp_address_t::resolve_nic_name(char const *,bool,bool)" (?resolve_nic_name@tcp_address_t@zmq@@IEAAHPEBD_N1@Z)
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp_address.obj) : error LNK2019: unresolved external symbol if_nametoindex referenced in function "public: int __cdecl zmq::tcp_address_t::resolve(char const *,bool,bool,bool)" (?resolve@tcp_address_t@zmq@@QEAAHPEBD_N11@Z)
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp_address.obj) : error LNK2019: unresolved external symbol if_indextoname referenced in function "protected: int __cdecl zmq::tcp_address_t::get_interface_name(unsigned long,char * *)const " (?get_interface_name@tcp_address_t@zmq@@IEBAHKPEAPEAD@Z)
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp_address.obj) : error LNK2019: unresolved external symbol __imp_getaddrinfo referenced in function "protected: int __cdecl zmq::tcp_address_t::resolve_interface(char const *,bool,bool)" (?resolve_interface@tcp_address_t@zmq@@IEAAHPEBD_N1@Z)
2>libzmq-v140-mt-sgd-4_2_4.lib(socks.obj) : error LNK2001: unresolved external symbol __imp_getaddrinfo
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp_address.obj) : error LNK2019: unresolved external symbol __imp_freeaddrinfo referenced in function "protected: int __cdecl zmq::tcp_address_t::resolve_interface(char const *,bool,bool)" (?resolve_interface@tcp_address_t@zmq@@IEAAHPEBD_N1@Z)
2>libzmq-v140-mt-sgd-4_2_4.lib(socks.obj) : error LNK2001: unresolved external symbol __imp_freeaddrinfo
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp_address.obj) : error LNK2019: unresolved external symbol __imp_getnameinfo referenced in function "public: virtual int __cdecl zmq::tcp_address_t::to_string(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?to_string@tcp_address_t@zmq@@UEAAHAEAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
2>libzmq-v140-mt-sgd-4_2_4.lib(ip.obj) : error LNK2001: unresolved external symbol __imp_getnameinfo
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp_address.obj) : error LNK2001: unresolved external symbol in6addr_any
2>libzmq-v140-mt-sgd-4_2_4.lib(select.obj) : error LNK2019: unresolved external symbol __imp_getsockname referenced in function "private: static unsigned short __cdecl zmq::select_t::determine_fd_family(unsigned __int64)" (?determine_fd_family@select_t@zmq@@CAG_K@Z)
2>libzmq-v140-mt-sgd-4_2_4.lib(signaler.obj) : error LNK2001: unresolved external symbol __imp_getsockname
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp_listener.obj) : error LNK2001: unresolved external symbol __imp_getsockname
2>libzmq-v140-mt-sgd-4_2_4.lib(select.obj) : error LNK2019: unresolved external symbol __imp_getsockopt referenced in function "private: static unsigned short __cdecl zmq::select_t::determine_fd_family(unsigned __int64)" (?determine_fd_family@select_t@zmq@@CAG_K@Z)
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp.obj) : error LNK2001: unresolved external symbol __imp_getsockopt
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp_connecter.obj) : error LNK2001: unresolved external symbol __imp_getsockopt
2>libzmq-v140-mt-sgd-4_2_4.lib(socks_connecter.obj) : error LNK2001: unresolved external symbol __imp_getsockopt
2>libzmq-v140-mt-sgd-4_2_4.lib(select.obj) : error LNK2019: unresolved external symbol __imp_WSACloseEvent referenced in function "public: __cdecl zmq::select_t::wsa_events_t::~wsa_events_t(void)" (??1wsa_events_t@select_t@zmq@@QEAA@XZ)
2>libzmq-v140-mt-sgd-4_2_4.lib(select.obj) : error LNK2019: unresolved external symbol __imp_WSACreateEvent referenced in function "public: __cdecl zmq::select_t::wsa_events_t::wsa_events_t(void)" (??0wsa_events_t@select_t@zmq@@QEAA@XZ)
2>libzmq-v140-mt-sgd-4_2_4.lib(select.obj) : error LNK2019: unresolved external symbol __imp_WSAEventSelect referenced in function "private: void __cdecl zmq::select_t::loop(void)" (?loop@select_t@zmq@@AEAAXXZ)
2>libzmq-v140-mt-sgd-4_2_4.lib(select.obj) : error LNK2019: unresolved external symbol __imp_WSAWaitForMultipleEvents referenced in function "private: void __cdecl zmq::select_t::loop(void)" (?loop@select_t@zmq@@AEAAXXZ)
2>libzmq-v140-mt-sgd-4_2_4.lib(signaler.obj) : error LNK2019: unresolved external symbol __imp_accept referenced in function "private: static int __cdecl zmq::signaler_t::make_fdpair(unsigned __int64 *,unsigned __int64 *)" (?make_fdpair@signaler_t@zmq@@CAHPEA_K0@Z)
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp_listener.obj) : error LNK2001: unresolved external symbol __imp_accept
2>libzmq-v140-mt-sgd-4_2_4.lib(udp_engine.obj) : error LNK2001: unresolved external symbol __imp_bind
2>libzmq-v140-mt-sgd-4_2_4.lib(signaler.obj) : error LNK2001: unresolved external symbol __imp_bind
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp_listener.obj) : error LNK2001: unresolved external symbol __imp_bind
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp_connecter.obj) : error LNK2001: unresolved external symbol __imp_bind
2>libzmq-v140-mt-sgd-4_2_4.lib(socks_connecter.obj) : error LNK2001: unresolved external symbol __imp_bind
2>libzmq-v140-mt-sgd-4_2_4.lib(socks_connecter.obj) : error LNK2001: unresolved external symbol __imp_closesocket
2>libzmq-v140-mt-sgd-4_2_4.lib(udp_engine.obj) : error LNK2001: unresolved external symbol __imp_closesocket
2>libzmq-v140-mt-sgd-4_2_4.lib(signaler.obj) : error LNK2001: unresolved external symbol __imp_closesocket
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp_listener.obj) : error LNK2001: unresolved external symbol __imp_closesocket
2>libzmq-v140-mt-sgd-4_2_4.lib(stream_engine.obj) : error LNK2001: unresolved external symbol __imp_closesocket
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp_connecter.obj) : error LNK2001: unresolved external symbol __imp_closesocket
2>libzmq-v140-mt-sgd-4_2_4.lib(signaler.obj) : error LNK2019: unresolved external symbol __imp_connect referenced in function "private: static int __cdecl zmq::signaler_t::make_fdpair(unsigned __int64 *,unsigned __int64 *)" (?make_fdpair@signaler_t@zmq@@CAHPEA_K0@Z)
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp_connecter.obj) : error LNK2001: unresolved external symbol __imp_connect
2>libzmq-v140-mt-sgd-4_2_4.lib(socks_connecter.obj) : error LNK2001: unresolved external symbol __imp_connect
2>libzmq-v140-mt-sgd-4_2_4.lib(signaler.obj) : error LNK2019: unresolved external symbol __imp_listen referenced in function "private: static int __cdecl zmq::signaler_t::make_fdpair(unsigned __int64 *,unsigned __int64 *)" (?make_fdpair@signaler_t@zmq@@CAHPEA_K0@Z)
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp_listener.obj) : error LNK2001: unresolved external symbol __imp_listen
2>libzmq-v140-mt-sgd-4_2_4.lib(signaler.obj) : error LNK2019: unresolved external symbol __imp_recv referenced in function "public: void __cdecl zmq::signaler_t::recv(void)" (?recv@signaler_t@zmq@@QEAAXXZ)
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp.obj) : error LNK2001: unresolved external symbol __imp_recv
2>libzmq-v140-mt-sgd-4_2_4.lib(signaler.obj) : error LNK2019: unresolved external symbol __imp_send referenced in function "public: void __cdecl zmq::signaler_t::send(void)" (?send@signaler_t@zmq@@QEAAXXZ)
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp.obj) : error LNK2001: unresolved external symbol __imp_send
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp_connecter.obj) : error LNK2001: unresolved external symbol __imp_setsockopt
2>libzmq-v140-mt-sgd-4_2_4.lib(udp_engine.obj) : error LNK2001: unresolved external symbol __imp_setsockopt
2>libzmq-v140-mt-sgd-4_2_4.lib(signaler.obj) : error LNK2001: unresolved external symbol __imp_setsockopt
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp_listener.obj) : error LNK2001: unresolved external symbol __imp_setsockopt
2>libzmq-v140-mt-sgd-4_2_4.lib(ip.obj) : error LNK2001: unresolved external symbol __imp_setsockopt
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp.obj) : error LNK2001: unresolved external symbol __imp_setsockopt
2>libzmq-v140-mt-sgd-4_2_4.lib(udp_address.obj) : error LNK2019: unresolved external symbol __imp_inet_addr referenced in function "public: int __cdecl zmq::udp_address_t::resolve(char const *,bool)" (?resolve@udp_address_t@zmq@@QEAAHPEBD_N@Z)
2>libzmq-v140-mt-sgd-4_2_4.lib(udp_engine.obj) : error LNK2001: unresolved external symbol __imp_inet_addr
2>libzmq-v140-mt-sgd-4_2_4.lib(ip.obj) : error LNK2019: unresolved external symbol __imp_ioctlsocket referenced in function "void __cdecl zmq::unblock_socket(unsigned __int64)" (?unblock_socket@zmq@@YAX_K@Z)
2>libzmq-v140-mt-sgd-4_2_4.lib(ip.obj) : error LNK2019: unresolved external symbol __imp_getpeername referenced in function "int __cdecl zmq::get_peer_ip_address(unsigned __int64,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?get_peer_ip_address@zmq@@YAH_KAEAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
2>libzmq-v140-mt-sgd-4_2_4.lib(ip.obj) : error LNK2019: unresolved external symbol __imp_socket referenced in function "unsigned __int64 __cdecl zmq::open_socket(int,int,int)" (?open_socket@zmq@@YA_KHHH@Z)
2>libzmq-v140-mt-sgd-4_2_4.lib(tcp.obj) : error LNK2019: unresolved external symbol __imp_WSAIoctl referenced in function "int __cdecl zmq::tune_tcp_keepalives(unsigned __int64,int,int,int,int)" (?tune_tcp_keepalives@zmq@@YAH_KHHHH@Z)
2>libzmq-v140-mt-sgd-4_2_4.lib(udp_engine.obj) : error LNK2019: unresolved external symbol __imp_inet_ntoa referenced in function "private: void __cdecl zmq::udp_engine_t::sockaddr_to_msg(class zmq::msg_t *,struct sockaddr_in *)" (?sockaddr_to_msg@udp_engine_t@zmq@@AEAAXPEAVmsg_t@2@PEAUsockaddr_in@@@Z)
2>libzmq-v140-mt-sgd-4_2_4.lib(udp_engine.obj) : error LNK2019: unresolved external symbol __imp_recvfrom referenced in function "public: virtual void __cdecl zmq::udp_engine_t::in_event(void)" (?in_event@udp_engine_t@zmq@@UEAAXXZ)
2>libzmq-v140-mt-sgd-4_2_4.lib(udp_engine.obj) : error LNK2019: unresolved external symbol __imp_sendto referenced in function "public: virtual void __cdecl zmq::udp_engine_t::out_event(void)" (?out_event@udp_engine_t@zmq@@UEAAXXZ)
2>C:\Users\Name\repo\zeromq_static_test\build\Debug\zeromqTest-bin.exe : fatal error LNK1120: 37 unresolved externals
========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
```
The compile flag `ZMQ_STATIC` is correctly set (thanks to the changes in #2861).
Note that the linker appears to search for `__imp` symbols that are part of the dynamic build and therefore should not be used with `libzmq-static`.
# What's the expected result?
That snippet should compile both with `libzmq `and `libzmq-static`, as it does on Linux and Mac.
# Note
Linking to `${CMAKE_THREAD_LIBS_INIT}` is actually only needed on Ubuntu 16.04 when using libzmq-static.
04 / LABELS
Labels from the report text only; not yet run
No supported category has been assigned.
Label rules and the text that matched
[]