diff --git a/CMakeLists.txt b/CMakeLists.txt index 5964791..6d34baa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -213,6 +213,13 @@ if (SQLITE_USE_LEGACY_STRUCT) target_compile_definitions(SQLiteCpp PUBLIC SQLITE_USE_LEGACY_STRUCT) endif (SQLITE_USE_LEGACY_STRUCT) +if(BUILD_SHARED_LIBS) + if(WIN32) + add_definitions("-DSQLITECPP_COMPILE_DLL") + target_compile_definitions(SQLiteCpp PRIVATE "SQLITECPP_DLL_EXPORT") + endif() +endif() + option(SQLITE_OMIT_LOAD_EXTENSION "Enable omit load extension" OFF) if (SQLITE_OMIT_LOAD_EXTENSION) # Enable the user definition of load_extension(). @@ -402,8 +409,10 @@ endif (SQLITECPP_RUN_DOXYGEN) option(SQLITECPP_BUILD_EXAMPLES "Build examples." OFF) if (SQLITECPP_BUILD_EXAMPLES) + # add the basic example executable add_executable(SQLiteCpp_example1 ${SQLITECPP_EXAMPLES}) + target_link_libraries(SQLiteCpp_example1 SQLiteCpp) if (MSYS OR MINGW) target_link_libraries(SQLiteCpp_example1 ssp) diff --git a/include/SQLiteCpp/Backup.h b/include/SQLiteCpp/Backup.h index 578f21f..8a5d966 100644 --- a/include/SQLiteCpp/Backup.h +++ b/include/SQLiteCpp/Backup.h @@ -11,6 +11,7 @@ */ #pragma once +#include #include #include @@ -31,7 +32,7 @@ namespace SQLite * See also the a reference implementation of live backup taken from the official site: * https://www.sqlite.org/backup.html */ -class Backup +class SQLITECPP_API Backup { public: /** diff --git a/include/SQLiteCpp/Column.h b/include/SQLiteCpp/Column.h index 8fee096..fbe2c1a 100644 --- a/include/SQLiteCpp/Column.h +++ b/include/SQLiteCpp/Column.h @@ -10,6 +10,7 @@ */ #pragma once +#include #include #include @@ -22,11 +23,11 @@ struct sqlite3_stmt; namespace SQLite { -extern const int INTEGER; ///< SQLITE_INTEGER -extern const int FLOAT; ///< SQLITE_FLOAT -extern const int TEXT; ///< SQLITE_TEXT -extern const int BLOB; ///< SQLITE_BLOB -extern const int Null; ///< SQLITE_NULL +SQLITECPP_API extern const int INTEGER; ///< SQLITE_INTEGER +SQLITECPP_API extern const int FLOAT; ///< SQLITE_FLOAT +SQLITECPP_API extern const int TEXT; ///< SQLITE_TEXT +SQLITECPP_API extern const int BLOB; ///< SQLITE_BLOB +SQLITECPP_API extern const int Null; ///< SQLITE_NULL /** * @brief Encapsulation of a Column in a row of the result pointed by the prepared Statement. @@ -44,7 +45,7 @@ extern const int Null; ///< SQLITE_NULL * because of the way it shares the underling SQLite precompiled statement * in a custom shared pointer (See the inner class "Statement::Ptr"). */ -class Column +class SQLITECPP_API Column { public: /** @@ -241,7 +242,7 @@ private: * * @return Reference to the stream used */ -std::ostream& operator<<(std::ostream& aStream, const Column& aColumn); +SQLITECPP_API std::ostream& operator<<(std::ostream& aStream, const Column& aColumn); #if __cplusplus >= 201402L || (defined(_MSC_VER) && _MSC_VER >= 1900) // c++14: Visual Studio 2015 diff --git a/include/SQLiteCpp/Database.h b/include/SQLiteCpp/Database.h index 3adde86..d0de0b4 100644 --- a/include/SQLiteCpp/Database.h +++ b/include/SQLiteCpp/Database.h @@ -10,6 +10,7 @@ */ #pragma once +#include #include // c++17: MinGW GCC version > 8 @@ -84,37 +85,37 @@ namespace SQLite // Those public constants enable most usages of SQLiteCpp without including in the client application. /// The database is opened in read-only mode. If the database does not already exist, an error is returned. -extern const int OPEN_READONLY; // SQLITE_OPEN_READONLY +SQLITECPP_API extern const int OPEN_READONLY; // SQLITE_OPEN_READONLY /// The database is opened for reading and writing if possible, or reading only if the file is write protected /// by the operating system. In either case the database must already exist, otherwise an error is returned. -extern const int OPEN_READWRITE; // SQLITE_OPEN_READWRITE +SQLITECPP_API extern const int OPEN_READWRITE; // SQLITE_OPEN_READWRITE /// With OPEN_READWRITE: The database is opened for reading and writing, and is created if it does not already exist. -extern const int OPEN_CREATE; // SQLITE_OPEN_CREATE +SQLITECPP_API extern const int OPEN_CREATE; // SQLITE_OPEN_CREATE /// Enable URI filename interpretation, parsed according to RFC 3986 (ex. "file:data.db?mode=ro&cache=private") -extern const int OPEN_URI; // SQLITE_OPEN_URI +SQLITECPP_API extern const int OPEN_URI; // SQLITE_OPEN_URI /// Open in memory database -extern const int OPEN_MEMORY; // SQLITE_OPEN_MEMORY +SQLITECPP_API extern const int OPEN_MEMORY; // SQLITE_OPEN_MEMORY /// Open database in multi-thread threading mode -extern const int OPEN_NOMUTEX; // SQLITE_OPEN_NOMUTEX +SQLITECPP_API extern const int OPEN_NOMUTEX; // SQLITE_OPEN_NOMUTEX /// Open database with thread-safety in serialized threading mode -extern const int OPEN_FULLMUTEX; // SQLITE_OPEN_FULLMUTEX +SQLITECPP_API extern const int OPEN_FULLMUTEX; // SQLITE_OPEN_FULLMUTEX /// Open database with shared cache enabled -extern const int OPEN_SHAREDCACHE; // SQLITE_OPEN_SHAREDCACHE +SQLITECPP_API extern const int OPEN_SHAREDCACHE; // SQLITE_OPEN_SHAREDCACHE /// Open database with shared cache disabled -extern const int OPEN_PRIVATECACHE; // SQLITE_OPEN_PRIVATECACHE +SQLITECPP_API extern const int OPEN_PRIVATECACHE; // SQLITE_OPEN_PRIVATECACHE /// Database filename is not allowed to be a symbolic link (Note: only since SQlite 3.31.0 from 2020-01-22) -extern const int OPEN_NOFOLLOW; // SQLITE_OPEN_NOFOLLOW +SQLITECPP_API extern const int OPEN_NOFOLLOW; // SQLITE_OPEN_NOFOLLOW -extern const int OK; ///< SQLITE_OK (used by check() bellow) +SQLITECPP_API extern const int OK; ///< SQLITE_OK (used by check() bellow) -extern const char* const VERSION; ///< SQLITE_VERSION string from the sqlite3.h used at compile time -extern const int VERSION_NUMBER; ///< SQLITE_VERSION_NUMBER from the sqlite3.h used at compile time +SQLITECPP_API extern const char* const VERSION; ///< SQLITE_VERSION string from the sqlite3.h used at compile time +SQLITECPP_API extern const int VERSION_NUMBER; ///< SQLITE_VERSION_NUMBER from the sqlite3.h used at compile time /// Return SQLite version string using runtime call to the compiled library -const char* getLibVersion() noexcept; +SQLITECPP_API const char* getLibVersion() noexcept; /// Return SQLite version number using runtime call to the compiled library -int getLibVersionNumber() noexcept; +SQLITECPP_API int getLibVersionNumber() noexcept; // Public structure for representing all fields contained within the SQLite header. // Official documentation for fields: https://www.sqlite.org/fileformat.html#the_database_header @@ -160,7 +161,7 @@ struct Header { * because of the way it shares the underling SQLite precompiled statement * in a custom shared pointer (See the inner class "Statement::Ptr"). */ -class Database +class SQLITECPP_API Database { friend class Statement; // Give Statement constructor access to the mSQLitePtr Connection Handle @@ -265,7 +266,7 @@ public: // Deleter functor to use with smart pointers to close the SQLite database connection in an RAII fashion. struct Deleter { - void operator()(sqlite3* apSQLite); + SQLITECPP_API void operator()(sqlite3* apSQLite); }; /** diff --git a/include/SQLiteCpp/Exception.h b/include/SQLiteCpp/Exception.h index eaf2542..cb7c665 100644 --- a/include/SQLiteCpp/Exception.h +++ b/include/SQLiteCpp/Exception.h @@ -10,6 +10,7 @@ */ #pragma once +#include #include #include @@ -23,7 +24,7 @@ namespace SQLite /** * @brief Encapsulation of the error message from SQLite3, based on std::runtime_error. */ -class Exception : public std::runtime_error +class SQLITECPP_API Exception : public std::runtime_error { public: /** diff --git a/include/SQLiteCpp/SQLiteCpp.h b/include/SQLiteCpp/SQLiteCpp.h index c8d1801..94751b6 100644 --- a/include/SQLiteCpp/SQLiteCpp.h +++ b/include/SQLiteCpp/SQLiteCpp.h @@ -18,6 +18,7 @@ // Include useful headers of SQLiteC++ +#include #include #include #include @@ -42,3 +43,5 @@ */ #define SQLITECPP_VERSION "3.02.01" // 3.2.1 #define SQLITECPP_VERSION_NUMBER 3002001 // 3.2.1 + + diff --git a/include/SQLiteCpp/SQLiteCppExport.h b/include/SQLiteCpp/SQLiteCppExport.h new file mode 100644 index 0000000..8629306 --- /dev/null +++ b/include/SQLiteCpp/SQLiteCppExport.h @@ -0,0 +1,40 @@ +/** + * @file SQLiteCppExport.h + * @ingroup SQLiteCpp + * @brief File with macros needed in the generation of Windows DLLs + * + * + * Copyright (c) 2012-2022 Sebastien Rombauts (sebastien.rombauts@gmail.com) + * + * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt + * or copy at http://opensource.org/licenses/MIT) + */ + +#pragma once + +/* +* #define SQLITECPP_COMPILE_DLL to compile a DLL under Windows +* #define SQLITECPP_EXPORT to export symbols when creating the DLL, otherwise it defaults to importing symbols +*/ + +/* Windows DLL export/import */ +#if defined(_WIN32)&& !defined(__GNUC__) && defined(SQLITECPP_COMPILE_DLL) + #if SQLITECPP_DLL_EXPORT + #define SQLITECPP_API __declspec(dllexport) + #pragma message("Exporting symbols") + #else + #define SQLITECPP_API __declspec(dllimport) + #pragma message("Importing symbols") + #endif +#else + #if __GNUC__ >= 4 + #define SQLITECPP_API __attribute__ ((visibility ("default"))) + #else + #define SQLITECPP_API + #endif +#endif + +#if defined(WIN32) && defined(SQLITECPP_COMPILE_DLL) + #pragma warning( disable : 4251 ) + #pragma warning( disable : 4275 ) +#endif diff --git a/include/SQLiteCpp/Savepoint.h b/include/SQLiteCpp/Savepoint.h index 0c0fba6..febe046 100644 --- a/include/SQLiteCpp/Savepoint.h +++ b/include/SQLiteCpp/Savepoint.h @@ -12,6 +12,7 @@ */ #pragma once +#include #include namespace SQLite @@ -53,7 +54,7 @@ class Database; * because of the way it shares the underling SQLite precompiled statement * in a custom shared pointer (See the inner class "Statement::Ptr"). */ -class Savepoint +class SQLITECPP_API Savepoint { public: /** diff --git a/include/SQLiteCpp/Statement.h b/include/SQLiteCpp/Statement.h index e14fed0..f33e18a 100644 --- a/include/SQLiteCpp/Statement.h +++ b/include/SQLiteCpp/Statement.h @@ -10,6 +10,7 @@ */ #pragma once +#include #include #include // SQLITECPP_PURE_FUNC @@ -30,7 +31,7 @@ namespace SQLite class Database; class Column; -extern const int OK; ///< SQLITE_OK +SQLITECPP_API extern const int OK; ///< SQLITE_OK /** * @brief RAII encapsulation of a prepared SQLite Statement. @@ -49,7 +50,7 @@ extern const int OK; ///< SQLITE_OK * because of the way it shares the underling SQLite precompiled statement * in a custom shared pointer (See the inner class "Statement::Ptr"). */ -class Statement +class SQLITECPP_API Statement { public: /** diff --git a/include/SQLiteCpp/Transaction.h b/include/SQLiteCpp/Transaction.h index af9de7e..5cc4171 100644 --- a/include/SQLiteCpp/Transaction.h +++ b/include/SQLiteCpp/Transaction.h @@ -10,6 +10,7 @@ */ #pragma once +#include #include @@ -50,7 +51,7 @@ enum class TransactionBehavior { * because of the way it shares the underling SQLite precompiled statement * in a custom shared pointer (See the inner class "Statement::Ptr"). */ -class Transaction +class SQLITECPP_API Transaction { public: /** diff --git a/meson.build b/meson.build index e4904b3..10240e4 100644 --- a/meson.build +++ b/meson.build @@ -57,6 +57,7 @@ sqlitecpp_args = cxx.get_supported_arguments( '-Wswitch-enum', '-Wshadow', '-Wno-long-long', + '-Wno-attributes', ) sqlitecpp_link = [] sqlitecpp_deps = [ @@ -66,6 +67,9 @@ sqlitecpp_deps = [ ## used to override the default sqlitecpp options like cpp standard sqlitecpp_opts = [] +## used to set required macros when using sqlitecpp +sqlitecpp_dep_args = [] + ## tests sqlitecpp_test_srcs = files( @@ -97,11 +101,6 @@ if host_machine.system() == 'windows' sqlitecpp_opts += [ 'cpp_std=c++14', ] - # check that we are not trying to build as dynamic library - if get_option('default_library') != 'shared' - message('warning: SQLiteCpp does not support shared library on Windows, the library will be built as static') - endif - endif # Options relative to SQLite and SQLiteC++ functions @@ -203,33 +202,40 @@ if get_option('b_coverage') ] endif -## Workarround for windows: if building on windows we will build the library as static -if host_machine.system() == 'windows' - libsqlitecpp = static_library( - 'sqlitecpp', - sqlitecpp_srcs, - include_directories: sqlitecpp_incl, - cpp_args: sqlitecpp_args, - dependencies: sqlitecpp_deps, - # override the default options - override_options: sqlitecpp_opts,) -else - libsqlitecpp = library( - 'sqlitecpp', - sqlitecpp_srcs, - include_directories: sqlitecpp_incl, - cpp_args: sqlitecpp_args, - dependencies: sqlitecpp_deps, - # override the default options - override_options: sqlitecpp_opts, - install: true, - # API version for SQLiteCpp shared library. - version: '0',) +sqlitecpp_static_args = sqlitecpp_args +sqlitecpp_static_dep_args = sqlitecpp_dep_args + +# if windows and shared library +if host_machine.system() == 'windows' and get_option('default_library') == 'shared' + # compile with SQLITECPP_COMPILE_DLL and SQLITECPP_DLL_EXPORT=1 + sqlitecpp_args += [ + '-DSQLITECPP_COMPILE_DLL', + '-DSQLITECPP_DLL_EXPORT', + ] + sqlitecpp_dep_args += [ + # we just need to define SQLITECPP_COMPILE_DLL + '-DSQLITECPP_COMPILE_DLL', + ] endif + +libsqlitecpp = library( + 'sqlitecpp', + sqlitecpp_srcs, + include_directories: sqlitecpp_incl, + cpp_args: sqlitecpp_args, + dependencies: sqlitecpp_deps, + # override the default options + override_options: sqlitecpp_opts, + install: true, + # API version for SQLiteCpp shared library. + version: '0',) + + + if get_option('SQLITECPP_BUILD_TESTS') # for the unit tests we need to link against a static version of SQLiteCpp - if host_machine.system() == 'windows' or get_option('default_library') == 'static' + if get_option('default_library') == 'static' # we do not need to recomplile the library libsqlitecpp_static = libsqlitecpp else @@ -237,7 +243,7 @@ if get_option('SQLITECPP_BUILD_TESTS') 'sqlitecpp_static', sqlitecpp_srcs, include_directories: sqlitecpp_incl, - cpp_args: sqlitecpp_args, + cpp_args: sqlitecpp_static_args, dependencies: sqlitecpp_deps, # override the default options override_options: sqlitecpp_opts,) @@ -251,6 +257,7 @@ install_subdir( sqlitecpp_dep = declare_dependency( include_directories: sqlitecpp_incl, link_with: libsqlitecpp, + compile_args: sqlitecpp_dep_args, ) if get_option('SQLITECPP_BUILD_TESTS') ## make the dependency static so the unit tests can link against it @@ -258,6 +265,7 @@ if get_option('SQLITECPP_BUILD_TESTS') sqlitecpp_static_dep = declare_dependency( include_directories: sqlitecpp_incl, link_with: libsqlitecpp_static, + compile_args: sqlitecpp_static_dep_args, ) endif @@ -268,7 +276,7 @@ if get_option('SQLITECPP_BUILD_TESTS') fallback: ['gtest', 'gtest_main_dep']) sqlitecpp_test_dependencies = [ gtest_dep, - sqlitecpp_static_dep, + sqlitecpp_dep, sqlite3_dep, ] diff --git a/sqlite3/CMakeLists.txt b/sqlite3/CMakeLists.txt index 0efc3ee..b3dc73a 100644 --- a/sqlite3/CMakeLists.txt +++ b/sqlite3/CMakeLists.txt @@ -11,6 +11,12 @@ add_library(sqlite3 sqlite3.h ) +if(WIN32) + if(BUILD_SHARED_LIBS) + add_definitions("-DSQLITE_API=__declspec(dllexport)") + endif() +endif() + add_library(SQLite::SQLite3 ALIAS sqlite3) target_include_directories(sqlite3 diff --git a/src/Column.cpp b/src/Column.cpp index 60b3c3b..74b288d 100644 --- a/src/Column.cpp +++ b/src/Column.cpp @@ -18,11 +18,11 @@ namespace SQLite { -const int INTEGER = SQLITE_INTEGER; -const int FLOAT = SQLITE_FLOAT; -const int TEXT = SQLITE_TEXT; -const int BLOB = SQLITE_BLOB; -const int Null = SQLITE_NULL; +SQLITECPP_API const int INTEGER = SQLITE_INTEGER; +SQLITECPP_API const int FLOAT = SQLITE_FLOAT; +SQLITECPP_API const int TEXT = SQLITE_TEXT; +SQLITECPP_API const int BLOB = SQLITE_BLOB; +SQLITECPP_API const int Null = SQLITE_NULL; // Encapsulation of a Column in a row of the result pointed by the prepared Statement. diff --git a/src/Database.cpp b/src/Database.cpp index c1403ae..c982dd5 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -23,29 +23,28 @@ #define SQLITE_DETERMINISTIC 0x800 #endif // SQLITE_DETERMINISTIC - namespace SQLite { -const int OK = SQLITE_OK; -const int OPEN_READONLY = SQLITE_OPEN_READONLY; -const int OPEN_READWRITE = SQLITE_OPEN_READWRITE; -const int OPEN_CREATE = SQLITE_OPEN_CREATE; -const int OPEN_URI = SQLITE_OPEN_URI; -const int OPEN_MEMORY = SQLITE_OPEN_MEMORY; -const int OPEN_NOMUTEX = SQLITE_OPEN_NOMUTEX; -const int OPEN_FULLMUTEX = SQLITE_OPEN_FULLMUTEX; -const int OPEN_SHAREDCACHE = SQLITE_OPEN_SHAREDCACHE; -const int OPEN_PRIVATECACHE = SQLITE_OPEN_PRIVATECACHE; +SQLITECPP_API const int OK = SQLITE_OK; +SQLITECPP_API const int OPEN_READONLY = SQLITE_OPEN_READONLY; +SQLITECPP_API const int OPEN_READWRITE = SQLITE_OPEN_READWRITE; +SQLITECPP_API const int OPEN_CREATE = SQLITE_OPEN_CREATE; +SQLITECPP_API const int OPEN_URI = SQLITE_OPEN_URI; +SQLITECPP_API const int OPEN_MEMORY = SQLITE_OPEN_MEMORY; +SQLITECPP_API const int OPEN_NOMUTEX = SQLITE_OPEN_NOMUTEX; +SQLITECPP_API const int OPEN_FULLMUTEX = SQLITE_OPEN_FULLMUTEX; +SQLITECPP_API const int OPEN_SHAREDCACHE = SQLITE_OPEN_SHAREDCACHE; +SQLITECPP_API const int OPEN_PRIVATECACHE = SQLITE_OPEN_PRIVATECACHE; // check if sqlite version is >= 3.31.0 and SQLITE_OPEN_NOFOLLOW is defined #if SQLITE_VERSION_NUMBER >= 3031000 && defined(SQLITE_OPEN_NOFOLLOW) -const int OPEN_NOFOLLOW = SQLITE_OPEN_NOFOLLOW; +SQLITECPP_API const int OPEN_NOFOLLOW = SQLITE_OPEN_NOFOLLOW; #else -const int OPEN_NOFOLLOW = 0; +SQLITECPP_API const int OPEN_NOFOLLOW = 0; #endif -const char* const VERSION = SQLITE_VERSION; -const int VERSION_NUMBER = SQLITE_VERSION_NUMBER; +SQLITECPP_API const char* const VERSION = SQLITE_VERSION; +SQLITECPP_API const int VERSION_NUMBER = SQLITE_VERSION_NUMBER; // Return SQLite version string using runtime call to the compiled library const char* getLibVersion() noexcept