7

If you create a static library for iOS do you have to distribute the header file(s) with it or is there another way to get it to work?

Currently I have a single my_lib.a file for both device and simulator but when I drag it into another test app to use it, it says it can't find the header and that all the places I'm using it in the code are undeclared. So I figure I'm either doing something wrong, or I have to also send the appropriate header files with it.

Background to my process:

I've seen two guides for creating a static library for both device and simulator. One on this site: Build fat static library (device + simulator) using Xcode and SDK 4+

and one here: http://mark.aufflick.com/blog/2010/11/19/making-a-fat-static-library-for-ios-device-and-simulator

I used the second site to just try it out. I'm also a bit curious if I did it correctly. I just went into the Release-iphone(os|simulator) folders and found the .a in the ios one and the .o in the simulator one.

Community
  • 1
  • 1
Aaron
  • 13,349
  • 11
  • 66
  • 105

2 Answers2

3

The short answer is yes, you have to package header files with your static library. You have to package header files with any library in fact, dynamic or static. The library itself contains the compiled code, but you still have to tell the compiler about the identifiers in the library so when it's compiling your code it knows that they exist.

If you care, you can package your static library into a static framework with a little care. You simply create the same directory structure that a dynamic framework has, with your .a file in place of the .dylib (or .so) file. Frameworks contain a directory for headers, so you can distribute the binary and headers as a single package, and you can easily import headers from a framework without messing with the Additional Header Search Paths build setting.

Adam Milligan
  • 2,826
  • 19
  • 17
  • Thanks for the info Adam. As I understand it (and from what I've googled), devs are not allowed to create frameworks for iOS. Is this no longer so, and if so, do you have any resources on how to create this framework? Finally, am I creating my universal library correctly? I only ask because the guide (2nd link) I used makes no mention of .o files. – Aaron Dec 02 '10 at 16:02
  • I found out that I was going into my project's .build folder (project_name.build/Objects-normal/ARCHITECTURE/project_target.[a|o]) when I should have been going to the higher level Releases-iphone[os|simulator]/project_target.a . Doing the later has the armv6 and armv7 in one .a file and the i386 in the simulator folder's .a file. Also, when adding the iphone os library you don't specify an architecture as it is a fat file containing armv6 and armv7. – Aaron Dec 02 '10 at 18:07
  • I don't know of any really good resources on static frameworks, since Xcode doesn't have a built-in way to make them. I figured them out by finding a couple projects that contained static libraries and picking them apart. PLBlocks came as a static framework, I believe GHUnit comes as a static framework, and I know that Cedar (http://github.com/pivotal/Cedar) has a target to build a static framework using bash scripts. Check them out; it's simply a matter of duplicating the directory structure and symlinks. – Adam Milligan Dec 03 '10 at 03:07
  • As for Apple not allowing custom frameworks on the device, that only applies to dynamic frameworks. The compiler and linker incorporate everything from a static library into your app bundle during the build, so no need to get a static framework onto the device. – Adam Milligan Dec 03 '10 at 03:08
0

Just in case it's useful - I followed Ray Wenderlich's instructions from here and was able to produce a framework for iOS that supported several architectures at once (including the simulator). The instructions are a bit too long to just copy-paste here.