从 iPhone/CocoaTouch 库链接到库的正确方法
我正在为 iPhone 编写一个静态库,我想知道我正在做的事情是否值得推荐,或者我是否应该采取不同的方法。
我正在编写的静态库依赖于libxml2。 libxml2 有一个动态库(dylib)和一个静态库(a)。 我尝试了两种方法。
方法一 - 当我尝试通过将“-lxml2”添加到“其他链接器标志”来链接静态库时,构建失败并显示以下消息:
/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/libtool: file: -lxml2 is not an object file (not allowed in a library)
方法二 - 我可以成功地从静态库链接动态库,但我'我不确定 iPhone 上是否允许这样做(或正确的方法)。 即使我正在构建静态库,Apple 是否允许静态库链接到动态库? 我仍在学习静态库,但根据我的理解,dylib 中的代码将与我的代码组合起来形成一个库,因此链接 dylib 应该不是问题。
总结一下:
- mylibrary.a -> libxml2.a [不起作用]
- mylibrary.a -> libxml2.dylib [已构建,但这正确且可接受吗?]
I'm writing a static library for the iPhone and I'm wondering if what I'm doing is recommended or if I should take a different approach.
The static library I'm writing is dependant on libxml2. libxml2 has a dynamic library (dylib) and a static library (a). I've tried two approaches.
Approach one - When I attempt to link against the static library by adding "-lxml2" to "Other linker flags" the build fails with the following message:
/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/libtool: file: -lxml2 is not an object file (not allowed in a library)
Approach two - I can successfully link against the dynamic library from my static library but I'm not sure if this is allowed (or the proper approach) on the iPhone. Even though I'm building a static library, does Apple allow static libraries that link against dynamic libraries? I'm still learning about static libraries, but from my understanding the code from the dylib would be combined with my code to make one library, thus linking against the dylib shouldn't be an issue.
To summarize:
- mylibrary.a -> libxml2.a [Doesn't work]
- mylibrary.a -> libxml2.dylib [Builds, but is this correct and acceptable?]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
SDK 中提供了 libxml2.dylib。 正确的方法是在 Xcode 中双击目标节点/应用程序,然后在“常规”下单击“链接库”下的+...您应该找到 libxml2.dylib 在该列表中。
0:42 在此截屏中,您可以了解如何将 CoreGraphics 添加到项目中。
libxml2.dylib is available in the SDK. The right way is to double-click the target node/app in Xcode and then under General click the + under "Linked Libraries"...you should find libxml2.dylib in that list.
0:42 in this Screencast you can see how CoreGraphics is added to a project.
您不能将库静态链接到其他静态库。 这就是为什么依赖于其他静态库的框架和库要求使用者将它们手动添加到“链接二进制与库”构建阶段或“其他链接器标志”构建设置中。
You can not statically link libraries into other static libraries. This is why frameworks and libraries that depend on other static libraries asks the consumer to add them manually to their Link Binary With Libraries build phase or Other Linker Flags build setting.
Apple 不允许您链接除 SDK 提供的框架和/或库之外的框架和/或库。
马可
Apple doesn't allow you to link frameworks and/or libraries other than that provided with the SDK.
Marco