Skip to content

Commit d21b605

Browse files
committed
SQLITECPP_IN_EXTENSION option controlling linking and use of sqlite3ext.h
1 parent f6b3225 commit d21b605

File tree

6 files changed

+34
-2
lines changed

6 files changed

+34
-2
lines changed

CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,13 @@ endif ()
233233
## Build provided copy of SQLite3 C library ##
234234

235235
option(SQLITECPP_INTERNAL_SQLITE "Add the internal SQLite3 source to the project." ON)
236-
if (SQLITECPP_INTERNAL_SQLITE)
236+
option(SQLITECPP_IN_EXTENSION "Use SQLiteCpp in the implementation of a SQLite3 loadable extension" OFF)
237+
if(SQLITECPP_IN_EXTENSION)
238+
# To use within a SQLite3 loadable extension, we must not target_link_libraries() SQLite3 itself; the
239+
# host program will already have loaded it (possibly a statically-linked version) and we'll receive its
240+
# API pointers via sqlite3_extension_init().
241+
target_compile_definitions(SQLiteCpp PUBLIC SQLITECPP_IN_EXTENSION)
242+
elseif (SQLITECPP_INTERNAL_SQLITE)
237243
message(STATUS "Compile sqlite3 from source in subdirectory")
238244
option(SQLITE_ENABLE_JSON1 "Enable JSON1 extension when building internal sqlite3 library." ON)
239245
# build the SQLite3 C library (for ease of use/compatibility) versus Linux sqlite3-dev package
@@ -285,7 +291,7 @@ else (SQLITECPP_INTERNAL_SQLITE)
285291
target_link_libraries(SQLiteCpp PRIVATE ${sqlcipher_LIBRARY})
286292
endif()
287293
endif()
288-
endif (SQLITECPP_INTERNAL_SQLITE)
294+
endif (SQLITECPP_IN_EXTENSION)
289295

290296
# Link target with pthread and dl for Unix
291297
if (UNIX)

src/Backup.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313

1414
#include <SQLiteCpp/Exception.h>
1515

16+
#if !defined(SQLITECPP_IN_EXTENSION) || defined(SQLITE_CORE)
1617
#include <sqlite3.h>
18+
#else
19+
#include <sqlite3ext.h>
20+
extern "C" const sqlite3_api_routines *sqlite3_api;
21+
#endif
1722

1823
namespace SQLite
1924
{

src/Column.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
*/
1111
#include <SQLiteCpp/Column.h>
1212

13+
#if !defined(SQLITECPP_IN_EXTENSION) || defined(SQLITE_CORE)
1314
#include <sqlite3.h>
15+
#else
16+
#include <sqlite3ext.h>
17+
extern "C" const sqlite3_api_routines *sqlite3_api;
18+
#endif
1419

1520
#include <iostream>
1621

src/Database.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@
1515
#include <SQLiteCpp/Exception.h>
1616
#include <SQLiteCpp/Statement.h>
1717

18+
#if !defined(SQLITECPP_IN_EXTENSION) || defined(SQLITE_CORE)
1819
#include <sqlite3.h>
20+
#else
21+
#include <sqlite3ext.h>
22+
extern "C" const sqlite3_api_routines *sqlite3_api;
23+
#endif
24+
1925
#include <fstream>
2026
#include <string.h>
2127

src/Exception.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
*/
1111
#include <SQLiteCpp/Exception.h>
1212

13+
#if !defined(SQLITECPP_IN_EXTENSION) || defined(SQLITE_CORE)
1314
#include <sqlite3.h>
15+
#else
16+
#include <sqlite3ext.h>
17+
extern "C" const sqlite3_api_routines *sqlite3_api;
18+
#endif
1419

1520

1621
namespace SQLite

src/Statement.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
#include <SQLiteCpp/Assertion.h>
1616
#include <SQLiteCpp/Exception.h>
1717

18+
#if !defined(SQLITECPP_IN_EXTENSION) || defined(SQLITE_CORE)
1819
#include <sqlite3.h>
20+
#else
21+
#include <sqlite3ext.h>
22+
extern "C" const sqlite3_api_routines *sqlite3_api;
23+
#endif
1924

2025
namespace SQLite
2126
{

0 commit comments

Comments
 (0)