Using a Static Library in an iOS App


01/13/2017

static library

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.

  1. 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.
  2. static library

  3. 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.
  4. static library

  5. 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.
  6. Static library

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.

static library

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.

Summary

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:

  1. Configure the project to reference the library include files.
  2. Configure the project to reference the library executable files.
  3. Configure the project to link with the library files.
  4. Write the required code to interact with the library API.
  5. Call the library wrapper code from any Objective-C code.

Stephen Bucholtz, Principal Engineer at Accusoft, has been developing software for longer than he cares to admit. He has worked on a wide variety of systems, including finance and account systems in COBOL, air traffic control systems in Ada, embedded communication systems in C, image processing toolkits in C/C++ (and even a bit of assembly), iOS and Android apps, and most recently SaaS systems using Javascript and node.js. When not developing software, Stephen can be found playing drums with various jazz groups around the area.

Share: Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInEmail this to someone

Related posts


PrizmDoc Application Services (PAS) Overview
Read More >
TeamCity Dependencies
Visualizing TeamCity Dependencies with Python
Read More >
reactive javascript
Vue.js: Embracing Reactive JavaScript Without Losing Your Mind
Read More >

Join the discussion.