如何创建静态库并可以在 ios 中的任何项目上添加 .a 文件

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

How can I create static library and can add just .a file on any project in ios

iosxcodeios4static-librariesobjective-c++

提问by dhaya

How can I create static library and can add just .a file on any project in ios.

如何创建静态库并可以在 ios 中的任何项目上添加 .a 文件。

I tried doing this but couldn't do it.

我尝试这样做,但无法做到。

Thanks in advance

提前致谢

回答by dhaya

if you want create static lib mean refer the link http://jaym2503.blogspot.in/2013/01/how-to-make-universal-static-library.html

如果你想创建静态库意味着参考链接http://jaym2503.blogspot.in/2013/01/how-to-make-universal-static-library.html

Step 1 : Create a New Project, Named it "Logger"

第 1 步:创建一个新项目,命名为“Logger”

Step 2 : Create Classes

第 2 步:创建类

You can create as many classes you wants, In our tutorial we will create one class named "Logger". So, now two files should be in our resource. 1. Logger.h 2. Logger.m

您可以创建任意数量的类,在我们的教程中,我们将创建一个名为“Logger”的类。所以,现在我们的资源中应该有两个文件。1. 记录器.h 2. 记录器.m

Step 3 : Put some useful code into Classes

第 3 步:将一些有用的代码放入类中

Step 4 : Create New Target

第 4 步:创建新目标

Create New Target from File Menu.

从文件菜单创建新目标。

New Target Select Cocoa Touch Static Library

新目标选择 Cocoa Touch 静态库

Step 5 : Add files to Compile Resource

第 5 步:将文件添加到编译资源

Select "Logger" Target of Static Library Go to Build Phases In Complied Sources section, Add all the .m and .mm files. In Copy Files section, Add all the .h files and resource files.

选择静态库的“记录器”目标转到编译源部分中的构建阶段,添加所有 .m 和 .mm 文件。在复制文件部分,添加所有 .h 文件和资源文件。

Build Phases

构建阶段

Step 6 : Compile Project with Static Library Target

第 6 步:使用静态库目标编译项目

Compile Project for iOS Device Compile Project for Simulator You can find two different .a files generated in build folders.

为 iOS 设备编译项目 为模拟器编译项目 您可以在构建文件夹中找到两个不同的 .a 文件。

Find .a file

查找 .a 文件

Step 7: Make Static Library Universal

第 7 步:使静态库通用

You can find two different library now, one is for simulator and one is for iOS devices.

您现在可以找到两个不同的库,一个用于模拟器,一个用于 iOS 设备。

Create a New Folder and name it LoggerMerge. Copy libLogger.afile of Debug-iphoneos folder to "LoggerMerge" rename it to libLogger_device.a Copy libLogger.afile of Debug-iphonesimulator folder to "LoggerMerge" rename it to libLogger_simulator.a Open LoggerMerge folder with Terminal Fire below command lipo -create "libLogger_simulator.a" "libLogger_device.a" -output "libLogger.a"

创建一个新文件夹并将其命名为 LoggerMerge。将libLogger.aDebug-iphoneos 文件夹的文件复制到“LoggerMerge”,将其重命名为 libLogger_device.a 将libLogger.aDebug-iphonesimulator 文件夹的文件复制到“LoggerMerge”,将其重命名为 libLogger_simulator.a 打开 LoggerMerge 文件夹,在命令 lipo -create " libLogger_simulator.a" " libLogger_device.a"下方使用 Terminal Fire输出“ libLogger.a

Now, you can find libLogger.a in LoggerMerge folder, this is Universal static library file. Now, Just one thing you need is headers, See above screenshot there is folder called include in both build folder. Just copy header file from this folder.

现在,您可以在 LoggerMerge 文件夹中找到 libLogger.a,这是通用静态库文件。现在,您只需要一件事就是标题,请参见上面的屏幕截图,在两个构建文件夹中都有一个名为 include 的文件夹。只需从此文件夹中复制头文件即可。

Step 8 : Test Static Library

第 8 步:测试静态库

Create New Project, name it TestLogger Import libLogger.aand header files Import header file "Logger.h" anywhere you want to use Now, Use this Logger class as default practice. In our case, [Logger log:@"Test String"]; Run Project in Simulator and Device both That's it!! You have your own static Library!!

创建新项目,将其命名为 TestLogger 导入libLogger.a和头文件 将头文件“ Logger.h”导入到任何你想使用的地方 现在,使用这个 Logger 类作为默认做法。在我们的例子中,[Logger log:@"Test String"]; 在模拟器和设备中运行项目就是这样!!你有自己的静态库!!

回答by yashwanth77

Step 1:Starting a New Static Library Project

第 1 步:启动一个新的静态库项目

Open XCode and start a new project. Under iOS, select Library and “Cocoa Touch Static Library” say it as "staticlibrary". This will create a nice new project for us that builds a .afile.

打开 XCode 并开始一个新项目。在iOS下,选择Library,然后“Cocoa Touch Static Library”将其称为“staticlibrary”。这将为我们创建一个很好的新项目来构建.a文件。

Step 2:Code your static library First we need to add some files. Add a new NSObject subclass to your project and name it StaticClass.Then Write some useful code in those files.

第 2 步:编码您的静态库 首先我们需要添加一些文件。将一个新的 NSObject 子类添加到您的项目中,并将其命名为 StaticClass。然后在这些文件中编写一些有用的代码。

Step 3:Building And Distributing Your Library Once you are happy with your library, simply build it in XCode. Obviously, don't do build and run as you can't run it (again unless you have a test suite). Now, look under the Products group in XCode and you should see a file called lib(libraryName).a. In our case, it's libstaticlibrary.a.

第 3 步:构建和分发您的库 一旦您对您的库感到满意,只需在 XCode 中构建它。显然,不要构建和运行,因为你不能运行它(同样,除非你有一个测试套件)。现在,查看 XCode 中的 Products 组,您应该会看到一个名为 lib(libraryName).a 的文件。在我们的例子中,它是libstaticlibrary.a.

Right click on that file and select “Reveal In Finder”. Drag this file into a new folder that you create where you will be bundling all of your library files.Now, do the same with all of the .h files. In our case, just copy StaticClass.hinto this new directory. Your directory structure should now look like:

右键单击该文件并选择“在 Finder 中显示”。将此文件拖到您创建的新文件夹中,您将在其中捆绑所有库文件。现在,对所有 .h 文件执行相同操作。在我们的例子中,只需复制StaticClass.h到这个新目录中。您的目录结构现在应如下所示:

FolderName
|- libstaticlibrary.a
|-  StaticClass.h

Now you can zip this folder and sell it to would-be iOS developers for millions!

现在您可以压缩此文件夹并将其以数百万美元的价格出售给潜在的 iOS 开发人员!

Step 4:Linking Your Library In A New Project So now that you have built your shiny new static library, it's time to test it out in another application.

第 4 步:在新项目中链接您的库 既然您已经构建了闪亮的新静态库,是时候在另一个应用程序中对其进行测试了。

Create a new View-Based project (or whatever it doesn't really matter). Name it as Test.

创建一个新的基于视图的项目(或任何无关紧要的项目)。将其命名为测试。

Now, just drag this folder into the project and XCode will set up all of the linking automatically. When prompted to copy, I usually say yes, but you can do whatever you want depending on how you intend on using the library. Sometimes just linking and not copying is far more beneficial if you have multiple projects sharing a single library. It ensures that they all use the most up to date version.

现在,只需将此文件夹拖到项目中,XCode 将自动设置所有链接。当提示复制时,我通常会说是,但你可以做任何你想做的事情,这取决于你打算如何使用库。如果您有多个项目共享一个库,有时只是链接而不是复制会更有益。它确保他们都使用最新的版本。

You should now see the .afile along with the header files in the new project.

您现在应该.a在新项目中看到该文件以及头文件。

Step 5:Using The Static Library Code

第 5 步:使用静态库代码

Now that all of the linking is set up, you just use your library like any other class.

现在所有的链接都已设置,您只需像使用任何其他类一样使用您的库即可。

For Further clarifications http://www.raywenderlich.com/41377/creating-a-static-library-in-ios-tutorialhope this tutorial helps for you.

有关进一步说明 http://www.raywenderlich.com/41377/creating-a-static-library-in-ios-tutorial希望本教程对您有所帮助。

回答by yoAlex5

Objective-C consumer -> Objective-C static library

Objective-C 消费者 -> Objective-C 静态库

Xcode version 10.2.1

Xcode 版本 10.2.1

Create Objective-C static library

创建 Objective-C 静态库

Follow Create Objective-C static library

跟随 Create Objective-C static library

Objective-C consumer with Objective-C static library

带有 Objective-C 静态库的 Objective-C 使用者

Follow Swift consumer with Objective-C static library

跟随 Swift consumer with Objective-C static library

Import module to the Objective-C client code[module_name]

将模块导入 Objective-C 客户端代码[module_name]

@import module_name;

More examples here

更多例子在这里