I have some Objective-C classes, which I am currently using in both a Cocoa application (Mac OS X) and a Cocoa-Touch application (iOS). Currently, when I update those classes, I have to copy those updated .h
and .m
files to both projects. Not that big of deal, but I'm going to be using them in many more projects.
我有一些Objective-C类,我目前在Cocoa应用程序(Mac OS X)和Cocoa- touch应用程序(iOS)中都使用这些类。目前,当我更新这些类时,我必须将更新后的.h和.m文件复制到两个项目中。没什么大不了的,但我会在更多的项目中使用它们。
So, I want to build these classes into an Objective-C static library. And then link against that library in all other projects.
我想把这些类构建为Objective-C静态库。然后在所有其他项目中链接到这个库。
Is there a way to build a static library such that it works with both Cocoa and cocoa-touch applications?
是否有一种方法可以构建一个静态库,使它可以同时使用Cocoa和Cocoa -touch应用程序?
I've tried just building a Cocoa static library with these classes, but I get various errors when I try to link against that library in my Cocoa application, and I'm sure that they are coming from the fact that I am not doing it right.
我尝试过用这些类构建一个Cocoa静态库,但是当我尝试在我的Cocoa应用程序中链接那个库时,我得到了各种各样的错误,我确信它们来自于我做得不对的事实。
So, how do I build an Objective-C static library the right way? Do I have to build both a Cocoa-Touch and Cocoa static library? Or can I just do one? How is this done?
那么,如何正确地构建Objective-C静态库呢?我需要同时建立一个可可触摸和可可静态库吗?或者我能做一个吗?这是怎么做的?
Are there any well-done tutorials on this subject?
有关于这个主题的好教程吗?
I have searched the web and cannot seem to find a comprehensive tutorial on this subject, so I've posed the question here, in hopes that a great answer will be posted and this question can serve as a resource for me and for future questioners.
我在网上搜索,似乎找不到关于这个主题的全面教程,所以我在这里提出了这个问题,希望能有一个很好的答案被发布,这个问题可以作为我和未来提问者的资源。
4 个解决方案
#1
25
For XCode 5. These answers seem a bit out of date. You can see the main steps here http://www.raywenderlich.com/41377/creating-a-status-library-in-ios-tutorial . But Xcode 5 does a lot more work for you and now works nearly as you want it to.
在XCode 5。这些答案似乎有点过时。您可以在这里看到http://www.raywenderlich.com/41377/creating-a- state -library-in-ios教程的主要步骤。但是Xcode 5为您做了更多的工作,现在几乎可以按照您的要求工作。
1.Create new Static Library App in Xcode
1。在Xcode中创建新的静态库应用程序
2.You can delete any files it creates and add your own. Add your methods etc.
2。您可以删除它创建的任何文件并添加您自己的文件。添加你的方法等。
3.Little problem with Static Libraries, is that you can only build either for the device or run in the simulator. So we need to create a library that supports ARM and i386 Architectures, to do this we need to create a universal binary (so it will support simulator and on device builds).
3所示。静态库的一个小问题是,您只能为设备构建或在模拟器中运行。因此,我们需要创建一个支持ARM和i386架构的库,为此我们需要创建一个通用的二进制文件(因此它将支持模拟器和设备构建)。
Create a new target (File > New > Target).
创建一个新目标(文件>新的>目标)。
4.Select New Target then add a new build phase (Editor > Add Build Phase > Add Run Script Build Phase
4所示。选择新的目标然后添加一个新的构建阶段(编辑>添加构建阶段>添加运行脚本构建阶段。
5.Select the build phase and enter the script from this link into run script window. https://gist.github.com/sponno/7228256
5。选择构建阶段,并在运行脚本窗口中从这个链接中输入脚本。https://gist.github.com/sponno/7228256
6.Now you need to build each architecture (simulator and iOS device)
6。现在需要构建每个体系结构(模拟器和iOS设备)
6.2.Select our libary and then iOS Device, click build or run (Command + B)
6.2。选择我们的libary,然后是iOS设备,单击build或run(命令+ B)
6.3.Then for the Simulator (select the Library Icon very top left of Xcode and then the iOS Simulator)
6.3。然后是模拟器(选择最左上角的库图标,然后是iOS模拟器)
7.Now you want to build the BullsEye icon > iOS Device. This will be the aggregate target and will combine our two libraries into a new universal file.
7所示。现在,您需要构建BullsEye图标> iOS设备。这将是聚合目标,并将我们的两个库合并到一个新的通用文件中。
8.Lets see if this all worked. Expanded the folder "Products" in the left tree, right click on the file ending with ".a" and "Show in Finder"
8。让我们看看这是否奏效。展开左边树中的文件夹“Products”,右击以“”结尾的文件。a和"Show in Finder"
9.If this all works you should see the following files in finder. You will see the
9。如果一切正常,您应该在finder中看到以下文件。你会看到的
10.The last step is easy, simply drag the "library.a file and the includes folder into your new Xcode project.
10。最后一步很简单,只需拖动“库”即可。新Xcode项目中的文件和包含文件夹。
In your new project, you wont have to do anything else, other than import the header files that want to use, but you do not have to change the linking flags, or header search paths. It will just work.
在您的新项目中,除了导入想要使用的头文件之外,您不必做任何其他事情,但是您不必更改链接标志或头搜索路径。它只会工作。
(any feedback appreciated) or please edit my post to improve clarity.
(如有任何意见)或请编辑我的帖子以提高清晰度。
** BONUS POINTS *** Install VVDocumenter https://github.com/onevcat/VVDocumenter-Xcode and use this to document any header.h files that you will share with the library.
**额外积分***安装VVDocumenter https://github.com/onevcat/vdocumenter-xcode并使用此文件记录任何头文件。h文件,你将与图书馆共享。
Now anyone that is using your library can opt+click to bring up context aware help for you classes.
现在任何使用您的库的人都可以选择+click来为您的类提供上下文感知帮助。
#2
12
I ended up finding this tutorial and it actually worked really nicely. If anyone would still like to offer assistance, please do and I will most likely upvote it and mark it as the accepted answer.
我最终找到了这个教程,它实际上运行得非常好。如果还有人愿意提供帮助,请帮忙,我很可能会投赞成票,并将其标记为已接受的答案。
#3
11
(using the tutorial as a starting point)
(以本教程为起点)
now create a second target in the library xcodeproj for the other OS.
现在在库xcodeproj中为其他操作系统创建第二个目标。
configure each target's sdk settings using an xcconfig file (one for each OS). reuse these xcconfig files in your other libs. this also allows for easy global control of build settings.
使用xcconfig文件(每个OS一个)配置每个目标的sdk设置。在其他libs中重用这些xcconfig文件。这也允许对构建设置进行简单的全局控制。
configure link and dependency references in your targets (apps) as usual, but select the correct target library.
像往常一样在目标(应用程序)中配置链接和依赖项引用,但是选择正确的目标库。
add an aggregate target to the library project for simple meta-compilation/easy maintenance.
为简单的元编译/容易维护向库项目添加聚合目标。
-- or --
——或
do it all by introducing a scripting layer or creating a custom build tool.
通过引入脚本层或创建自定义构建工具来实现这一切。
#4
2
I also found this "Building a Static Library with Jenkins" that is really good. Talks about how to properly setup a static lib, how to get Jenkins to automatically build it and then place the resulting .a and .h files into somewhere you can grab them from.
我还发现这个“用Jenkins构建一个静态库”真的很不错。讨论如何正确地设置一个静态库,如何让Jenkins自动构建它,然后将生成的a和.h文件放到您可以从其获取的任何地方。
Combine that with CocoaPods or Maven (I haven't done this yet) and you could have a pretty nice little ecosystem.
再加上可可粉或Maven(我还没做过这个),你就可以拥有一个相当不错的小生态系统。
#1
25
For XCode 5. These answers seem a bit out of date. You can see the main steps here http://www.raywenderlich.com/41377/creating-a-status-library-in-ios-tutorial . But Xcode 5 does a lot more work for you and now works nearly as you want it to.
在XCode 5。这些答案似乎有点过时。您可以在这里看到http://www.raywenderlich.com/41377/creating-a- state -library-in-ios教程的主要步骤。但是Xcode 5为您做了更多的工作,现在几乎可以按照您的要求工作。
1.Create new Static Library App in Xcode
1。在Xcode中创建新的静态库应用程序
2.You can delete any files it creates and add your own. Add your methods etc.
2。您可以删除它创建的任何文件并添加您自己的文件。添加你的方法等。
3.Little problem with Static Libraries, is that you can only build either for the device or run in the simulator. So we need to create a library that supports ARM and i386 Architectures, to do this we need to create a universal binary (so it will support simulator and on device builds).
3所示。静态库的一个小问题是,您只能为设备构建或在模拟器中运行。因此,我们需要创建一个支持ARM和i386架构的库,为此我们需要创建一个通用的二进制文件(因此它将支持模拟器和设备构建)。
Create a new target (File > New > Target).
创建一个新目标(文件>新的>目标)。
4.Select New Target then add a new build phase (Editor > Add Build Phase > Add Run Script Build Phase
4所示。选择新的目标然后添加一个新的构建阶段(编辑>添加构建阶段>添加运行脚本构建阶段。
5.Select the build phase and enter the script from this link into run script window. https://gist.github.com/sponno/7228256
5。选择构建阶段,并在运行脚本窗口中从这个链接中输入脚本。https://gist.github.com/sponno/7228256
6.Now you need to build each architecture (simulator and iOS device)
6。现在需要构建每个体系结构(模拟器和iOS设备)
6.2.Select our libary and then iOS Device, click build or run (Command + B)
6.2。选择我们的libary,然后是iOS设备,单击build或run(命令+ B)
6.3.Then for the Simulator (select the Library Icon very top left of Xcode and then the iOS Simulator)
6.3。然后是模拟器(选择最左上角的库图标,然后是iOS模拟器)
7.Now you want to build the BullsEye icon > iOS Device. This will be the aggregate target and will combine our two libraries into a new universal file.
7所示。现在,您需要构建BullsEye图标> iOS设备。这将是聚合目标,并将我们的两个库合并到一个新的通用文件中。
8.Lets see if this all worked. Expanded the folder "Products" in the left tree, right click on the file ending with ".a" and "Show in Finder"
8。让我们看看这是否奏效。展开左边树中的文件夹“Products”,右击以“”结尾的文件。a和"Show in Finder"
9.If this all works you should see the following files in finder. You will see the
9。如果一切正常,您应该在finder中看到以下文件。你会看到的
10.The last step is easy, simply drag the "library.a file and the includes folder into your new Xcode project.
10。最后一步很简单,只需拖动“库”即可。新Xcode项目中的文件和包含文件夹。
In your new project, you wont have to do anything else, other than import the header files that want to use, but you do not have to change the linking flags, or header search paths. It will just work.
在您的新项目中,除了导入想要使用的头文件之外,您不必做任何其他事情,但是您不必更改链接标志或头搜索路径。它只会工作。
(any feedback appreciated) or please edit my post to improve clarity.
(如有任何意见)或请编辑我的帖子以提高清晰度。
** BONUS POINTS *** Install VVDocumenter https://github.com/onevcat/VVDocumenter-Xcode and use this to document any header.h files that you will share with the library.
**额外积分***安装VVDocumenter https://github.com/onevcat/vdocumenter-xcode并使用此文件记录任何头文件。h文件,你将与图书馆共享。
Now anyone that is using your library can opt+click to bring up context aware help for you classes.
现在任何使用您的库的人都可以选择+click来为您的类提供上下文感知帮助。
#2
12
I ended up finding this tutorial and it actually worked really nicely. If anyone would still like to offer assistance, please do and I will most likely upvote it and mark it as the accepted answer.
我最终找到了这个教程,它实际上运行得非常好。如果还有人愿意提供帮助,请帮忙,我很可能会投赞成票,并将其标记为已接受的答案。
#3
11
(using the tutorial as a starting point)
(以本教程为起点)
now create a second target in the library xcodeproj for the other OS.
现在在库xcodeproj中为其他操作系统创建第二个目标。
configure each target's sdk settings using an xcconfig file (one for each OS). reuse these xcconfig files in your other libs. this also allows for easy global control of build settings.
使用xcconfig文件(每个OS一个)配置每个目标的sdk设置。在其他libs中重用这些xcconfig文件。这也允许对构建设置进行简单的全局控制。
configure link and dependency references in your targets (apps) as usual, but select the correct target library.
像往常一样在目标(应用程序)中配置链接和依赖项引用,但是选择正确的目标库。
add an aggregate target to the library project for simple meta-compilation/easy maintenance.
为简单的元编译/容易维护向库项目添加聚合目标。
-- or --
——或
do it all by introducing a scripting layer or creating a custom build tool.
通过引入脚本层或创建自定义构建工具来实现这一切。
#4
2
I also found this "Building a Static Library with Jenkins" that is really good. Talks about how to properly setup a static lib, how to get Jenkins to automatically build it and then place the resulting .a and .h files into somewhere you can grab them from.
我还发现这个“用Jenkins构建一个静态库”真的很不错。讨论如何正确地设置一个静态库,如何让Jenkins自动构建它,然后将生成的a和.h文件放到您可以从其获取的任何地方。
Combine that with CocoaPods or Maven (I haven't done this yet) and you could have a pretty nice little ecosystem.
再加上可可粉或Maven(我还没做过这个),你就可以拥有一个相当不错的小生态系统。