sqlite3: Compile Windows DLL
Compile sqlite3 as a DLL on 32bit Windows with Visual Studio 2010 for use with a Borland Delphi 7 native application.
Motivation
Legacy code written in Delphi runs in many places, I wanted to add support to such an application that would allow it to access an SQLite3 database from within the application.
How to Compile
The instructions on the sqlite3 website
offer a command line that will compile the code but will not export the
function names (and so the result was quite useless). This is the recommended
command, and result of dumpbin
:
> cl sqlite3.c -link -dll -out:sqlite3.dll
> dumpbin /exports sqlite3.dll
Microsoft (R) COFF/PE Dumper Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file sqlite3.dll
File Type: DLL
Summary
C000 .data
9000 .rdata
4000 .reloc
BB000 .text
The above was executed in the Visual Studio Command Prompt (2010) where the source is located.
The modification is quite simple, simply add an export define to the compile command line:
> cl /DSQLITE_API=__declspec(dllexport) sqlite3.c -link -dll -out:sqlite3.dll
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
sqlite3.c
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:sqlite3.exe
-dll
-out:sqlite3.dll
sqlite3.obj
Creating library sqlite3.lib and object sqlite3.exp
Notice that the linker output includes the creation of a .lib
and .exp
file which was not present the first time, now to view the exports:
> dumpbin /exports sqlite3.dll
Microsoft (R) COFF/PE Dumper Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file sqlite3.dll
File Type: DLL
Section contains the following exports for sqlite3.dll
00000000 characteristics
6440E7E7 time date stamp Thu Apr 20 08:21:11 2023
0.00 version
1 ordinal base
280 number of functions
280 number of names
ordinal hint RVA name
1 0 00027D70 sqlite3_aggregate_context
2 1 00027F90 sqlite3_aggregate_count
3 2 00053F20 sqlite3_auto_extension
4 3 000A8040 sqlite3_autovacuum_pages
...
Great, now a sqlite3.dll
file has been created with all the required exports
that will allow linkage with the Delphi (or any other) application.