如何使用 Xcode 创建动态库 (dylib)? [英] How do I create a dynamic library (dylib) with Xcode?

查看:15
本文介绍了如何使用 Xcode 创建动态库 (dylib)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Xcode 中构建了一些命令行实用程序(纯 C,没有 Cocoa).我希望他们所有人都使用我定制的 libpng 版本,并且我想通过在所有可执行文件之间共享一个库副本来节省空间(我不介意与他们重新分发 .dylib).

I'm building few command-line utilities in Xcode (plain C, no Cocoa). I want all of them to use my customized version of libpng, and I want to save space by sharing one copy of the library among all executables (I don't mind re-distributing .dylib with them).

我需要做一些魔术来获取 libpng 导出符号吗?

Do I need to do some magic to get libpng export symbols?

"Link Binary With Libraries" 是否静态构建阶段链接?

Does "Link Binary With Libraries" build phase link statically?

Apple 的文档提到在运行时使用 dlopen 加载库,但我如何才能让 Xcode 创建可执行文件而不抱怨缺少符号?

Apple's docs mention loading of libraries at run time with dlopen, but how I can make Xcode create executable without complaining about missing symbols?

我想我已经明白了:

  • libpng 没有正确链接,因为我已经构建了 32/64 位可执行文件和 32 位库.库和可执行文件的构建设置必须匹配.

  • libpng wasn't linking properly, because I've built 32/64-bit executables and 32-bit library. Build settings of the library and executables must match.

libpng 的 config.h 需要有大量的定义,例如 #define FEATURE_XXX_SUPPORTED

libpng's config.h needs to have tons of defines like #define FEATURE_XXX_SUPPORTED

"Link Binary With Libraries" 构建阶段可以很好地处理动态库,并且 DYLD_FALLBACK_LIBRARY_PATH 环境变量是加载 .dylib来自应用程序包.

"Link Binary With Libraries" build phase handles dynamic libraries just fine, and DYLD_FALLBACK_LIBRARY_PATH environmental variable is neccessary for loading .dylibs from application bundle.

推荐答案

您可能需要确保您构建的动态库有一个导出的符号文件,其中列出了应该从库中导出的内容.它只是要导出的符号的平面列表,每行一个.

You probably need to ensure that the dynamic library you build has an exported symbols file that lists what should be exported from the library. It's just a flat list of the symbols, one per line, to export.

此外,当您的动态库被构建时,它会在其中嵌入一个 安装名称,默认情况下,它是构建它的路径.随后,任何链接到它的东西都会首先在指定的路径中寻找它,然后才在 dyld(1) 手册页.

Also, when your dynamic library is built, it gets an install name embedded within it which is, by default, the path at which it is built. Subsequently anything that links against it will look for it at the specified path first and only afterwards search a (small) set of default paths described under DYLD_FALLBACK_LIBRARY_PATH in the dyld(1) man page.

如果您打算将此库放在可执行文件旁边,则应调整其安装名称以引用它.只需在 Google 上搜索安装名称"就可以找到大量相关信息.

If you're going to put this library next to your executables, you should adjust its install name to reference that. Just doing a Google search for "install name" should turn up a ton of information on doing that.

这篇关于如何使用 Xcode 创建动态库 (dylib)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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