简体   繁体   中英

How to build an Objective-C static library?

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.

So, I want to build these classes into an Objective-C static library. And then link against that library in all other projects.

Is there a way to build a static library such that it works with both Cocoa and cocoa-touch applications?

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.

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?

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.

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.

1.Create new Static Library App in Xcode Xcode中的新静态库

2.You can delete any files it creates and add your own. Add your methods etc. 代码代码代码

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).

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 新建阶段

5.Select the build phase and enter the script from this link into run script window. https://gist.github.com/sponno/7228256

运行脚本代码

6.Now you need to build each architecture (simulator and iOS device)

6.2.Select our libary and then iOS Device, click build or run (Command + B)

6.3.Then for the Simulator (select the Library Icon very top left of Xcode and then the iOS Simulator) 在此处输入图片说明

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.

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"

9.If this all works you should see the following files in finder. You will see the 库.a文件和包含文件

10.The last step is easy, simply drag the "library.a file and the includes folder into your new Xcode project. 在此处输入图片说明

在此处输入图片说明

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.

Now anyone that is using your library can opt+click to bring up context aware help for you classes.

基于头文件的上下文感知帮助

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.

(using the tutorial as a starting point)

now create a second target in the library xcodeproj for the other OS.

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.

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.

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.

Combine that with CocoaPods or Maven (I haven't done this yet) and you could have a pretty nice little ecosystem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM