SQLite User Forum

Compiling SQLite3.DLL with FILEIO.C
Login

Compiling SQLite3.DLL with FILEIO.C

(1) By Aask (AAsk1902) on 2023-08-31 07:55:38 [source]

5. Building A Windows DLL does not indicate how to compile SQLITE3.DLL with an extension.

For Windows, how can I compile the DLL with the fileio.c extension?

(2.1) By Nuno Cruces (ncruces) on 2025-01-18 09:23:18 edited from 2.0 in reply to 1 [link] [source]

If you can compile such a DLL from the amalgamation, one possible hacky fix is to create a file like this one:

#define SQLITE_OMIT_AUTOINIT

// Amalgamation
#include "sqlite3.c"
// Extensions
#include "ext/anycollseq.c"
#include "ext/base64.c"
#include "ext/decimal.c"
#include "ext/ieee754.c"
#include "ext/regexp.c"
#include "ext/series.c"
#include "ext/spellfix.c"
#include "ext/uint.c"

__attribute__((constructor)) void init() {
  sqlite3_initialize();
  sqlite3_auto_extension((void (*)(void))sqlite3_base_init);
  sqlite3_auto_extension((void (*)(void))sqlite3_decimal_init);
  sqlite3_auto_extension((void (*)(void))sqlite3_ieee_init);
  sqlite3_auto_extension((void (*)(void))sqlite3_regexp_init);
  sqlite3_auto_extension((void (*)(void))sqlite3_series_init);
  sqlite3_auto_extension((void (*)(void))sqlite3_spellfix_init);
  sqlite3_auto_extension((void (*)(void))sqlite3_uint_init);
}

Place it besides the amalgamation (sqlite3.c), add the desired extensions in an ext folder (and tweak the above - pay attention to the exact _init function's name), then compile it as if you were compiling sqlite3.c. www:/howtocompile.html#building_a_windows_dll

PS: it seems __attribute__((constructor)) doesn't work with MSVC. I'm a bit spoiled by clang/GCC. Does MinGW work for you?

(3) By Aask (AAsk1902) on 2025-01-18 19:48:50 in reply to 2.1 [link] [source]

Thanks for the guidelines.

it seems __attribute__((constructor)) doesn't work with MSVC. ... Does MinGW work for you?

I do not have MinGW.

Not knowing C is enough of an affliction without opening pathways to another compiler of which I have no knowledge.

(4) By Nuno Cruces (ncruces) on 2025-01-18 23:39:02 in reply to 3 [link] [source]

OK then, try something like this:

#define SQLITE_EXTRA_INIT extra_init

#ifdef _WIN32
#define SQLITE_API __declspec(dllexport)
#endif

// Amalgamation
#include "sqlite3.c"
// Extensions
#include "ext/anycollseq.c"
#include "ext/base64.c"
#include "ext/decimal.c"
#include "ext/ieee754.c"
#include "ext/regexp.c"
#include "ext/series.c"
#include "ext/spellfix.c"
#include "ext/uint.c"

int extra_init(const char* dummy) {
  int n = 0;
  n += sqlite3_auto_extension((void (*)(void))sqlite3_base_init);
  n += sqlite3_auto_extension((void (*)(void))sqlite3_decimal_init);
  n += sqlite3_auto_extension((void (*)(void))sqlite3_ieee_init);
  n += sqlite3_auto_extension((void (*)(void))sqlite3_regexp_init);
  n += sqlite3_auto_extension((void (*)(void))sqlite3_series_init);
  n += sqlite3_auto_extension((void (*)(void))sqlite3_spellfix_init);
  n += sqlite3_auto_extension((void (*)(void))sqlite3_uint_init);
  return n ? SQLITE_ERROR : SQLITE_OK;
}

(5.2) By Aask (AAsk1902) on 2025-01-19 08:13:01 edited from 5.1 in reply to 4 [link] [source]

Thanks for your patience.

The amalgamation has just these files:shell.c sqlite3.c sqlite3.h sqlite3ext.h

I am somewhat confused since in this block of your suggestion:

// Amalgamation
#include "sqlite3.c"
// Extensions
#include "ext/anycollseq.c"
#include "ext/base64.c"
#include "ext/decimal.c"
#include "ext/ieee754.c"
#include "ext/regexp.c"
#include "ext/series.c"
#include "ext/spellfix.c"
#include "ext/uint.c"
  1. the files listed are NOT in the amalgamation e.g. #include "ext/anycollseq.c" etc.
  2. I do not see FILEIO.C

(6) By Nuno Cruces (ncruces) on 2025-01-19 11:36:00 in reply to 5.2 [link] [source]

This is an example that includes several extensions I use, none of which are fileio.c.

I understand you don't know C, but I don't have a Windows machine, so I'd rather you tried to learn enough C to understand what I'm trying to explain, rather than just copying something verbatim and building it without much consideration.

So, please try this, if it makes sense:

  1. replace those #include lines with the extension(s) you need
  2. replace the sqlite3_auto_extension with one that calls the initialization function from the extension(s) you need

To explain a bit what's going on: #include <FILE> is like copy pasting the contents of a file into this new file; #define is like defining a constant.

So what you're actually doing is:

  1. telling SQLite there's an additional initialization function it should call (named extra_init), before
  2. copy pasting the amalgamation, then
  3. appending a copy of all extensions you want, and finally
  4. defining the extra_init function, which I initializes all extensions and reports any errors.

Does that make sense?

(7.2) By Aask (AAsk1902) on 2025-01-19 13:09:56 edited from 7.1 in reply to 6 [link] [source]

Does that make sense?

Very very slowly beginning to ... (as always it is easy when you know how and I am not there yet!)

My custom file looks as folows:

#define SQLITE_EXTRA_INIT extra_init

#ifdef _WIN32
#define SQLITE_API __declspec(dllexport)
#endif

// Amalgamation
#include "sqlite3.c"
// Extensions
#include "ext/fileio.c"

int extra_init(const char* dummy) {
  int n = 0;
  n += sqlite3_auto_extension((void (*)(void))sqlite3_fileio_init);
  return n ? SQLITE_ERROR : SQLITE_OK;
}

I got an error relating to test_windirent.h; I downloaded this file.

Then I hit these errors:

error LNK2019: unresolved external symbol _opendir referenced in function _fsdirSetErrmsg
error LNK2019: unresolved external symbol _readdir referenced in function _fsdirSetErrmsg
error LNK2019: unresolved external symbol _closedir referenced in function _fsdirResetCursor

(8.2) By Nuno Cruces (ncruces) on 2025-01-19 14:56:51 edited from 8.1 in reply to 7.2 [link] [source]

OK, then you should be close(r).

It seems you need test_windirent.c and test_windirent.h too.

Add them to the ext folder, and #include them after the amalgamation:

// Amalgamation
#include "sqlite3.c"
// Needed by extensions
#include "ext/test_windirent.c"
// Extensions
#include "ext/fileio.c"

(you don't need an ext folder, you can organize them however you want, just adjust the paths)

(9.1) By Aask (AAsk1902) on 2025-01-19 16:42:51 edited from 9.0 in reply to 8.2 [link] [source]

Thank you for your help; much appreciated.

Managed to build the DLL which incorporates fileio.c.

My file is called custom.c and I end up with custom.dll by default (which I renamed sqlite3.DLL).

(Now on to some exhaustive testing.)