Compiling SQLite as DLL with MSVC

Compiling DLLs on your own can get really messy and take a lot of time (e.g., FFmpeg) if you are not familiar with this stuff. SQLite is one of the positive examples which make it really easy: download the amalgamation source, and follow the DLL compilation guide. Windows users can just use the Visual Studio compiler to compile x86 and x64 versions with a single configuration-less command, and don’t need to fool around with Cygwin/MinGW/MSYS/MinGW-W64/Win-builds etc.

Unfortunately there’s a small detail missing in the SQLite guide, and they don’t seem to have an easy way of contacting them except for an antiquated mailing list. So I’m posting here where nobody is ever going to find it:

cl sqlite3.c -link -dll -out:sqlite3.dll

The line above is the recommended command for the MSVC command prompt to compile an SQLite DLL. The problem is, it does not export any functions, does not create a .lib for native programming, and cannot be p/invoked from .NET/C# (raising an EntryPointNotFoundException).

The solution is to export the API functions by setting the SQLITE_API preprocessor definition:

cl sqlite3.c -DSQLITE_API=__declspec(dllexport) -link -dll -out:sqlite3.dll

To test if your DLL is exporting functions, you can either run dumpbin /exports sqlite3.dll on the command line or use the Dependency Walker and check the export function list (where the first column is called “E”).