SQLite Forum

Compiling FILEIO.C
Login
> Compiling is not as straight forward as made out here.

True. I am about to add an expando-tip to that "usually works" claim. It's author and I are not accustomed to using computers which do not have a compiler, linker, library manipulator, debugger, symbol table dumper, and other tools available with a few keystrokes at a command line shell. So read "usually" as "usually for the supposed target audience."

Given that tool setup [described in post #2](https://sqlite.org/forum/forumpost/188a920d07823b65?t=h), fileio.c is still not going to build as easily as described in [Compiling A Loadable Extension](https://sqlite.org/loadext.html#build). This is unusual because fileio.c, when built for WIN32, has (apparently) been designed to link against a semi-public API of the SQLite library. (See sqlite3_win32_utf8_to_unicode if curious.)

To build a standalone DLL for WIN32, this code can be used:

<code>
/\* utf8_to_wide.c
.
 Fixup for SQLite project's ext/misc/fileio.c extension which appears to
 require static linking with the SQLite library to get one function that
 is unavailable otherwise and reuse several of that library's functions
 which are available from the C runtime. This wrapper provides the said
 unavailable function and #define's the others to use C runtime versions.
 This allows the extension to be built and used as a DLL.
.
 Build thusly on Windows using the MSVC development tool set:
  CL -Os utf8_to_wide.c -link -dll -out:fileio.dll
.
 In addition to this file, these 3 project sources are required:
 https://www.sqlite.org/src/file?name=ext/misc/fileio.c&ci=trunk
 https://www.sqlite.org/src/file?name=src/test_windirent.c&ci=trunk
 https://www.sqlite.org/src/file?name=src/test_windirent.h&ci=trunk 
\*/
\#undef sqlite3_win32_utf8_to_unicode
\#define sqlite3_win32_utf8_to_unicode utf8_to_utf16
\#include "fileio.c"
/* Replacement for SQLite library API: */
LPWSTR utf8_to_utf16(const char *z){
  int nAllot = MultiByteToWideChar(CP_UTF8, 0, z, -1, NULL, 0);
  LPWSTR rv = malloc(nAllot * sizeof(WCHAR));
  if( rv!=0 && 0 < MultiByteToWideChar(CP_UTF8, 0, z, -1, rv, nAllot) )
    return rv;
  free(rv);
  return 0;
}
\#undef sqlite3_malloc
\#define sqlite3_malloc malloc
\#undef sqlite3_free
\#define sqlite3_free free
\#undef sqlite3_stricmp
\#define sqlite3_stricmp stricmp
\#include "test_windirent.c"
</code>

After saving the above as utf8_to_wide.c in some directory, and placing sqlite3.h, sqlite3ext.h and the 3 above-referenced sources in that same directory, see its heading comment for the one-liner build invocation.