简体   繁体   English

如何将SQlite扩展源文件链接到iPhone的Xcode?

[英]How to link a SQlite Extension Source File into Xcode for iPhone?

I use a statically linked library for Sqlite in an iPhone Xcode project. 我在iPhone Xcode项目中为Sqlite使用静态链接库。 I am now trying to include a .C extension to Sqlite in this project. 我现在正试图在这个项目中包含一个.c扩展名到Sqlite。 However, I am having trouble making the Sqlite in the build SEE the extension. 但是,我在构建中使用Sqlite时遇到问题SEE扩展。

The statically linked Sqlite library works fine. 静态链接的Sqlite库工作正常。 Also the .C extension works on my desktop, and builds fine as a statically linked library in Xcode. 此.C扩展也适用于我的桌面,并在Xcode中作为静态链接库构建。 However, the custom functions it defines are missing when called. 但是,调用时,它定义的自定义函数会丢失。

For example, I load the extension as so with no errors. 例如,我加载扩展名,没有错误。

SELECT load_extension('extension_name.so');

But when I try to call a function defined in the extension, I get this message 但是当我尝试调用扩展中定义的函数时,我得到了这条消息

DB Error: 1 "no such function: custom_function"

Does anyone know much about linking a Sqlite extension into an Xcode project? 有没有人知道将Sqlite扩展链接到Xcode项目?

As said by @jbworld you cannot load dynamic libraries on the iPhone, so this link should be made statically, at compilation time. 正如@jbworld所说,你不能在iPhone上加载动态库,所以这个链接应该在编译时静态创建。 While reading at the code of spatialite (that's an SQLite extension), I found that kind of invocation: 在阅读spatialite的代码(这是一个SQLite扩展)时,我发现了这种调用:

void spatialite_init (int verbose) {
/* used when SQLite initializes SpatiaLite via statically linked lib */
    sqlite3_auto_extension ((void (*)(void)) init_static_spatialite);
}

And the init_static_spatialite code: init_static_spatialite代码:

static void init_static_spatialite (sqlite3 * db, char **pzErrMsg,
            const sqlite3_api_routines * pApi)
{
    SQLITE_EXTENSION_INIT2 (pApi);
/* setting the POSIX locale for numeric */
    setlocale (LC_NUMERIC, "POSIX");
    *pzErrMsg = NULL;
    sqlite3_create_function (db, "spatialite_version", 0, SQLITE_ANY, 0,
                             fnct_spatialite_version, 0, 0);
    sqlite3_create_function (db, "proj4_version", 0, SQLITE_ANY, 0,
                             fnct_proj4_version, 0, 0);
    sqlite3_create_function (db, "geos_version", 0, SQLITE_ANY, 0,
                             fnct_geos_version, 0, 0);
    sqlite3_create_function (db, "GeometryConstraints", 3, SQLITE_ANY, 0,
                             fnct_GeometryConstraints, 0, 0);
    sqlite3_create_function (db, "CheckSpatialMetaData", 0, SQLITE_ANY, 0,
                             fnct_CheckSpatialMetaData, 0, 0);
...

So it looks like if the sqlite_auto_extension enables you to statically link an extension. 所以看起来sqlite_auto_extension允许您静态链接扩展。 The documentation seems to confirm that: 该文件似乎证实:

“This API can be invoked at program startup in order to register one or more statically linked extensions that will be available to all new database connections.” “可以在程序启动时调用此API,以便注册一个或多个可用于所有新数据库连接的静态链接扩展。”

Then, at runtime, for spatialite, I just have to invoke the spatialite_init(0) to get the extension up and running. 然后,在运行时,对于spatialite,我只需要调用spatialite_init(0)来启动并运行扩展。

Invoke such method before loading any sqlite DB. 加载任何sqlite DB 之前调用此方法。

Hope this helps. 希望这可以帮助。

Would it be possible for you to build your custom functions directly into your main executable, and tell sqlite about them with sqlite3_create_function ? 您是否可以直接将自定义函数构建到主可执行文件中,并使用sqlite3_create_function告诉sqlite它们?

I don't know what details apply in this situation, but I do recall that the iPhone is funny about loading code at runtime. 我不知道在这种情况下适用的细节,但我记得iPhone在运行时加载代码很有趣。

The function load_extension loads a shared library (.so). 函数load_extension加载共享库(.so)。 This is not allowed on the iPhone. iPhone上不允许这样做。 You will have to recompile your static library to include the extension you wish to use. 您必须重新编译静态库以包含要使用的扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM