Thursday, March 24, 2011

Using static library

To reduce and reuse common code across multiple development projects, the typical development pattern is to use library. Typically library can be static or dynamic, however IPhone development only support static library. The following steps briefly describe how this can be done.
 
1. Create the xcode static library project using the File->New->New Project. Choose Cocoa Touch Static Library, under the Framework and Library tab. Follow through the steps to specify the name and location to complete the static library creation process. Add the source files and header files. Ensure that the project can be build successfully.


2.  In the project that need to use the above created static library, add the static library project file (eg. MyStaticLib.xcodeproj file) by using the Add File to "MyProject" ... menu. Make sure to choose the following option:
  • Uncheck the Destination - Copy files into destination folder (if neccessary) option. This will ensure that the physical files in the static library project is not copied and only the reference to the static library project is created.
  • Select the create group for any added folders option under the Folder selection. **create folder references for any added folders option seem to work as well.
Once the static library project reference is created, the folder structure will be created under the project file as well.


3. Add the static library project into the target dependencies section of the project.
Also add the static library archeive file (eg. libXXX.a) into the Link binary with library section. Make sure that all the iPhone framework (eg. AVFoundation) that is needed by the static library's codes are added to the Link binary with library section as well.

4. The header file for the static library project can be added to the source code in the target project by either of the following way:
  • Use of absolute path to the static library project (eg. import "/user/admin/XXX/xxx.h").
  • Use of relative path to the static library project (eg. import "xxx.h"), but make sure that the absolute path to the static library include files are added to the User header search paths section for the target project's build setting tag under search path section.
Make sure that the target project is recompiled without any library and test the code accordingly.

No comments:

Post a Comment