Windows DLLs & Import Libraries
Windows has the same ideas as Linux (object files, static libraries, shared libraries, a loader) with different names, a different file format and two habits that surprise Linux developers: nothing is exported unless you say so, and linking against a DLL needs a separate import library.
| Concept | Linux (ELF) | Windows (PE/COFF) |
|---|---|---|
| object file | .o |
.obj |
| static library | .a (made by ar) |
.lib (made by lib.exe) |
| shared library | .so |
.dll |
| what you link against | the .so itself |
an import library .lib that describes the DLL |
| exported symbols | all globals by default | only those marked __declspec(dllexport) or listed in a .def file |
| run-time search | rpath/runpath, LD_LIBRARY_PATH, ldconfig cache |
the executable's folder first, then system folders and PATH (simplified) |
| inspect | nm, readelf, objdump, ldd |
dumpbin /exports, dumpbin /imports, Dependencies (the modern Dependency Walker) |
Building a DLL without Windows
Clang can target Windows from Linux, and LLVM's linker has an MSVC-compatible front end, lld-link, that accepts link.exe-style options. That's enough to build a DLL and an executable that imports it, and to inspect them with GNU objdump, which understands PE files. Running them needs Windows (or Wine), and a real program needs the Windows SDK and C runtime libraries, which this machine doesn't have. So the executable below has its own entry point and no C runtime at all:
mkdir -p win && cd win
cat > mathdll.c <<'SRC'
__declspec(dllexport) int add(int a, int b) { return a + b; }
int helper(int x) { return x * 2; } /* not exported */
SRC
cat > app.c <<'SRC'
__declspec(dllimport) int add(int a, int b);
int mainCRTStartup(void) { return add(2, 3); } /* entry point, no C runtime */
SRC
clang --target=x86_64-pc-windows-msvc -c mathdll.c -o mathdll.obj
clang --target=x86_64-pc-windows-msvc -c app.c -o app.obj
lld-link /dll /noentry /out:mathdll.dll mathdll.obj
lld-link /entry:mainCRTStartup /subsystem:console /nodefaultlib /out:app.exe app.obj mathdll.lib
ls
file mathdll.obj | cut -d, -f1
file mathdll.dll mathdll.lib app.exeapp.c
app.exe
app.obj
mathdll.c
mathdll.dll
mathdll.lib
mathdll.obj
mathdll.obj: Intel amd64 COFF object file
mathdll.dll: PE32+ executable (DLL) (GUI) x86-64, for MS Windows, 3 sections
mathdll.lib: current ar archive
app.exe: PE32+ executable (console) x86-64, for MS Windows, 3 sections
(The cut hides the rest of the .obj description, which includes a creation time: Clang writes the current time into COFF object headers unless you pass -mno-incremental-linker-compatible, a detail that matters for Reproducible Builds.) lld-link /dll produced two files: the DLL and mathdll.lib, its import library. The executable was linked against mathdll.lib; the DLL itself was not needed at link time.
Exports and imports
The DLL's export table lists only add: helper is a global function, but it wasn't marked dllexport. The executable's import table names the DLL and the function, like NEEDED plus a symbol list on Linux:
mkdir -p win && cd win
printf '__declspec(dllexport) int add(int a, int b) { return a + b; }\nint helper(int x) { return x * 2; }\n' > mathdll.c
printf '__declspec(dllimport) int add(int a, int b);\nint mainCRTStartup(void) { return add(2, 3); }\n' > app.c
clang --target=x86_64-pc-windows-msvc -c mathdll.c -o mathdll.obj
clang --target=x86_64-pc-windows-msvc -c app.c -o app.obj
lld-link /dll /noentry /out:mathdll.dll mathdll.obj
lld-link /entry:mainCRTStartup /subsystem:console /nodefaultlib /out:app.exe app.obj mathdll.lib
echo "--- exports of mathdll.dll"
objdump -p mathdll.dll | sed -n '/\[Ordinal\/Name Pointer\] Table/,/^$/p'
echo "--- imports of app.exe"
objdump -p app.exe | sed -n '/DLL Name/,/^$/p'
echo "--- inside the import library"
nm mathdll.lib | grep -E ' [TI] (add|__imp_add)