如何在 Xcode 中链接动态库?

发布于 2024-07-14 11:32:06 字数 699 浏览 7 评论 0原文

我目前正在 Qt 中开发一个程序,它使用库 libqextserialport.1.dylib。

我构建它并在 x 代码中运行,它返回:

dyld: Library not loaded: libqextserialport.1.dylib
    Referenced from: /Users/samuelreh/Desktop/QtOpenCV/build/Debug/QtOpenCV.app/Contents/MacOS/QtOpenCV
    Reason: image not found

该库位于 /Users/samuelreh/Desktop/QtOpenCV/qextserialport/build/ 中。

我可以通过更改为可执行文件夹 /Users/samuelreh/Desktop/QtOpenCV/build/Debug/QtOpenCV.app/Contents/MacOS/ 并输入:

install_name_tool -change libqextserialport.1.dylib /Users/samuelreh 来运行我的程序/Desktop/QtOpenCV/qextserialport/build/libqextserialport.1.dylib QtOpenCV

我知道除此之外可能还有很多解决方案。 有人知道 x 代码中最好/最优雅/最简单的做法吗?

I am currently developing a program in Qt and it uses the library libqextserialport.1.dylib.

I build it and run in x-code and it spits back:

dyld: Library not loaded: libqextserialport.1.dylib
    Referenced from: /Users/samuelreh/Desktop/QtOpenCV/build/Debug/QtOpenCV.app/Contents/MacOS/QtOpenCV
    Reason: image not found

The library is located in /Users/samuelreh/Desktop/QtOpenCV/qextserialport/build/.

I can run my program by changing to the executable folder /Users/samuelreh/Desktop/QtOpenCV/build/Debug/QtOpenCV.app/Contents/MacOS/ and entering:

install_name_tool -change libqextserialport.1.dylib /Users/samuelreh/Desktop/QtOpenCV/qextserialport/build/libqextserialport.1.dylib QtOpenCV

I know there is probably many solutions besides this. Anybody know the best / most-elegant / easiest to do from x-code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷夜 2024-07-21 11:32:06

我刚刚通过模块捆绑项目完成了这一点。 这被包含到一个具有单独可执行文件的更大项目中。

我添加了“将 dylibs 复制到框架”步骤,将 dylibs 复制到 /Foobar.bundle/Contents/Frameworks/Foobar/。 然后我添加了一个运行脚本阶段作为最后一步运行,以修复可执行文件中 dylib 的安装名称:

install_name_tool -change libBobDylan.dylib @executable_path/../Plugins/Foobar.bundle/Contents/Frameworks/Foobar/libBobDylan.dylib path/to/build/product/Foobar.Bundle/Contents/MacOS/Foobar

当然,libBobDylan.dylib 也链接到 libBillyIdol.dylib。 因此,我必须在目标的最开始添加另一个运行脚本阶段来修复此处的安装名称:

install_name_tool -change libBillyIdol.dylib @executable_path/../Plugins/FooBar.bundle/Contents/Frameworks/Foobar/libBillyIdol.dylib local/path/to/libBobDylan.dylib

我有十几个要链接的这些名称; 我必须说服 dylib 的供应商填充他们的标头以适应我的许多 install_name 更改......

I've just done exactly this, with a Module Bundle project. This was being included into a larger project with a separate executable.

I added a "Copy dylibs to frameworks" step, which copied the dylibs to /Foobar.bundle/Contents/Frameworks/Foobar/. Then I added a Run Script Phase to run as the final step, to fix the install names of the dylibs in the executable:

install_name_tool -change libBobDylan.dylib @executable_path/../Plugins/Foobar.bundle/Contents/Frameworks/Foobar/libBobDylan.dylib path/to/build/product/Foobar.Bundle/Contents/MacOS/Foobar

Of course, libBobDylan.dylib also linked to libBillyIdol.dylib. So I had to add another Run Script Phase at the very start of the Target to fix the install names here:

install_name_tool -change libBillyIdol.dylib @executable_path/../Plugins/FooBar.bundle/Contents/Frameworks/Foobar/libBillyIdol.dylib local/path/to/libBobDylan.dylib

I had over a dozen of these to link against; I had to persuade the supplier of the dylibs to pad their header to accommodate my many install_name changes ...

魂ガ小子 2024-07-21 11:32:06

如果我正确理解您的问题,您的应用程序构建正常,链接时没有错误,但是当您尝试启动它时,找不到库。

这并不奇怪,因为 dylib 文件位于某个任意目录中,而不是在系统路径上。 您需要将其复制到 /usr/lib 中(可能不是一个好主意)或将其包含在应用程序包中。 后者可能是更好的方法。

我从未尝试过,但显然您需要使用 复制文件构建阶段 将 dylib 放入包中,然后 配置 Xcode 以便您的可执行文件知道在哪里找到它。

If I understand your problem correctly, your app is building fine, no errors when linking, but when you try to launch it the library cannot be found.

That's not surprising, since the dylib file is in some arbitrary directory not on the system path. You either need to copy it into /usr/lib (probably not a good idea) or include it in the application bundle. The latter is probably the better approach.

I've never tried it, but apparently you need to use a Copy Files Build Phase to put the dylib inside your bundle and then configure Xcode so that your executable will know where to find it.

牵强ㄟ 2024-07-21 11:32:06

您只需将库从目录拖到您的 Xcode 项目中,最好拖到资源中,但这并不重要(我相信)。 我尝试过检查和不检查“将文件复制到捆绑包中”,它在两种情况下都有效,不确定是否需要将其包含在捆绑包中进行部署。

我已经使用 sqlite3 和 cocoa 应用程序(cocoa-touch)对此进行了测试。

You just drag the library from the directory onto your Xcode project, best into resources but it does not matter (I believe). I tried with and without checking the 'copy files into bundle' and it work in both cases, not sure if you need to include it in bundle for deployment.

I've tested this with sqlite3 and cocoa application (cocoa-touch).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文