Static Libraries in Xcode 4

Static libraries in Xcode 4

Lots of hoop jumping, but here are my notes now that it I got it working.

  • If you create a new stock XCode4 iOS "Cocoa Touch Static Library" project (and add some code to it) the project will build fine out of the box. But the Product file libLibraryName.a only turns black (from red, denoting the file doesn't exist) when you do a device build. A simulator build does not show that the target was built when in fact it was.

  • In the project target build-settings, the "Per-Configuration Build Products Path" defaults to $(BUILD_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME) If you change this to something else (or if you upgraded the project from XCode3.x which used $(BUILD_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)$(IPHONEOS_DEPLOYMENT_TARGET) as the default, I believe), then the Product file libLibraryName.a wont ever turn black. To me this says there's a bug in XCode somewhere.

  • I can live without the Product file turning black after a build (it is a nice indicator, but hey, whatever.) But I do need my consuming project to be able to find the correct build of the library, be it for simulator or device. In an ideal world there would be a single .a file with i386/arm6/arm7 bits in it, but again, this isn't my library / library project.

  • The XCode4 Transition Guide is what showed me the light. It prescribes creating a workspace that hosts both of the projects, and they will both build into the same shared build directory. I was not using a workspace previously, so I used the File/Save As Workspace command to create the new workspace file. Then, I added the library project, taking care to ensure it was placed as a peer to my primary project and not as a child.

  • I had to make sure the workspace was configured to place build output into a common folder. In the Workspace Settings dialog, set the Build Location setting to "Place Build Products in Derived Data Location"

  • I also had to make sure to check the "shared" checkbox for each project in the Manage Schemes dialog.

  • Finally, to specify the library dependency for my main project, I simply went to the target Build Phases tab, Link Binaries with Libraries section, and clicked the '+', then selected the libLibraryName.a file from beneath the Workspace folder. Note that I'd tried this before when there was no workspace and no common build dir, and the result was that XCode couldn't find the .a file during link.

All said and done, it works like a charm. I can't help but think it should be a lot easier - as I believe it was in XCode3.

I'd be happy to read about anyone else's experience with all this, or any feedback about other (simpler?) ways to make linking static libs work well.

Xcode 4: How to Add Static Library Target Dependency to Project

In general Xcode 4 seems to discover the dependencies automatically as the Edit Scheme sheet implies. Other developers have mentioned that the dependencies are not automatically discovered and require explicitly listing them:

So, Edit Scheme -> Build -> add targets from your workspace.

As far as the static library header files go, Xcode 4 seems to have a problem, at least with code completion and syntax highlighting. The only way I can get either to work properly with classes in static libraries to to drag a copy of the header files in question to a location into a group folder in the main project. Note that you should uncheck Add to Target... That takes care of the syntax highlighting and code completion. The rest should be handled by giving it the proper header search path. That would be User Header Search Paths = $(BUILT_PRODUCTS_DIR) - depending on how you set up your locations preferences.

See: this link

Xcode 4 can't locate public header files from static library dependency

Xcode 4 Project Fails to compile a static library

Related question: “lexical or preprocessor issue file not found ” in Xcode 4

Errors might include; missing header files, "lexical or preprocessor issue"

Solutions:

  1. Check the "user header paths" are correct
  2. Set "Always search user paths" to YES
  3. Create a group call "Indexing headers" in your project and drag the headers to this group, DO NOT add to any targets when prompted.

Adding a static library to an XCode 4 project breaks build and archive feature

You should change your static library's "Build Settings", make sure Skip Install is set to YES

Sample Image

If still not work, check your static library's "Build Phases", make sure:

Sample Image

After that, your Archive should be OK.
But if your Xcode start to report who can not find your static library's .h files, you can just add them into your project as references, or change your project's "Build Settings", let it find your .h files in your static library's folder.

Linking a static library to an iOS project in Xcode 4

I do this as follows:

  1. Drag in the static library project. If you have the static library project open in Xcode, close it now.
  2. Select the main project in the project navigator (the project I'm adding the static library to) and in the editor, under the header TARGETS in the left-hand column, select my main project's target and navigate to the Build Phases tab.
  3. Click the "+" for Target Dependencies and add the library icon target dependency from the added static library project.
  4. Click the "+" for Link Binary with Libraries and add the library icon that is under the folder "Workspace".
  5. It may also be necessary to enter a Header Search Path for the headers of the static library project if that is how the headers are linked in the static library project itself.

If you don't see the static library project as nested under the main project in the main project's project navigator, the most likely reason for that is that the static library's own Xcode project is still open. Quit Xcode and open up the main project that has the nested static library project in it without opening up the original static library project itself, and you should see it appearing as a nested project in your main project.

Changing the source of a static library needs clean and build in xcode 4

This is a bug in xcode 4.0.2 (might be fixed in future version). From https://devforums.apple.com/thread/91711?start=25&tstart=0:

  1. Set static libraries in project,
    under Frameworks to: Relative to
    Build Products
  2. Close XCode
  3. Edit project.pbxproj and remove all the path components of the static library so that only the filname remains, like this (the important part is "path = libLibrary.a")

    A74F787413566130000D0AFC /* libLibrary.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libLibrary.a; sourceTree = BUILT_PRODUCTS_DIR; };



Related Topics



Leave a reply



Submit