Rust compiler error when linking with windows dll

i have a strange error with building android emulator on windows. It seems that MSVC compiler cannot link a rust code with windows .dll library
Does anyone have suggestions how could i resolve this issue?

[5038/9992] Linking CXX executable netsim.exe
FAILED: netsim.exe
cmd.exe /C "cd . && D:\emu\prebuilts\cmake\windows-x86\bin\cmake.exe -E vs_link_exe --intdir=android\third_party\netsim\CMakeFiles\netsim.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\x64\mt.exe --manifests  -- D:\emu\prebuilts\clang\host\windows-x86\clang-r487747\bin\lld-link.exe /nologo @CMakeFiles\netsim.rsp  /out:netsim.exe /implib:archives\netsim.lib /pdb:build\debug_info\netsim.pdb /version:0.0 /IGNORE:4099 /DEBUG /NODEFAULTLIB:LIBCMT /MANIFEST:NO /INCREMENTAL:NO /subsystem:console  && cmd.exe /C "cd /D D:\emu\external\qemu\android\objs\android\third_party\netsim && D:\emu\prebuilts\cmake\windows-x86\bin\cmake.exe -E make_directory D:/emu/external/qemu/android/objs/build/symbols && D:\emu\external\qemu\android\objs\dump_syms.exe D:/emu/external/qemu/android/objs/netsim.exe > D:/emu/external/qemu/android/objs/build/symbols/netsim.sym && echo "Not uploading symbols." && C:\Users\mzutic\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe D:/emu/external/qemu/android/build/python/aemu/symbol_processor.py -o D:/emu/external/qemu/android/objs/build/symbols D:/emu/external/qemu/android/objs/build/symbols/netsim.sym""
LINK: command "D:\emu\prebuilts\clang\host\windows-x86\clang-r487747\bin\lld-link.exe /nologo @CMakeFiles\netsim.rsp /out:netsim.exe /implib:archives\netsim.lib /pdb:build\debug_info\netsim.pdb /version:0.0 /IGNORE:4099 /DEBUG /NODEFAULTLIB:LIBCMT /MANIFEST:NO /INCREMENTAL:NO /subsystem:console" failed (exit code 1) with the following output:
lld-link: error: undefined symbol: __declspec(dllimport) NtCreateFile

>>> referenced by /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library\std\src\sys\windows\fs.rs:829

>>>               frontend_proto.lib(std-4e8f46f7d0c95848.std.8cc7dfd4267f2efd-cgu.0.rcgu.o):(std::sys::windows::fs::open_link_no_reparse::h8e579f91ed4cd982)



lld-link: error: undefined symbol: __declspec(dllimport) RtlNtStatusToDosError

>>> referenced by /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library\std\src\sys\windows\fs.rs:858

>>>               frontend_proto.lib(std-4e8f46f7d0c95848.std.8cc7dfd4267f2efd-cgu.0.rcgu.o):(std::sys::windows::fs::open_link_no_reparse::h8e579f91ed4cd982)

>>> referenced by /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library\std\src\sys\windows\handle.rs:273

>>>               frontend_proto.lib(std-4e8f46f7d0c95848.std.8cc7dfd4267f2efd-cgu.0.rcgu.o):(std::sys::windows::handle::Handle::synchronous_read::hfa39d6a5f17db40d)

>>> referenced by /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library\std\src\sys\windows\handle.rs:319

>>>               frontend_proto.lib(std-4e8f46f7d0c95848.std.8cc7dfd4267f2efd-cgu.0.rcgu.o):(std::sys::windows::handle::Handle::synchronous_write::h017470e7d9d95434)



lld-link: error: undefined symbol: __declspec(dllimport) NtReadFile

>>> referenced by /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library\std\src\sys\windows\handle.rs:242

>>>               frontend_proto.lib(std-4e8f46f7d0c95848.std.8cc7dfd4267f2efd-cgu.0.rcgu.o):(std::sys::windows::handle::Handle::synchronous_read::hfa39d6a5f17db40d)



lld-link: error: undefined symbol: __declspec(dllimport) NtWriteFile

>>> referenced by /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library\std\src\sys\windows\handle.rs:291

>>>               frontend_proto.lib(std-4e8f46f7d0c95848.std.8cc7dfd4267f2efd-cgu.0.rcgu.o):(std::sys::windows::handle::Handle::synchronous_write::h017470e7d9d95434)

[5046/9992] Building CXX object android/emu/feature/CMakeFiles/android-emu-feature.dir/src/android/opengl/gpuinfo.cpp.obj
ninja: build stopped: subcommand failed.
Build failure due to D:\emu\prebuilts\cmake\windows-x86\bin\cmake.EXE --build D:\emu\external\qemu\android\objs --target install Status: 1 != 0

what is the content of the response file CMakeFiles\netsim.rsp? the unresolved symbols are system calls from ntdll and it should be able to link if you have correctly installed Windows Kits and configured the linker.

the log indicates you are using linker from (possibly a custom build of) clang-msvc. did you try to use the default linker from msvc?

Basically you need to add ntdll.lib to your imports. Well technically you have to add all the libs your dll uses but some are implicit (e.g. kernel32.lib).

Currently the full list of import libraries looks something like: ws2_32.lib, bcrypt.lib, ntdll.lib, kernel32.lib and advapi32.lib. However, not all of those will be used by any particular dll.

If you pass --print native-static-libs to rustc when compiling, it will show you all the linker args necessary to successfully link the produced staticlib.

2 Likes

This works but is slightly borked on stable and beta. It shows a very long list of mostly duplicates. This is fixed in nightly though.