Skip to content

Do we need to set -DSQLITE_HAS_CODEC manually? #2

Open

Description

@b95505017

As the document says that -DSQLITE_HAS_CODEC need to be set manually in the Other C Flags. It looks like the compilation success without this flag. What issue should I notice in this case?

Activity

  1. R4N commented on Nov 24, 2025

    @R4N
    Member

    Hi @b95505017

    Thanks for your interest in SQLCipher.swift.

    You'll need the SQLITE_HAS_CODEC flag in your consuming project to enable SQLCipher encryption.

    For Objective-C based projects, you'll want to use the Other C Flag -DSQLITE_HAS_CODEC.

    For Swift based projects, you'll want to use the SQLITE_HAS_CODEC=1 Preprocessor Macro.

    If you don't set the appropriate build setting based on your target language, you should get a compilation failure when attempting to use sqlite3_key along the lines of:

    Cannot find 'sqlite3_key' in scope
    

    and SQLCipher encryption won't be enabled.

    The README Build Settings section of this project mentions these two different settings depending on your project's language.

    In the documentation page you linked from our website, it's not as clear that you'll only need to set the appropriate build setting based on target language (for Swift Package Manager integration). We'll adjust that to make it clearer.

    It's good to test and verify that SQLCipher is properly linked/integrated/enabled. We have recommendations at the bottom of the documentation page you linked: https://www.zetetic.net/sqlcipher/sqlcipher-apple-community/#testing-and-verification

  2. b95505017 commented on Nov 24, 2025

    @b95505017
    Author

    I didn't get any compilation failure when leaving Other C flag empty, any reason?

  3. R4N commented on Nov 24, 2025

    @R4N
    Member

    @b95505017

    I didn't get any compilation failure when leaving Other C flag empty, any reason?

    Are you calling sqlite3_key somewhere in your project's code to key your database? The compilation error will only occur when your project attempts to use sqlite3_key without the appropriate (SQLITE_HAS_CODEC) build setting based on your target language.

    If your project's target language is Swift, you'll only need to set the Preprocessor Macro SQLITE_HAS_CODEC=1 for both debug and release as mentioned in my previous comment.

  4. jcesarmobile commented on Apr 14, 2026

    @jcesarmobile

    Couldn't you add the flag in the Package.swift using cSettings? or that doesn't work on binary targets?

  5. R4N commented on Apr 15, 2026

    @R4N
    Member

    The SQLITE_HAS_CODEC flag (-DSQLITE_HAS_CODEC for Objective-C SQLITE_HAS_CODEC=1 Preprocessor Macro for Swift) needs to the applied to the consuming application's project to enable SQLCipher encryption and utilize functions like sqlite3_key/sqlite3_key_v2 to encrypt databases. There's no capability for Swift Packages to apply CFlags/Preprocessor macros to consuming projects.

    Defining SQLITE_HAS_CODEC within the Package.swift would only allow sources within the package to utilize SQLCipher encryption (i.e. call sqlite3_key), but not external sources/consumers.

    The same thing would be true if SQLCipher.swift Package contained the source amalgamation rather than a binaryTarget.

  6. jcesarmobile commented on Apr 17, 2026

    @jcesarmobile

    I'm trying to use this package in a Cordova plugin (Objective-C), when using cordova-ios 8 with CocoaPods, the dependency SQLCipher works without having to add SQLITE_HAS_CODEC to the app (because the podspec already includes it).

    Trying to update the plugin to use SPM with this repository (https://github.com/sqlcipher/SQLCipher.swift), I get this on this line sqlite3_key(db, key, strlen(key));

    Call to undeclared function 'sqlite3_key'; ISO C99 and later do not support implicit function declarations
    I've tried adding SQLITE_HAS_CODEC and/or -DSQLITE_HAS_CODEC to the app and even to my plugin's Package.swift but the code won't compile.

    But using https://github.com/skiptools/swift-sqlcipher repository, which has the SQLITE_HAS_CODEC in their Package.swift makes my code compile, so that's why I think you should add the SQLITE_HAS_CODEC to the repository's Package.swift.

    If I add https://github.com/sqlcipher/SQLCipher.swift as a dependency to a project and the source code is also on the project, the same source code compiles when I add SQLITE_HAS_CODEC, but has no effect when the source code is in a swift package (as it is when you are using it in a Cordova or Capacitor plugin)

  7. R4N commented on Apr 22, 2026

    @R4N
    Member

    @jcesarmobile

    I'm trying to use this package in a Cordova plugin (Objective-C), when using cordova-ios 8 with CocoaPods, the dependency SQLCipher works without having to add SQLITE_HAS_CODEC to the app (because the podspec already includes it).

    With Cocoapods the podspec definition allowed you to define Other C flags for the consuming project. If you pod install with the SQLCipher pod, notice that the -DSQLITE_HAS_CODEC Other C flag is added to your consuming project.

    We sell a SQLCipher for Cordova turnkey solution under our Enterprise program: https://www.zetetic.net/sqlcipher/cordova/ -- you can request a trial of it here: https://www.zetetic.net/sqlcipher/trial/

    I've tried adding SQLITE_HAS_CODEC and/or -DSQLITE_HAS_CODEC to the app and even to my plugin's Package.swift but the code won't compile.

    You'll need to add the SQLITE_HAS_CODEC flag to the location where you're attempting to use SQLCipher encryption. If you have a Swift Package that consumes SQLCipher.swift package (and uses it's encryption), you'll need to add the flag to that package.

    For example if you have SQLCipherConsumingPackage which has a dependency on SQLCipher.swift and you intend to use SQLCipher encryption in the code of SQLCipherConsumingPackage, you'll need to add the applicable flag to SQLCipherConsumingPackage Package.swift. Here's an example of what the application portion of the Package.swift might look like:

        dependencies: [
          .package(url: "https://github.com/sqlcipher/SQLCipher.swift.git", from: "4.10.0")
        ],
        targets: [
            .target(
                name: "SQLCipherConsumingPackage",
                dependencies: [
                    .product(name: "SQLCipher", package: "SQLCipher.swift")
                ],
                cSettings: [
                    .define("SQLITE_HAS_CODEC", to: nil)
                ],
            ),
    
        ],
    

    Then your final consuming project (the app project which is consuming SQLCipherConsumingPackage), can call through to functions in sources internal to SQLCipherConsumingPackage to perform any SQLCipher encryption. If you also need to use sqlite3_key directly in your conusming application (outside of the SQLCipherConsumingPackage), you'll also need to set the applicable SQLITE_HAS_CODEC flag there as well.

    But using https://github.com/skiptools/swift-sqlcipher repository, which has the SQLITE_HAS_CODEC in their Package.swift makes my code compile, so that's why I think you should add the SQLITE_HAS_CODEC to the repository's Package.swift.

    These are the flags used when compiling sqlite3.c within the package. The binary target within SQLCipher.swift package already includes the SQLITE_HAS_CODEC flag when compiling the binaryTarget, so this isn't the reason why that package works without the undefined function error.

    Instead, the https://github.com/skiptools/swift-sqlcipher package you referenced has explicitly commented out the #ifdef SQLITE_HAS_CODEC guard in the sqlite3.h header around sqlite3_key*: https://github.com/skiptools/swift-sqlcipher/blob/main/Sources/SQLCipher/sqlite/sqlite3.h#L6664 so the sqlite3_key function will always be defined even if you're not properly setting SQLITE_HAS_CODEC.

  8. jcesarmobile commented on Apr 22, 2026

    @jcesarmobile

    I had already tried adding the cSettings for SQLITE_HAS_CODEC in several different ways ("SQLITE_HAS_CODEC", to: nil), ("SQLITE_HAS_CODEC"), ("SQLITE_HAS_CODEC", to: "1"), and none worked.

    Does your cordova plugin support SPM?

  9. R4N commented on Apr 28, 2026

    @R4N
    Member

    The SQLCipher commercial Cordova plugin doesn't currently have a Swift Package associated with it. That being said, we could look into supporting that if you're interested in licensing it. Please reach out to us at support@zetetic.net.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions