如何从 Objective-c 中使用 sqlite3_load_extension [错误:未找到 dlopen(...) 图像] [英] HOW TO sqlite3_load_extension from within Objective-c [error: dlopen(...) image not found]

查看:65
本文介绍了如何从 Objective-c 中使用 sqlite3_load_extension [错误:未找到 dlopen(...) 图像]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试加载 sqlite3 的扩展,以便在 iPhone 应用程序 Objective-c 代码中使用.我成功地将新函数的 c 文件编译成一个名为 libsqlitefunctions.dylib 的 dylib.这是我有点迷失的地方.我调用 sqlite3_load_extension 如下:

I am trying to load an extension to sqlite3 for use within iPhone application objective-c code. I successfully compiled the c file of new functions into a dylib named libsqlitefunctions.dylib. Here is where I am a bit lost. I call sqlite3_load_extension as follows:

char *error = sqlite3_malloc(MAX_SQLITE_ERROR_MESSAGE_SIZE);const char *library = [@"libsqlitefunctions.dylib" UTF8String];

char *error = sqlite3_malloc(MAX_SQLITE_ERROR_MESSAGE_SIZE); const char *library = [@"libsqlitefunctions.dylib" UTF8String];

if (sqlite3_load_extension(database, library, 0, &error) != SQLITE_OK) {message = [NSString stringWithFormat:@"%s", error];}

if (sqlite3_load_extension(database, library, 0, &error) != SQLITE_OK) { message = [NSString stringWithFormat:@"%s", error]; }

sqlite3_free(错误);

sqlite3_free(error);

无论我做什么,都会出现错误:dlopen(libsqlitefunctions.dylib, 10): image not found

No matter what I do, I get the error: dlopen(libsqlitefunctions.dylib, 10): image not found

我试过了:

  1. 指示 dylib 的完全限定路径
  2. 表示相对路径
  3. 不指示路径(如上所示)
  4. 将 dylib 添加为框架
  5. 将 .c 文件添加到我的项目中并将其编译为 .o 文件,然后尝试加载它

请注意,这不是入口点问题,因为我将 0 作为该参数传递.这将通过调用 dylib 中定义的 init 函数来强制加载 dylib.我什至没有达到那个点.

Please note that this is not an entry point problem, since I pass 0 as that arg. This will force the dylib to load by calling an init function defined in the dylib. I do not even get to that point.

与你们其他人相比,我几乎是一个新手,觉得我可能对库的加载方式缺乏了解.

I am pretty much a newbie compared to the rest of you guys and feel that I am probably lacking an understanding of how libraries are loaded.

我真的很感激任何想法,因为能够使用这个库中的函数对我的应用程序的功能很重要.提前谢谢大家.

I would really appreciate any ideas since the ability to make use of the functions in this library is important to the functionality of my app. Thank you all in advance.

推荐答案

经过几天的搜索,我终于找到了解决方案!

After days of search I finally found a solution !

1) 一旦您的 sqlite_extension.h 和 sqlite_extension.c 文件移动到您的 sqlite_extension 文件夹中

1) once your sqlite_extension.h and sqlite_extension.c files moved into your sqlite_extension folder

2) cd 到那个 sqlite_extension 文件夹

2) cd to that sqlite_extension folder

3) 使用 :

gcc -g -fPIC -std=gnu99 -current_version 1.0 -compatibility_version 1.0 -dynamiclib -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk -arch armv7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk sqlite_extension.c -o sqlite_extension.dylib

您可能需要更改 ../iPhoneOSN.M.sdk 之类的 SDK 路径,其中 NM 表示最新可用的 iOS 版本,请查看文件夹/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/" 查找现有路径

You might need to change the SDK path for something like ../iPhoneOSN.M.sdk where N.M indicate the last available version of iOS, have a look to the folder "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/" to find an existing path

4) 您可以使用以下命令检查生成的文件:

4) You can check the resulting file using :

file /path/to/your/sqlite_extension.dylib

应该返回如下内容:

/./././sqlite_extension.dylib (for architecture armv7): Mach-O dynamically linked shared library arm
/./././sqlite_extension.dylib (for architecture arm64): Mach-O 64-bit dynamically linked shared library

或者使用更详细的:

otool -l sqlite_extension.dylib

5) 现在您的库需要使用与嵌入应用程序相同的身份进行代码签名,您可以使用:

5) Now your library needs to be code-signed with the same identity as your embedding app you can do this using :

codesign -fs "your_apple_identity" sqlite_extension.dylib

your_apple_identity 可以采用your_name: your_apple_login_email@address.xxx"的形式

your_apple_identity can be in the form "your_name: your_apple_login_email@address.xxx"

确保的最佳方法是从使用 Xcode 编译并在设备上运行的嵌入式应用程序可执行文件或计算机上的存档中复制一个:

The best way to make sure is to copy the one from your embedded app executable compiled and running on your device with Xcode or the archive on your computer using :

codesign --display --verbose=4 ~/Library/Developer/Xcode/Archives/2016-09-25/YourApp\ 25-09-2016\ 12.00.xcarchive/Products/Applications/YourApp.app/YourApp

用于代替your_apple_identity"的字符串是在第一次出现 Authority= 后找到的字符串

The string to use in place of "your_apple_identity" is the one found after the first occurence of Authority=

6) 对库进行代码签名后,您可以使用相同的命令检查是否获得与应用程序类似的签名信息:

6) Once your library code-signed, you can check that you get similar signing information as your App using the same command :

codesign --display --verbose=4 sqlite_extension.dylib

7) 您的库现在已准备好嵌入您的应用程序中,您只需确保 Xcode 刷新您的库文件(如果您尝试过以前的同名文件).为此,您可以删除以前的文件,在您的设备上编译并运行,放回新文件,确保它位于应用程序构建阶段选项卡的Copy Bundle ressources"中,而不是Link Binary With Librairies"中

7) Your Library is now ready to be embedded in your app, all you need is to make sure Xcode refreshes your library file if you had try a previous file with the same name. To do so you can remove the former file, compile and run on your device, put back the new file making sure it is in the "Copy Bundle ressources" not the "Link Binary With Librairies" of the Build Phases tab of your app

当您的应用程序请求 SQLite 使用它时,您的扩展程序应该正确加载

Your extension should then load without error when your app requests SQLite to use it

iOS 9.3.5Xcode 8.0SQLite 3.9.2

这篇关于如何从 Objective-c 中使用 sqlite3_load_extension [错误:未找到 dlopen(...) 图像]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆