如何在 Xcode 中链接动态库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/637081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:30:31  来源:igfitidea点击:

How can I link a dynamic library in Xcode?

c++xcodemacosdylib

提问by Sam

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

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

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

我构建它并在 x-code 中运行它并吐出:

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/.

该库位于 /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:

我可以通过更改到可执行文件夹 /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

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?

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

采纳答案by benzado

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.

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

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

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

回答by north5

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

我刚刚使用 Module Bundle 项目完成了这项工作。这被包含在一个具有单独可执行文件的更大项目中。

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:

我添加了一个“Copy dylibs to frameworks”步骤,将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

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:

当然,libBobDylan.dylib 也链接到 libBillyIdol.dylib。所以我不得不在 Target 的最开始添加另一个运行脚本阶段来修复这里的安装名称:

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 ...

我有十多个这样的链接;我不得不说服 dylib 的供应商填充他们的标题以适应我的许多 install_name 更改......

回答by stefanB

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.

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

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

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