xcode 构建一个使用 cocoapods 的可分发静态库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22851677/
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-15 04:50:34  来源:igfitidea点击:

Building a distributable static library that uses cocoapods

objective-cxcodestatic-librariescocoapods

提问by stackunderflow

I'm building a static library to be distributed to other iOS developers and having some trouble configuring the linker to allow the static library to be used in another app. I have used this guideto create a MyStaticLibrary.framework bundle containing the lib itself, and other assets such as images. This builds successfully and uses cocoapods to source the required dependencies (AFNetworking, etc.). So far, so good.

我正在构建一个要分发给其他 iOS 开发人员的静态库,并且在配置链接器以允许在另一个应用程序中使用该静态库时遇到了一些麻烦。我已经使用本指南创建了一个 MyStaticLibrary.framework 包,其中包含库本身和其他资产(如图像)。这将成功构建并使用 cocoapods 来获取所需的依赖项(AFNetworking 等)。到现在为止还挺好。

But when I import MyStaticLibrary.framework into a new Xcode project to test build an app with the library, I get tons of linker errors (Undefined symbols for architecture i386, _OBJC_CLASS_$_CLASSNAME) indicating that I'm doing something very wrong here.

但是,当我将 MyStaticLibrary.framework 导入新的 Xcode 项目以测试使用该库构建应用程序时,我收到了大量链接器错误(架构 i386 的未定义符号,_OBJC_CLASS_$_CLASSNAME),表明我在这里做错了什么。

So my question is, how can I build MyStaticLibrary.framework with the dependencies sourced from cocoapods, such that I can provide a 3rd party with just my framework file and allow them access to all functions specified in the public headers?

所以我的问题是,如何使用源自 cocoapods 的依赖项构建 MyStaticLibrary.framework,以便我可以仅向第 3 方提供我的框架文件并允许他们访问公共标头中指定的所有功能?

采纳答案by michaels

Any libraries you include using CocoaPods will notbe compiled into your framework by default - they're meant to be external dependencies that are not part of your actual product. However, according to their FAQ, they support a mode where you can download pods and nothave them linked to your project. From their FAQ:

默认情况下,您使用 CocoaPods 包含的任何库都不会编译到您的框架中 - 它们是外部依赖项,不属于您的实际产品。但是,根据他们的常见问题解答,他们支持一种模式,您可以在其中下载 pod,而不会将它们链接到您的项目。从他们的常见问题解答:

Note that CocoaPods itself does not require the use of a workspace. If you prefer to use sub-projects, you can do so by running pod install --no-integrate, which will leave integration into your project up to you as you see fit.

请注意,CocoaPods 本身不需要使用工作区。如果您更喜欢使用子项目,则可以通过运行 pod install --no-integrate 来实现,这将根据您的需要将集成到您的项目中。

To include external dependencies in your compiled binary:

要在编译的二进制文件中包含外部依赖项:

  • For code: Instead of using cocoapods, check out the repositories you want to include and copy the source files into your project -- this will ensure they are compiled with the rest of your code

  • For static libraries (i.e. .afiles), in your framework's Link Binary With Librariesbuild phase, make sure to include all the ones you would like to compile. You should also make sure the associated header files are included in Copy Headersbuild phase, with the appropriate visibility.

  • 对于代码:不要使用 cocoapods,而是检查您想要包含的存储库并将源文件复制到您的项目中——这将确保它们与您的其余代码一起编译

  • 对于静态库(即.a文件),在框架的Link Binary With Libraries构建阶段,请确保包含所有要编译的库。您还应该确保相关的头文件包含在Copy Headers构建阶段,并具有适当的可见性。

NoteWhen bundling third party libraries in this way, you run the risk of conflicting with the projects that are integrating your framework. For example, let's say you are using a lib called SOSomeView, and you choose to compile that in with your framework. Now, if the app you are integrating with also includes SOSomeView, you will get a compile-time error that the class is declared twice. To fix this issue, you should re-namespace any external dependencies you want to hardcode into your framework (i.e. rename the class to XXSOSomeView).

注意以这种方式捆绑第三方库时,您会面临与集成您的框架的项目发生冲突的风险。例如,假设您正在使用一个名为 的库SOSomeView,并且您选择在您的框架中编译它。现在,如果您正在集成的应用程序也包含SOSomeView,您将收到一个编译时错误,该类被声明了两次。要解决此问题,您应该将要硬编码到框架中的任何外部依赖项重新命名空间(即将类重命名为XXSOSomeView)。

I don't know how to solve that problem if you are compiling static libraries in with your framework.

如果您使用框架编译静态库,我不知道如何解决该问题。