Skip to content

[Rust] [Windows] [Clang] Linking issues with native x86_64-pc-windows-msvc compile or when cross compile with x86_64-pc-windows-gnu #14366

Open
@kasper93

Description

@kasper93

Describe the bug
There seem to be issues when statically linking static modules with C ones in single binary. I was testing it and works on every platform we target, except Windows, in both cross and native compile scenario. mpv-player/mpv#16051


native compile, clang + rustc --target x86_64-pc-windows-msvc:

Final link step have mixed gnu and msvc style arguments in clang invocation. Resulting in failure like

FAILED: test/language.exe test/language.pdb
"clang"  -Wl,/OUT:test/language.exe test/language.exe.p/language.c.obj "-Wl,/nologo" "-fuse-ld=lld-link" "-Wl,/DEBUG" "-Wl,/PDB:test\language.pdb" "test/libtest-utils.a" "subprojects/ffmpeg/libavutil.a" "subprojects/vulkan-loader/vulkan.lib" "liblibmpv_rs.a" "subprojects/isolang-2.4.0/libisolang.rlib" "subprojects/phf-0.11.3/libphf.rlib" "subprojects/phf_shared-0.11.3/libphf_shared.rlib" "subprojects/siphasher-1.0.1/libsiphasher.rlib" "-lws2_32" "-luser32" "-lbcrypt" "-limm32" "-lntdll" "-lpathcch" "-lws2_32" "-luser32" "-lbcrypt" "kernel32.lib" "advapi32.lib" "ntdll.lib" "userenv.lib" "ws2_32.lib" "dbghelp.lib" "/defaultlib:msvcrt" "-Wl,/SUBSYSTEM:CONSOLE" "-lkernel32" "-lgdi32" "-lwinspool" "-lshell32" "-lole32" "-loleaut32" "-luuid" "-lcomdlg32" "-ladvapi32"
clang: error: no such file or directory: 'kernel32.lib'
clang: error: no such file or directory: 'advapi32.lib'
clang: error: no such file or directory: 'ntdll.lib'
clang: error: no such file or directory: 'userenv.lib'
clang: error: no such file or directory: 'ws2_32.lib'
clang: error: no such file or directory: 'dbghelp.lib'
clang: error: no such file or directory: '/defaultlib:msvcrt'

Note that clang expects -lkernel32 which is also present in the command line, but things kernel32.lib are bogus. See for full log: https://github.com/mpv-player/mpv/actions/runs/13860237310/job/38786647135


cross compile, gcc + rustc --target x86_64-pc-windows-gnu:

Multiple missing symbols, looks like required libraries are not added to the link

/usr/bin/x86_64-w64-mingw32-ld: liblibmpv_rs.a(std-54d88ed746954cc3.std.e21d6b74f58e833-cgu.0.rcgu.o): in function `std::sys::pal::windows::net::init::{{closure}}':
/rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library\std\src\sys\pal\windows/net.rs:122:(.text+0x1810): undefined reference to `WSAStartup'
/usr/bin/x86_64-w64-mingw32-ld: liblibmpv_rs.a(std-54d88ed746954cc3.std.e21d6b74f58e833-cgu.0.rcgu.o): in function `core::mem::maybe_uninit::MaybeUninit<T>::write':
/rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library\core\src\mem/maybe_uninit.rs:512:(.text+0x1821): undefined reference to `WSACleanup'
...

See for full long https://github.com/mpv-player/mpv/actions/runs/13860237310/job/38786647130 it's too much to paste here.


To Reproduce
You can try any mixed C / Rust project that links together statically into one binary.

You can test this with mpv-player/mpv#16051 the build steps are defined in build.yml and (build-mingw64.sh)[https://github.com/mpv-player/mpv/blob/master/ci/build-mingw64.sh], but you can just clone the repo and use the standard meson steps, the build-system is pretty standard.

Expected behavior
Link successfully.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions