Including sqlite3ext.h instead of sqlite3.h

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Including sqlite3ext.h instead of sqlite3.h

curmudgeon
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
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Including sqlite3ext.h instead of sqlite3.h

Keith Medcalf

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-
>[hidden email]] On Behalf Of x
>Sent: Saturday, 19 August, 2017 10:41
>To: [hidden email]
>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
>[hidden email]
>http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users



_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Including sqlite3ext.h instead of sqlite3.h

curmudgeon
This morning I tried creating a new blank package on c++ builder and then added

#include “sqlite3ext.h”

Same error message : E245 Undefined symbol ‘sqlite3_api’.

Is there some define I should have set?

Full code


//---------------------------------------------------------------------------
#include "sqlite3ext.h"
#include <System.hpp>
#pragma hdrstop
#pragma package(smart_init)
//---------------------------------------------------------------------------

//   Package source.
//---------------------------------------------------------------------------


#pragma argsused
extern "C" int _libmain(unsigned long reason)
{
        sqlite3 *DB;
        sqlite3_stmt *s;
        sqlite3_open("c:/SQLiteData/Meta.db",&DB); // error line
        sqlite3_close(DB);
        return 1;
}
//---------------------------------------------------------------------------
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Including sqlite3ext.h instead of sqlite3.h

Dan Kennedy-4
On 08/20/2017 02:33 PM, x wrote:
> This morning I tried creating a new blank package on c++ builder and then added
>
> #include “sqlite3ext.h”
>
> Same error message : E245 Undefined symbol ‘sqlite3_api’.
>
> Is there some define I should have set?

Add "SQLITE_EXTENSION_INIT1" on a line by itself after including
sqlite3ext.h:

   http://sqlite.org/loadext.html#programming_loadable_extensions

Dan.



>
> Full code
>
>
> //---------------------------------------------------------------------------
> #include "sqlite3ext.h"
> #include <System.hpp>
> #pragma hdrstop
> #pragma package(smart_init)
> //---------------------------------------------------------------------------
>
> //   Package source.
> //---------------------------------------------------------------------------
>
>
> #pragma argsused
> extern "C" int _libmain(unsigned long reason)
> {
>          sqlite3 *DB;
>          sqlite3_stmt *s;
>          sqlite3_open("c:/SQLiteData/Meta.db",&DB); // error line
>          sqlite3_close(DB);
>          return 1;
> }
> //---------------------------------------------------------------------------
> _______________________________________________
> sqlite-users mailing list
> [hidden email]
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Including sqlite3ext.h instead of sqlite3.h

curmudgeon
In reply to this post by Keith Medcalf
Thanks Dan. It now compiles but gives me warnings about ‘sqlite3_api’ being defined in more than one module. ‘sqlite3ext.h’ is included in several files although I only added the SQLITE_EXTENSION_INIT1 line to one such file.


> This morning I tried creating a new blank package on c++ builder and then added
>
> #include “sqlite3ext.h”
>
> Same error message : E245 Undefined symbol ‘sqlite3_api’.
>
> Is there some define I should have set?

Add "SQLITE_EXTENSION_INIT1" on a line by itself after including
sqlite3ext.h:

   http://sqlite.org/loadext.html#programming_loadable_extensions

Dan.



>
> Full code
>
>
> //---------------------------------------------------------------------------
> #include "sqlite3ext.h"
> #include <System.hpp>
> #pragma hdrstop
> #pragma package(smart_init)
> //---------------------------------------------------------------------------
>
> //   Package source.
> //---------------------------------------------------------------------------
>
>
> #pragma argsused
> extern "C" int _libmain(unsigned long reason)
> {
>          sqlite3 *DB;
>          sqlite3_stmt *s;
>          sqlite3_open("c:/SQLiteData/Meta.db",&DB); // error line
>          sqlite3_close(DB);
>          return 1;
> }
> //---------------------------------------------------------------------------
> _______________________________________________
> sqlite-users mailing list
> [hidden email]
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Including sqlite3ext.h instead of sqlite3.h

curmudgeon
In reply to this post by Dan Kennedy-4
Having read the link Dan suggested I take it I shouldn’t be changing all my sqlite3.h includes to sqlite3ext.h. I should only be using sqlite3ext.h in the file I’m creating for the loadable extension? I’m very short on experience and easily confused when it comes to compiler directives, dll’s, filing etc.

To start off, I’m looking to use the carray extension. Keith has made some suggestions to me regarding adding code to the bottom of the amalgamation file but for the time being I’m trying to follow some of the sqlite documentation. On this page https://www.sqlite.org/carray.html it says

The carray() function is not compiled into SQLite by default. It is available as a loadable extension<https://www.sqlite.org/loadext.html> in the ext/misc/carray.c<https://www.sqlite.org/src/artifact?ci=trunk&filename=ext/misc/carray.c> source file.

When I click on the ext/mis/carray.c link it takes me to a page with source code. Am I supposed to copy and paste that source code or is it already in the amalgamation somewhere? Or is there some files beyond the amalgamation I should be downloading?

Tom


From: Dan Kennedy<mailto:[hidden email]>
Sent: 20 August 2017 09:21
To: [hidden email]<mailto:[hidden email]>
Subject: Re: [sqlite] Including sqlite3ext.h instead of sqlite3.h

On 08/20/2017 02:33 PM, x wrote:
> This morning I tried creating a new blank package on c++ builder and then added
>
> #include “sqlite3ext.h”
>
> Same error message : E245 Undefined symbol ‘sqlite3_api’.
>
> Is there some define I should have set?

Add "SQLITE_EXTENSION_INIT1" on a line by itself after including
sqlite3ext.h:

   http://sqlite.org/loadext.html#programming_loadable_extensions

Dan.



>
> Full code
>
>
> //---------------------------------------------------------------------------
> #include "sqlite3ext.h"
> #include <System.hpp>
> #pragma hdrstop
> #pragma package(smart_init)
> //---------------------------------------------------------------------------
>
> //   Package source.
> //---------------------------------------------------------------------------
>
>
> #pragma argsused
> extern "C" int _libmain(unsigned long reason)
> {
>          sqlite3 *DB;
>          sqlite3_stmt *s;
>          sqlite3_open("c:/SQLiteData/Meta.db",&DB); // error line
>          sqlite3_close(DB);
>          return 1;
> }
> //---------------------------------------------------------------------------
> _______________________________________________
> sqlite-users mailing list
> [hidden email]
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users