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

Viewed 3190

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

2 Answers

You are misreading LLVM's documentation. I believe the part you are referring to is:

Linking against DLL

Done. LLD can read import libraries needed to link against DLL. Both export-by-name and export-by-ordinal are supported.

They do not mean the same thing as MinGW / TinyCC's "direct" (no .lib) linking. They mean literally "LLD can link to shared libraries on Windows and is able to read the .lib files to do so". Which is true, but you still need the .lib, just like MSVC does.

So the answer is, unfortunately, you cannot make Clang/LLD do this at time of writing.

Related