Search notes:

Creating a DLL with a .def file

The following files try to be a minimal example on how to create a DLL with the Visual C compiler and a .def file.

dll.def

The def file (dll.def) lists the exported functions. We only have one exported function (add).
LIBRARY the
EXPORTS
  add
Github repository about-cl, path: /dll/def/dll.def

dll.c

dll.c is the c source file for the dll with the exported function add. Note how the function is declared with the __stdcall calling convention.
#include "dll.h"

int __stdcall add(int a, int b) {
  return a+b;
}
Github repository about-cl, path: /dll/def/dll.c

dll.h

dll.h is the header file which is needed for the executables that use the DLL.
int __stdcall add(int, int);
Github repository about-cl, path: /dll/def/dll.h

main.c

main.c is used to create the executable that uses the dll.
#include <stdio.h>
#include "dll.h"

int main() {

  printf("18+24 = %d\n", add(18, 24));

}
Github repository about-cl, path: /dll/def/main.c

Makefile

This make file (to be invoked with nmake) creates the dll and the executable that uses the dll.
all: the.dll main.exe

main.exe: main.c
	@cl /nologo main.c the.lib

the.dll: dll.obj
	@link /nologo /dll dll.obj /def:dll.def /out:the.dll

dll.obj: dll.c
	@cl /nologo /c dll.c /Fodll.obj

clean:
	@if exist *.obj del *.obj
	@if exist *.dll del *.dll
	@if exist *.exe del *.exe
	@if exist *.exp del *.exp
	@if exist *.lib del *.lib
Github repository about-cl, path: /dll/def/Makefile

Index