Protyposis

Travelling through time & space in real-time...

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 really into this stuff. SQLite is one of the positive examples which make it really easy: download the amalgamation source, and follow the DLL compilation guide. The nicest thing for Windows users is that you can just use the Visual Studio compiler to compile x86 and x64 versions without any configuration or dependencies and just a single short 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”).


Comments
Posted at 21:43 May 8, 2015
timwah
Reply
Author

“where nobody is ever going to find it”. I found it, and THANK YOU

Posted at 23:06 August 14, 2015
dougwj
Reply
Author

Thank you, this has been driving me crazy!

Posted at 18:52 October 21, 2015
x64
Reply
Author

so so sooooooo thx

Posted at 22:06 January 4, 2016
Michael
Reply
Author

Found it. Thanks,

Posted at 16:24 October 13, 2016
KaFu
Reply
Author

Found it too :), much appreciated!

Posted at 17:36 November 13, 2016
TheRaven
Reply
Author

Found it myself, and it still fills a void even after all this time.
Windows 10 has achieved a level of complexity that makes everything more prone to confusion and frustration making this post incredibly valuable; many thnx to you for sharing your experience.

Posted at 17:31 January 17, 2017
Vsevolod
Reply
Author

Thanks a lot!

For me (win8.1, sqlite 3.16.2, VS2015 Express) works only:
cl sqlite3.c -DSQLITE_ENABLE_COLUMN_METADATA -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_JSON1 -DSQLITE_API=__declspec(dllexport) -link -dll -out:sqlite3.dll

Posted at 04:03 November 23, 2020
Pesho
Reply
Author

THANKS!

Leave a Reply to x64 Cancel reply