Load Extensions or Write Extensions?

There are a number of C Extensions in the SQLite3 Fossil Repository

https://www.sqlite.org/src/dir?ci=745bc8decd18d4dc&name=ext/misc

These are all written in C so if you are going to stuff them in mangled C++ 
files you need to wrap them in the appropriate directive to prevent the 
pre-processor from converting what it thinks is C++ input to real C that can be 
compiled (usually achieved by wrapping the bits already in C with the extern 
"C" { } directive).  Either that or preferably make sure to compile the 
extensions in C like the amalgamation.

You can also "add extensions" internally by appending them to the amalgamation 
source and then using the -DSQLITE_EXTRA_INIT hook to provide a C function that 
calls auto_extension on each of your embeded extensions so they get added to 
every connection as part if its initialization procedure.


If you want your extensions to work "either way" (that is as independant 
modules and able to embed with no polution to the amalgamation, or perhaps as 
separate modules statically linked with the amalgamation) then your extension 
init should look like this:

#ifdef SQLITE_CORE
    #include "sqlite3.h"
#else
    #ifdef _HAVE_SQLITE_CONFIG_H
        #include "config.h"
    #endif
    #include "sqlite3ext.h"
    SQLITE_EXTENSION_INIT1
#endif

and your init function should look like this:

#ifdef _WIN32
#ifndef SQLITE_CORE
__declspec(dllexport)
#endif
#endif
#ifdef SQLITE_CORE
static
#endif
int sqlite3_sqlmath_init(sqlite3 *db, char **pzErrMsg, const 
sqlite3_api_routines *pApi) ...
{
    SQLITE_EXTENSION_INIT2(pApi);
...
}

This is to avoid poluting the external symbol tables (export tables) with names 
not meant to be accessed outside the module.
Of course, if you are compiling the extension as a separate compilation unit 
that will be statically linked to the amalgamation, then you need to remove the 
"static" qualifier since the init function needs to be in module linkage table.

---
The fact that there's a Highway to Hell but only a Stairway to Heaven says a 
lot about anticipated traffic volume.

>-----Original Message-----
>From: sqlite-users [mailto:sqlite-users-
>boun...@mailinglists.sqlite.org] On Behalf Of x
>Sent: Saturday, 19 August, 2017 10:41
>To: sqlite-users@mailinglists.sqlite.org
>Subject: [sqlite] Including sqlite3ext.h instead of sqlite3.h
>
>I was trying to work out how to load extensions today. Currently I’ve
>just got the amalgamation file included in my project. Started by
>replacing #include “sqlite3.h” with #include “sqlite3ext.h” in all my
>source files.
>
>I then tried compiling the project but received a string of error
>messages stating
>
>E2451 undefined symbol sqlite3_api
>
>All of these errors point to function declarations in my c++ sqlite
>wrapper that contain a sqlite3_ .. call. e.g.
>
>        int64_t ColI64(int i) {return sqlite3_column_int64(stmt,i);}
>
>Am I doing anything obviously wrong?
>
>I’m using C++ builder Berlin on windows 10.
>_______________________________________________
>sqlite-users mailing list
>sqlite-users@mailinglists.sqlite.org
>http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users



_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to