Using a Static Library in an iOS App
Stephen Bucholtz, Accusoft Principal Engineer
Are you developing an iOS app? Do you need to perform some specialized processing? Perhaps you have found a static C library that implements exactly what you need. Assuming the C library has been built for iOS, did you know that you can easily incorporate the library into your app? (Porting a C library to iOS is a topic for another blog post.) This post will show you how to incorporate a C library into your Objective-C iOS app, using Xcode.
A Real-World Example
For this post, we will use Accusoft’s photo compression app, Cram, as an example. This app compresses the photos on your mobile device by up to 60%, without any degradation of image quality. It makes use of C libraries provided by the AIMTools mobile imaging software development kit. By incorporating the libraries from AIMTools, the app developers were able to focus on the iOS-specific aspects of the app, leaving the image processing aspects to the imaging experts.
Configuring the Project
C libraries generally consist of header files that expose the library’s API and the actual static library executable file. These assets need to placed somewhere in the project’s directory structure. Then, Xcode will be configured to use these files.
For AIMTools, a set of include files is required, which are incorporated into the project directory structure in an include directory. Several static library files are required, which reside in a lib directory in the project structure.
After the required files are in place in the project directory structure, the Xcode project configuration will be updated to incorporate these files.
In Xcode, there are three project Build Settings that must be configured.
- The User Header Search Paths setting is configured to point to the location of the library include files. In the example below, relative addressing is used to reference the library directory. AIMTools also has some useful include files in the samples/common directory, so the project references that directory as well.
- The Library Search Paths setting is configured to point to the location of the library executable files. The example below uses the handy Xcode predefined variable: PROJECT_DIR to reference the lib directory contained within the project’s root directory.
- The Other Linker Flags setting is used to link the iOS app with the library. This setting is configured to refer to each of the required library executable files. In this case, three library files, which reside in the lib directory referenced in Step 2, are required: libpici20.a, libpici2320.a, and libpicnullop.a.
Using the Library
Now that the library has been incorporated into the project, how do we use it? Since the library exposes a C API and the setup of C data structures is required, a C file is added to the project that serves as the interface to the library. This C file exposes a function (RequantizeJPEG) that can be called from Objective-C to perform the required image processing. This function calls the library API function (Pegasus), that performs an image processing operation. A snippet of RequantizeJPEG is shown below.
The function RequantizeJPEG is also declared in a .h file. Any Objective-C file can import this .h file and call the exposed function with a function call like this:
status = RequantizeJPEG(buffer, imageSize, &outputBuffer, &outputSize, (WORD)quality);
So with this function call, the iOS app can make use of the image processing functionality embedded in AIMTools.
We have seen that incorporating a static library into an iOS app is quite simple. Follow the following simple steps and you can easily incorporate specialized processing into an app:
- Configure the project to reference the library include files.
- Configure the project to reference the library executable files.
- Configure the project to link with the library files.
- Write the required code to interact with the library API.
- Call the library wrapper code from any Objective-C code.