Static Libraries in iPhone SDK

    Recently I encountered the need to create a static library in the iPhone SDK. I found that this process is poorly documented, so I offer you a step-by-step guide on creating and using static libraries in the iPhone SDK.


    Creating a static library


    To describe the process of creating a static library, I used the materials in the article “Building static libraries with the iPhone SDK” and some personal experience.

    Starting point: a finished project in which you want to take out part of the functionality into a separate library.

    1) In your Xcode project, in the “Targets” section, right-click add a new target:

    image

    In the window that opens, select the Static Library:

    image

    Set the library name, for example TestStaticLib .

    2) Transfer existing .m files to our library. No need to add .h files.

    image

    3) Delete the files that we transferred to the library from our project:

    image

    4) Add a link to the library in the “General” tab of the properties of our application:

    image

    Note: If we change something in the source code of the library, we must first recompile it, and then the application itself. Manually recompiling the library is not necessary if you add the library not to Linked Libraries, but to Direct Dependencies.

    5) In the Build tab in the Linking section, add the "-ObjC" flag to the "Other Linker Flags" . This is only necessary if your static library defines the Objective-C classes that your application will use:

    image

    Note: the "-ObjC" flag is not particularly important, as long as there is nothing Objective-C specific in the library, but it’s worth adding to the library, for example, categories for some class - immediately a run-time exception occurs. The reason is the lack of the -ObjC flagfor the linker. This is described in more detail here .

    6) Compile the library.

    7) Compile the application.

    General notes:

    1. After the library has been compiled, the source code can be removed from it.
    2. If the library and the application that uses it are compiled for different targets (for example, the library is release for the simulator, the application is debug for the device), then you will probably get a compilation error. In such cases, you need to use several library options or compile for the same targets.
    3. As a result of compiling the library, we get a file with the extension .a - this is our static library.

    Using the library in a new application


    1). Create a new application, for example LinkingLibraryDemo:

    image

    2). Add our * .a library (Add-> Existing Files-> Navigate to the file -> Check “Copy items into destination group's folder (if needed)” checkbox):

    image

    3). Go to Targets, double click - the Target Info window will open. On the General tab in the Linked Libraries section, you will see the plug-in library.

    image

    4). In the Build tab in the Linking section , add the “–ObjC” flag to Other Linker Flags .

    image

    5). Add all the necessary * .h files to the project.

    After these steps, you can use the library.

    PS From the first glance it may seem that everything is very simple, but I had to tinker for a long time in order to understand the nuances of working with static libraries. I hope this article helps someone save time.

    Thanks for attention!

    Also popular now: