jean-airoldie / zeromq-src-rs

Source code and logic to build ZeroMQ from source
MIT License
11 stars 15 forks source link

Windows: Linkage error when `opt-leve=z` and `crt-static` is used #13

Open oblique opened 5 years ago

oblique commented 5 years ago

I tried to compile a project on Windows and I was getting linkage error. After some investigation I found the it has to do with opt-leve=z (set in Cargo.toml) and -Ctarget-feature=+crt-static (set in RUSTFLAGS env var). If I omit one of the two, the everything works, however this is not an option for the actual project.

I created a repo with minimal example that reproduces the problem: oblique/optlevel-z-crt-static-issue. You can view the linkage error here.

I have no idea why this is caused since I don't have a lot experience with Windows. Also I'm not sure if this is a bug in rustc.

The error:

          zmq.lib(proxy.obj) : error LNK2001: unresolved external symbol __imp_strerror
          zmq.lib(proxy.obj) : error LNK2019: unresolved external symbol __imp_puts referenced in function "int __cdecl zmq::proxy(class zmq::socket_base_t *,class zmq::socket_base_t *,class zmq::socket_base_t *,class zmq::socket_base_t *)" (?proxy@zmq@@YAHPEAVsocket_base_t@1@000@Z)
          zmq.lib(socket_base.obj) : error LNK2019: unresolved external symbol __imp_isdigit referenced in function "public: int __cdecl zmq::socket_base_t::connect(char const *)" (?connect@socket_base_t@zmq@@QEAAHPEBD@Z)
          zmq.lib(socket_base.obj) : error LNK2019: unresolved external symbol __imp_isxdigit referenced in function "public: int __cdecl zmq::socket_base_t::connect(char const *)" (?connect@socket_base_t@zmq@@QEAAHPEBD@Z)
          zmq.lib(socket_base.obj) : error LNK2019: unresolved external symbol __imp_isalnum referenced in function "public: int __cdecl zmq::socket_base_t::connect(char const *)" (?connect@socket_base_t@zmq@@QEAAHPEBD@Z)
          zmq.lib(msg.obj) : error LNK2019: unresolved external symbol __imp_strncpy referenced in function "public: int __cdecl zmq::msg_t::set_group(char const *,unsigned __int64)" (?set_group@msg_t@zmq@@QEAAHPEBD_K@Z)
          zmq.lib(thread.obj) : error LNK2001: unresolved external symbol __imp_strncpy
          zmq.lib(thread.obj) : error LNK2019: unresolved external symbol __imp__beginthreadex referenced in function "public: void __cdecl zmq::thread_t::start(void (__cdecl*)(void *),void *,char const *)" (?start@thread_t@zmq@@QEAAXP6AXPEAX@Z0PEBD@Z)
          zmq.lib(select.obj) : error LNK2019: unresolved external symbol __imp_rand referenced in function "private: unsigned short __cdecl zmq::select_t::get_fd_family(unsigned __int64)" (?get_fd_family@select_t@zmq@@AEAAG_K@Z)
          zmq.lib(random.obj) : error LNK2001: unresolved external symbol __imp_rand
          zmq.lib(random.obj) : error LNK2019: unresolved external symbol __imp_srand referenced in function "void __cdecl zmq::seed_random(void)" (?seed_random@zmq@@YAXXZ)
          zmq.lib(xpub.obj) : error LNK2019: unresolved external symbol __imp_realloc referenced in function "private: void __cdecl zmq::generic_mtrie_t<class zmq::pipe_t>::rm_helper<class zmq::xpub_t *>(class zmq::pipe_t *,unsigned char * *,unsigned __int64,unsigned __int64,void (__cdecl*)(unsigned char const *,unsigned __int64,class zmq::xpub_t *),class zmq::xpub_t *,bool)" (??$rm_helper@PEAVxpub_t@zmq@@@?$generic_mtrie_t@Vpipe_t@zmq@@@zmq@@AEAAXPEAVpipe_t@1@PEAPEAE_K2P6AXPEBE2PEAVxpub_t@1@@Z4_N@Z)
          zmq.lib(mtrie.obj) : error LNK2001: unresolved external symbol __imp_realloc
          zmq.lib(trie.obj) : error LNK2001: unresolved external symbol __imp_realloc
          zmq.lib(ip_resolver.obj) : error LNK2019: unresolved external symbol __imp_isalpha referenced in function "public: int __cdecl zmq::ip_resolver_t::resolve(union zmq::ip_addr_t *,char const *)" (?resolve@ip_resolver_t@zmq@@QEAAHPEATip_addr_t@2@PEBD@Z)
          D:\a\optlevel-z-crt-static-issue\optlevel-z-crt-static-issue\target\release\deps\optlevel_z_crt_static_issue-d8e83ac4c3d865cc.exe : fatal error LNK1120: 11 unresolved externals
oblique commented 5 years ago

@alexcrichton Since you have more experience for -sys and -src crates. Do you have any Ideas on how to fix this? /GL- is already used. Do you think that I should post this in Rust repo?

jean-airoldie commented 5 years ago

I don't have much experience with windows either I'm afraid, so I won't be able to help much.

alexcrichton commented 5 years ago

I'm not so certain myself unfortunately, but this looks like it's sort of a routine linking error where either the C standard library isn't specified or it's the wrong C standard library by accident (/MD vs /MT)

oblique commented 5 years ago

If I add -Copt-level=z in RUSTFLAGS, for some reason it works. In this case -Copt-level=3 -Copt-level=z is passed to rustc (the -Copt-level=3 part is added by cargo). This is more confusing.

I think I'm going to open an issue in Rust. It looks like a bug.