TExcelTAD
Home › Build Fundamentals: From Source to Binary › Windows DLLs & Import Libraries

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:

bash · try it yourself
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.exe
app.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:

bash · try it yourself
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)
  

--- exports of mathdll.dll
[Ordinal/Name Pointer] Table
	[   0] add

--- imports of app.exe
	DLL Name: mathdll.dll
	vma:  Hint/Ord Member-Name Bound-To
	2048	    0  add

--- inside the import library
0000000000000000 I __imp_add
0000000000000000 T add

The import library is an ordinary archive (ar format) of tiny stub objects. It defines two symbols for each export:

  • __imp_add: a slot in the executable's Import Address Table that the Windows loader fills with the real address of add. Code compiled with __declspec(dllimport) calls through this slot directly.
  • add: a small thunk (a jump through __imp_add) for code that called add without dllimport.

Without the import library the linker has no idea add lives in a DLL:

bash · try it yourself
mkdir -p win && cd win
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 app.c -o app.obj
lld-link /entry:mainCRTStartup /subsystem:console /nodefaultlib /out:app.exe app.obj
lld-link: error: undefined symbol: __declspec(dllimport) add
>>> referenced by app.obj:(mainCRTStartup)

This is the Windows version of "undefined reference", and the fix is always the same: add the DLL's .lib to the link line (in Visual Studio: Linker → Input → Additional Dependencies; in CMake: target_link_libraries).

The export macro pattern

One header serves two audiences: the library's own sources must say dllexport, the library's users must say dllimport, and on Linux both should be visibility("default"). Every cross-platform C/C++ library has a macro like this (CMake can generate it with GenerateExportHeader):

c
/* mathlib_export.h */
#if defined(_WIN32)
  #if defined(MATHLIB_BUILDING)          /* set with -DMATHLIB_BUILDING while compiling the DLL */
    #define MATHLIB_API __declspec(dllexport)
  #else
    #define MATHLIB_API __declspec(dllimport)
  #endif
#else
  #define MATHLIB_API __attribute__((visibility("default")))
#endif

MATHLIB_API int add(int a, int b);

Other Windows differences worth knowing

  • Static .lib vs import .lib. Both use the .lib extension. A static library contains the code; an import library contains only stubs. A 5 KB foo.lib next to foo.dll is almost certainly an import library.
  • The C runtime. MSVC programs link the C runtime statically (/MT) or as a DLL (/MD). All objects and libraries in one program must agree, or you get duplicate-symbol errors (LNK2005) or two heaps that can't free each other's memory. /MDd and /MTd are the debug variants, and debug and release libraries can't be mixed either.
  • Search order. By default the loader looks in the application's folder before the system folders, which is why Windows applications usually ship their DLLs next to the .exe. Planting a malicious DLL in that folder ("DLL hijacking") is a known attack; signed installers and SetDefaultDllDirectories mitigate it.
  • MinGW. The GCC port for Windows (x86_64-w64-mingw32-gcc) produces the same PE files, uses .a names for import libraries (libfoo.dll.a), and can link directly against a DLL without an import library.
  • Tools: dumpbin /exports foo.dll and dumpbin /imports app.exe ship with Visual Studio; llvm-objdump -p and GNU objdump -p work on any OS.

Test yourself

What does lld-link /dll (or link.exe /DLL) produce besides the DLL?

  1. A PDB only
  2. An import library (.lib) that programs link against
  3. A static library with all the code
  4. A .so file

helper is a global function in the DLL source. Why is it missing from the export table?

  1. It was optimised away
  2. Windows exports only symbols marked __declspec(dllexport) or listed in a .def file
  3. It is static
  4. objdump hides it

How can you usually tell a static .lib from an import .lib?

  1. They have different extensions
  2. An import library is tiny and sits next to a DLL; it holds stubs, not code
  3. Static libraries are always larger than 1 GB
  4. You can't