diff --git a/CMakeLists.txt b/CMakeLists.txt index b651f472..123f84cb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -233,7 +233,13 @@ endif () ## Build provided copy of SQLite3 C library ## option(SQLITECPP_INTERNAL_SQLITE "Add the internal SQLite3 source to the project." ON) -if (SQLITECPP_INTERNAL_SQLITE) +option(SQLITECPP_IN_EXTENSION "Use SQLiteCpp in the implementation of a SQLite3 loadable extension" OFF) +if(SQLITECPP_IN_EXTENSION) + # To use within a SQLite3 loadable extension, we must not target_link_libraries() SQLite3 itself; the + # host program will already have loaded it (possibly a statically-linked version) and we'll receive its + # API pointers via sqlite3_extension_init(). + target_compile_definitions(SQLiteCpp PUBLIC SQLITECPP_IN_EXTENSION) +elseif (SQLITECPP_INTERNAL_SQLITE) message(STATUS "Compile sqlite3 from source in subdirectory") option(SQLITE_ENABLE_JSON1 "Enable JSON1 extension when building internal sqlite3 library." ON) # build the SQLite3 C library (for ease of use/compatibility) versus Linux sqlite3-dev package @@ -285,7 +291,7 @@ else (SQLITECPP_INTERNAL_SQLITE) target_link_libraries(SQLiteCpp PRIVATE ${sqlcipher_LIBRARY}) endif() endif() -endif (SQLITECPP_INTERNAL_SQLITE) +endif (SQLITECPP_IN_EXTENSION) # Link target with pthread and dl for Unix if (UNIX) diff --git a/src/Backup.cpp b/src/Backup.cpp index 17a13e2b..3e2ba335 100644 --- a/src/Backup.cpp +++ b/src/Backup.cpp @@ -13,7 +13,12 @@ #include +#if !defined(SQLITECPP_IN_EXTENSION) || defined(SQLITE_CORE) #include +#else +#include +extern "C" const sqlite3_api_routines *sqlite3_api; +#endif namespace SQLite { diff --git a/src/Column.cpp b/src/Column.cpp index b3960fef..6b626b1b 100644 --- a/src/Column.cpp +++ b/src/Column.cpp @@ -10,7 +10,12 @@ */ #include +#if !defined(SQLITECPP_IN_EXTENSION) || defined(SQLITE_CORE) #include +#else +#include +extern "C" const sqlite3_api_routines *sqlite3_api; +#endif #include diff --git a/src/Database.cpp b/src/Database.cpp index 7b81f8b9..d9f5bd53 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -15,7 +15,13 @@ #include #include +#if !defined(SQLITECPP_IN_EXTENSION) || defined(SQLITE_CORE) #include +#else +#include +extern "C" const sqlite3_api_routines *sqlite3_api; +#endif + #include #include diff --git a/src/Exception.cpp b/src/Exception.cpp index 2840787e..cdd5e2a8 100644 --- a/src/Exception.cpp +++ b/src/Exception.cpp @@ -10,7 +10,12 @@ */ #include +#if !defined(SQLITECPP_IN_EXTENSION) || defined(SQLITE_CORE) #include +#else +#include +extern "C" const sqlite3_api_routines *sqlite3_api; +#endif namespace SQLite diff --git a/src/Statement.cpp b/src/Statement.cpp index 8ab68624..0f838dc4 100644 --- a/src/Statement.cpp +++ b/src/Statement.cpp @@ -15,7 +15,12 @@ #include #include +#if !defined(SQLITECPP_IN_EXTENSION) || defined(SQLITE_CORE) #include +#else +#include +extern "C" const sqlite3_api_routines *sqlite3_api; +#endif namespace SQLite {