Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XCode 4.3: Static Library Generation

I've done a some looking around but most of the answers I've found have been or felt incomplete and have left me a little confused. I have been given a C Library that I need to compile into a static library using XCode 4.3 and then use in a separate iOS app project, but I'm unsure about how to proceed. I'm not sure if the directory structure matters or not, but here it is anyways:

Library -> Section1 -> src -> .c files
                    -> sec1 -> .h files
                    -> sec1.h 
        -> Section2 -> src -> .c files
                    -> sec2 -> .h files
                    -> sec2.h

I've been trying to work from this: http://blog.stormyprods.com/2008/11/using-static-libraries-with-iphone-sdk.html which was linked in a question similar to this one though being from 2008 its fairly out of date, nor could I get it to work. There is also this question: Including external C library with Xcode but it doesn't go into the details of actually generating the library, before then including in a separate project.

If someone could provide a clear and up-to-date answer I, and many others, would very much appreciate it I'm sure. Let me know if any more information is needed!

like image 713
Karoly S Avatar asked Jul 25 '12 18:07

Karoly S


1 Answers

To build the static library:

  1. Create a static library project in Xcode
  2. Add all the .c and .h file to the project
  3. Compile

The easiest way to use this library is then to add this static library project to you application project. This avoids having to worry about creating fat libraries (i.e. libraries with code for both the simulator and device).

To add the static library project to your application project:

  1. Choose File > Add Files to ""...
  2. Add the .xcodeproj for your static library
  3. Click on your app's .xcodeproj in the Project Navigator to show build options
  4. Click on your app's target and choose the "Build Phases" tab.
  5. Expand the "Link With Binaries Section"
  6. Click the '+' button
  7. Expand the "Workspace" section (you should see your library, a .a file, there)
  8. Click on your library and you should be good to go.

Apologies for excruciating level of detail above, but somehow people always seem to forget to do steps 4-8 and then they wonder why they are getting link errors!

Xcode will not be able to find the headers for your library. You can either add the public headers to your project as you would any other header file or set the "Header Search Paths" in your build settings.

like image 160
idz Avatar answered Sep 22 '22 13:09

idz