Link static lib made in rust to cpp

Hello,

I have created a very simple lib, with three kind of iterators, but all very simple.
I have no dependencies (except std ofc, with iterator and so)

I'm on windows (sadly...)
I have a "staticlib" lib target in my cargo.toml, I build it in release

I'm trying to use it in a cpp test, link the .lib with a cmake target_link_librairies

aaaaand

wow

LINK Pass 1: command "C:\PROGRA~2\MICROS~1\2019\COMMUN~1\VC\Tools\MSVC\1425~1.286\bin\Hostx64\x64\link.exe /nologo @CMakeFiles\untitled2.dir\objects1.rsp /out:untitled2.exe /implib:untitled2.lib /pdb:C:\Users\33602\CLionProjects\untitled2\cmake-build-debug\untitled2.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console C:\Users\33602\dev\wsl\mini_collider\ugens\target\release\ugens.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\untitled2.dir/intermediate.manifest CMakeFiles\untitled2.dir/manifest.res" failed (exit code 1120) with the following output:
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol WSAStartup referenced in function _ZN4core3ops8function6FnOnce40call_once$u7b$$u7b$vtable.shim$u7d$$u7d$17hc6767b155d52e562E
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol WSACleanup referenced in function _ZN4core3ops8function6FnOnce40call_once$u7b$$u7b$vtable.shim$u7d$$u7d$17hc718bd02d77e7a92E
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol closesocket referenced in function _ZN4core3ptr13drop_in_place17h96e19e83405d6a8fE
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol freeaddrinfo referenced in function _ZN4core3ptr13drop_in_place17hcb2726088f40b87cE
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol GetUserProfileDirectoryW referenced in function _ZN3std3env8home_dir17h80d426f6b25878e8E
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol getpeername referenced in function _ZN3std3net3tcp9TcpStream9peer_addr17hfb6d9f4d7c4b03dfE
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol WSAGetLastError referenced in function _ZN3std3net3tcp9TcpStream9peer_addr17hfb6d9f4d7c4b03dfE
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol shutdown referenced in function _ZN3std3net3tcp9TcpStream8shutdown17h0df78b167cd0e44fE
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol recv referenced in function _ZN3std3net3tcp9TcpStream4peek17h6dbc440c3d57fbd7E
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol setsockopt referenced in function _ZN3std3net3tcp9TcpStream11set_nodelay17h8e0dd9e7bab0a7d9E
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol WSARecv referenced in function _ZN58_$LT$std..net..tcp..TcpStream$u20$as$u20$std..io..Read$GT$13read_vectored17h4f481f125be3b546E
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol send referenced in function _ZN59_$LT$std..net..tcp..TcpStream$u20$as$u20$std..io..Write$GT$5write17h590bf4c9f3dd9242E
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol WSASend referenced in function _ZN59_$LT$std..net..tcp..TcpStream$u20$as$u20$std..io..Write$GT$14write_vectored17h807b41b04058b646E
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol getsockname referenced in function _ZN3std3net3tcp11TcpListener10local_addr17hf40f259be52f4245E
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol accept referenced in function _ZN3std3net3tcp11TcpListener6accept17h311764ff298b7f42E
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol getsockopt referenced in function _ZN3std3net3tcp11TcpListener3ttl17h348433187650111dE
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol ioctlsocket referenced in function _ZN3std3net3tcp11TcpListener15set_nonblocking17hb483466478255131E
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol getaddrinfo referenced in function _ZN104_$LT$std..sys_common..net..LookupHost$u20$as$u20$core..convert..TryFrom$LT$$LP$$RF$str$C$u16$RP$$GT$$GT$8try_from17h4f8281b9602ece33E
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol connect referenced in function _ZN3std10sys_common3net9TcpStream7connect17h27edf08e52989b17E
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol bind referenced in function _ZN3std10sys_common3net11TcpListener4bind17h79fd8eb34f54e2fdE
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol listen referenced in function _ZN3std10sys_common3net11TcpListener4bind17h79fd8eb34f54e2fdE
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol sendto referenced in function _ZN3std10sys_common3net9UdpSocket7send_to17h85573b6fcf0c61a8E
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol WSASocketW referenced in function _ZN3std3sys7windows3net6Socket3new17he53c54ff120bfb7fE
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol select referenced in function _ZN3std3sys7windows3net6Socket15connect_timeout17h09f8b778f3a918fbE
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol WSADuplicateSocketW referenced in function _ZN3std3sys7windows3net6Socket9duplicate17h9918439c755ae3f8E
ugens.lib(std-b913707ba2c8fe6b.std.3w3r55uz-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol recvfrom referenced in function _ZN3std3sys7windows3net6Socket20recv_from_with_flags17hd8c4c969c6b5b3d4E
untitled2.exe : fatal error LNK1120: 26 unresolved externals

gosh, closesocket, connect ??? o_o ? TcpStream !! ??

On linux, I can handle it better and I see that I have to manually link with -lpthread, -ldl and -lm (for some math).

Is that intended or am I doing something wrong ?? Could I have a more 'static' staticlib ??

Thank you

Could you say which Rust windows target are you using, mingw or msvc? I think this is important to know to find the solution (although I myself don’t use windows often, so probably can’t help directly).

I'm using msvc.

In fact it's a way more strange bug that I have
I'm able to make 'working' but not 'bug' which is quite strange...

even on linux (exactly WSL2)

You have to link the target exe with ws2_32.lib on Windows. It may help to add a build.rs file

build.rs:

fn main() {
    println!("cargo:rustc-link-lib=ws2_32");
}

Cargo.toml:

[package]
# ...
build = "build.rs"

Or simply add /L ws2_32.lib flags when linking, it can be added next to the existing kernel32.lib

ok thanks

I just found that https://github.com/rust-lang/rust/issues/43633

In fact, the non working is just using println! which strangely require a lot of stuff :smiley: