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

Solution 1:

Each of the solutions I've seen to this problem have either seemed inelegant (copying headers into the application's project) or overly simplified to the point that they only work in trivial situations.

The short answer

Add the following path to your User Header Search Paths

"$(BUILD_ROOT)/../IntermediateBuildFilesPath/UninstalledProducts"

Why does this work?

First, we need to understand the problem. Under normal circumstances, which is to say when you Run, Test, Profile or Analyze, Xcode builds your project and puts the output in the Build/Products/Configuration/Products directory, which is available via the $BUILT_PRODUCTS_DIR macro.

Most guides regarding static libraries recommend setting the Public Headers Folder Path to $TARGET_NAME, which means that your lib file becomes $BUILT_PRODUCTS_DIR/libTargetName.a and your headers are put into $BUILT_PRODUCTS_DIR/TargetName. As long as your app includes $BUILT_PRODUCTS_DIR in its search paths, then imports will work in the 4 situations given above. However, this will not work when you try to archive.

Archiving works a little differently

When you archive a project, Xcode uses a different folder called ArchiveIntermediates. Within that folder you'll find /YourAppName/BuildProductsPath/Release-iphoneos/. This is the folder that $BUILT_PRODUCTS_DIR points to when you do an archive. If you look in there, you'll see that there is a symlink to your built static library file but the folder with the headers is missing.

To find the headers (and the lib file) you need to go to IntermediateBuildFilesPath/UninstalledProducts/. Remember when you were told to set Skip Install to YES for static libraries? Well this is the effect that setting has when you make an archive.

Side note: If you don't set it to skip install, your headers will be put into yet another location and the lib file will be copied into your archive, preventing you from exporting an .ipa file that you can submit to the App Store.

After a lot of searching, I couldn't find any macro that corresponds to the UninstalledProducts folder exactly, hence the need to construct the path with "$(BUILD_ROOT)/../IntermediateBuildFilesPath/UninstalledProducts"

Summary

For your static library, make sure that you skip install and that your public headers are placed into $TARGET_NAME.

For your app, set your user header search paths to "$(BUILT_PRODUCTS_DIR)", which works fine for regular builds, and "$(BUILD_ROOT)/../IntermediateBuildFilesPath/UninstalledProducts", which works for archive builds.

Solution 2:

I ran into this same issue when developing my own static library and although Colin's answer was very helpful, I had to modify it a bit to work consistently and simply when both running and archiving projects under Xcode 4 using a Workspace.

What's different about my method is you can use a single user header path for all your build configurations.

My method is as follows:

Create a Workspace

  1. Under Xcode 4, go to File, New, Workspace.
  2. From Finder you can then drag in the .xcodeproj projects for both the static library you want to use, and the new app you are building that uses the library. See Apple Docs for more info on setting up Workspaces: https://developer.apple.com/library/content/featuredarticles/XcodeConcepts/Concept-Workspace.html

Static Library Project Settings

  1. Make sure all the static library's headers are set to copy to "Public". This is done under the settings for the static library target > Build Phases. In the "Copy Headers" phase, make sure all your headers are within the "Public" section.
  2. Next go to Build Settings, find "Public Headers Folder Path" and type in a path for your library. I choose to use this:

include/LibraryName

I've adopted this from use with RestKit and found it works best with all my static libraries. What this does is tells Xcode to copy all the headers we moved to the "Public" headers section in step 1 to the folder we specify here which resides within the Derived Data folder when building. Like with RestKit, I like using a single "include" folder to contain each static library I'm using in a project.

I also don't like using macros here because it will allow us to use a single user header search path later when we configure the project using the static library.

  1. Find "Skip Install" and make sure this is set to YES.

Settings for the Project Using the Static Library

  1. Add the static library as a framework under Build Phases > Link Binary With Libraries and add the libLibraryName.a file for whatever static library you want to use.
  2. Next make sure project is set to search for User Search Paths. This is done under Build Settings > Always Search User Paths and make sure its set to YES.
  3. In the same area find User Header Search Paths and add:

    "$(PROJECT_TEMP_DIR)/../UninstalledProducts/include"

This tells Xcode to look for static libraries within the intermediate build folder that Xcode creates during the build process. In here, we have the "include" folder we are using for our static library locations we setup in step 2 for the static library project settings. This is the most important step in getting Xcode to correctly find your static libraries.

Configure the Workspace

Here we want to configure the workspace so that it will build the static library when we build our app. This is done by editing the scheme used for our app.

  1. Make sure you have the scheme selected that will create your application.
  2. From the scheme drop-down, choose Edit Scheme.
  3. Select Build at the top of list on the left. Add a new target by pressing the + on the middle pane.
  4. You should see the static library show up for the library you are trying to link. Choose the iOS static library.
  5. Click both Run and Archive. This tells the scheme to compile the libraries for the static library whenever you build your app.
  6. Drag the static library above your application target. This makes the static libraries compile before your application target.

Start Using the Library

Now, you should be able to import your static library using

import <LibraryName/LibraryName.h>

This method gets around the hassle of having to have different user header paths for different configurations, so you should have no problem compiling for archives.

Why does this work?

It all depends on this path:

"$(PROJECT_TEMP_DIR)/../UninstalledProducts/include"

Because we configure our static library to use "Skip Install", the compiled files are moved to the "UninstalledProjects" folder within the temporary build directory. Our path here also resolves to the "include" folder we setup for our static library and use for our user header search path. The two working together lets Xcode know where to find our library during the compile process. Since this temporary build directory exists for both Debug and Release configurations, you only need a single path for Xcode to search for static libraries.