cwindowsdllclang

How do you make clang link directly to a DLL in Windows without a .lib


TinyCC and GCC both have supported lib-less linking, in favor of directly linking to a DLL file for some time (since lib's haven't had real purpose since Win3.1). But for some reason in Windows, Clang insists on interpreting the .dll file as a .lib file. According to LLVM's page, here, https://lld.llvm.org/windows_support.html, lld-link does support direct dll linkage, but in practice, I'm not seeing any way to specify. (This is true with LLVM 10.0 and 11.0)

To be clear, I'm not referring to manual loading with LoadLibrary and GetProcAddress. I'm referring to invoking the compiler like this:

"C:\Program Files\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0\mingw64\bin\gcc" -o rdtest.exe rdtest.c -lgdi32 -luser32 openvr_api.dll C:\windows\system32\opengl32.dll C:\windows\system32\msvcrt.dll

^^ Works

"C:\Program Files\LLVM\bin\clang.exe" -fuse-ld=lld-link -v -o rdtest.exe rdtest.c -lgdi32 -luser32 openvr_api.dll C:\windows\system32\opengl32.dll C:\windows\system32\msvcrt.dll
[...]
1 warning generated.
 "C:\\Program Files\\LLVM\\bin\\lld-link" -out:rdtest.exe -defaultlib:libcmt "-libpath:C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Professional\\VC\\Tools\\MSVC\\14.28.29333\\lib\\x64" "-libpath:C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Professional\\VC\\Tools\\MSVC\\14.28.29333\\atlmfc\\lib\\x64" "-libpath:C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.18362.0\\ucrt\\x64" "-libpath:C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.18362.0\\um\\x64" -nologo "C:\\Users\\cnlohr\\AppData\\Local\\Temp\\rdtest-a9472b.o" gdi32.lib user32.lib openvr_api.dll "C:\\windows\\system32\\opengl32.dll" "C:\\windows\\system32\\msvcrt.dll"
lld-link: error: openvr_api.dll: bad file type. Did you specify a DLL instead of an import library?
lld-link: error: C:\windows\system32\opengl32.dll: bad file type. Did you specify a DLL instead of an import library?
lld-link: error: C:\windows\system32\msvcrt.dll: bad file type. Did you specify a DLL instead of an import library?
clang: error: linker command failed with exit code 1 (use -v to see invocation)

^^ Fails


Solution

  • This was implemented recently upstream https://reviews.llvm.org/rGa9ff1ce1b9a52add7557cf0579d424c9d0678860 and was backported to the MSYS2 llvm package.