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

72,234

Solution 1

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.

Solution 2

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 3

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.

Solution 4

This was a very helpful thread. In researching for my own situation, I found that Apple has a 12-page document dated September 2012 titled "Using Static Libraries in iOS." Here's the pdf link: http://developer.apple.com/library/ios/technotes/iOSStaticLibraries/iOSStaticLibraries.pdf

It's much simpler than most of the Internet discussion, and with some small mods to account for how the external libraries I'm using are configured, it's working well for me. The most important part is probably:

If your library target has a “Copy Headers” build phase, you should delete it; copy headers build phases do not work correctly with static library targets when performing the “Archive” action in Xcode.

New static library targets created with Xcode 4.4 or later will come with an appropriately-configured Copy Files phase for headers, so you should check to see if you already have one before creating one. If you do not, press “Add Build Phase” at the bottom of the target editor and choose to “Add Copy Files.” Disclose the new Copy Files build phase and set the Destination to “Products Directory.” Set the Subpath to include/${PRODUCT_NAME}. This will copy files into a folder named after your library (taken from the PRODUCT_NAME build setting), inside a folder named include, inside your built products directory. The include folder inside a build products directory is in the default header search path for applications, so this is an appropriate place to put header files.

I am sure that in many existing situations Apple's approach may not be enough. I post this here for anyone who is just beginning their journey on the static library garden path - this may be the best starting point for simple cases.

Solution 5

http://developer.apple.com/library/ios/#technotes/iOSStaticLibraries/Articles/creating.html

Per Apple documention:

Your library will have one or more header files that clients of that library need to import. To configure which headers are exported to clients, select your library project to open the project editor, select the library target to open the target editor, and select the build phases tab. If your library target has a “Copy Headers” build phase, you should delete it; copy headers build phases do not work correctly with static library targets when performing the “Archive” action in Xcode.

Share:
72,234
Richard Stelling
Author by

Richard Stelling

Tech geek and hopelessly optimistic about my pessimistic outlook. http://www.richardstelling.com http://twitter.com/rjstelling http://careers.stackoverflow.com/rjstelling SOreadytohelp

Updated on February 08, 2020

Comments

  • Richard Stelling
    Richard Stelling about 4 years

    Alternate titles to aid search

    • Xcode can't find header
    • Missing .h in Xcode
    • Xcode .h file not found
    • lexical or preprocessor issue file not found

    I'm working on an iOS application project which came from Xcode 3. I have now moved to Xcode 4 my project builds a number of static libraries.

    Those static libraries also declare public headers and those headers are used by the application code. In Xcode 3.x the headers were copied (as a build phase) to the public headers directory, then in the application project the public headers directory was added to the headers search list.

    Under Xcode 4 the build directory is moved to ~/Library/Developer/Xcode/DerivedData/my-project.

    The problem is how do I reference this new location in the headers search settings? It seems that:

    • public headers directory is relative to DerivedData directory, but
    • headers search directory is relative to something else (possibly the project location)

    How should I set up a static library target for iOS development in Xcode 4 that will ensure the header files are made available to the clients that use the static library when trying to compile as a dependancy?

  • Brad
    Brad over 12 years
    Another easily overlooked but extremely important step: Ensure that your search paths are wrapped in double quotes to escape any spaces. I always forget to do that.
  • Julian D.
    Julian D. over 12 years
    Thanks @Brad, this is indeed a very important and helpful remark.
  • Richard Stelling
    Richard Stelling about 12 years
    How does this relate to the DerivedData folder?
  • Colin
    Colin about 12 years
    Regular builds go to: DerivedData/WorkspaceName-hash/Build/Products/Debug-iphoneos‌​/TargetName.app --- Archive builds go to: DerivedData/WorkspaceName-hash/Build/Intermediates/ArchiveIn‌​termediates/TargetNa‌​me/BuildProductsPath‌​/Release-iphoneos/Ta‌​rgetName.app
  • Codezy
    Codezy almost 12 years
    Good note on the recursive checkbox, same issue here.
  • Codezy
    Codezy almost 12 years
    Combine this solution with Collins solution and that did it for me.
  • neevek
    neevek almost 12 years
    You are awesome, man! This is really helpful, and it works as you described.
  • gdavis
    gdavis almost 12 years
    I've added a modified version of this that does not require multiple build paths and makes configuration a little easier. See what you think: stackoverflow.com/a/10855606/189292
  • TPoschel
    TPoschel over 11 years
    For me, using "$(BUILD_ROOT)/../IntermediateBuildFilesPath/UninstalledProd‌​ucts" with the recursive checkbox did not work. As soon as I used "$(BUILD_ROOT)/../IntermediateBuildFilesPath/UninstalledProd‌​ucts/<nameOfStaticLi‌​brary>" without the recursive flag, it worked for me.
  • jscs
    jscs over 11 years
    While this may technically answer the question, it would be preferable for you to include the essential parts of the linked article in your answer, and provide the link for reference. Failing to do that leaves the answer at risk from link rot.
  • John Wordsworth
    John Wordsworth over 11 years
    I just wanted to echo that I found the same result as TPoschel. I wish I'd found that there was one more comment here about 45 minutes ago! I was pulling my hair out. It appears that the recursive setting doesn't carry through to the build here?
  • Colin
    Colin over 11 years
    Curious that the recursive setting isn't working for you guys, yet specifying one more nested folder does work. I'm using multiple static libraries in my project and it'd be a real pain to have to add a line for each one!
  • eonil
    eonil over 11 years
    This method sometimes doesn't work by user's build settings which is very hard to predict. I recommend to put libs and headers somewhere in source file directory and adding relative path to $(SRCROOT) to search path option.
  • Ramesh
    Ramesh over 11 years
    awesome. Where shall I send flowers :)
  • Pascal
    Pascal over 11 years
    Note that your $(BUILD_ROOT)/../IntermediateBuildFilesPath/UninstalledProdu‌​cts is actually $(TARGET_BUILD_DIR). Even while archiving. ;)
  • abbood
    abbood over 11 years
    for those following the above link.. beware: the step that asks you to delete the dummy template files when you create the library project.. DON'T DELETE THE *.pch file.. it will come to haunt you eventually + the above advice doesn't work with categories.. there is a fix for that though (saw it somewhere)
  • MkVal
    MkVal about 11 years
    After setting this up, some of you might encounter "...] unrecognized selector sent to class 0x...". In case you do, try adding this up in your project's Targets->Project->Build Settings->Linking->Other Linker Flags: -all_load
  • augusto callejas
    augusto callejas about 11 years
    Thanks for providing a link to an official Apple approach for building static libraries. I started off by building my static library using this approach, but was unable to Archive. @abbood - What is the problem with deleting the pch file that is generated?
  • abbood
    abbood about 11 years
    if you delete the .pch file.. the project simply won't compile.. (for the purpose of archiving or otherwise)..
  • respectTheCode
    respectTheCode about 11 years
    Make sure the User Header Search Paths are in quotes if they have spaces.
  • c roald
    c roald almost 11 years
    Xcode 4.6.2 is not writing anything to "$(PROJECT_TEMP_DIR)/../UninstalledProducts/include" for me. Any idea what I'm missing? Or has Xcode behaviour changed again?
  • malhal
    malhal almost 11 years
    UninstalledProducts has gone
  • poshaughnessy
    poshaughnessy over 10 years
    I had this under 'Distribution' but I needed to add it to 'Release'. Thanks!
  • Graham Perks
    Graham Perks over 10 years
    Works for me with Xcode 5.1; build and archive success. Thanks!
  • xarly
    xarly about 10 years
    For me in Xcode 5: $(BUILD_ROOT)/../IntermediateBuildFilesPath/UninstalledProdu‌​cts/include/NAME OF LIBRARY I didn't set to YES: Always search user paths.
  • Oliver Pearmain
    Oliver Pearmain over 9 years
    Yep I had to specify a particular sub-folder as well, checking recursive didn't work.
  • Oliver Pearmain
    Oliver Pearmain over 9 years
    I ended up using this path however (simply because its shorter) "$(PROJECT_TEMP_DIR)/../UninstalledProducts/SubProjectHeader‌​s"
  • user1010819
    user1010819 over 9 years
    This works but my bundle size has increased to 80MB.It includes app and extension as well.Any reasons?
  • user1010819
    user1010819 over 9 years
    It doesnt work for me.When I try import statement with "" it doesnt work I need <>.Also while archiving it is not able to find headers.Any ideas?
  • Michael Tyson
    Michael Tyson over 8 years
    This was close: didn't quite work for me, but the following did: "$(TEMP_ROOT)/UninstalledProducts/$(PLATFORM_NAME)"
  • Srini
    Srini almost 8 years
    Worked like a charm, although I got a warning for setting Always Use Search Paths to Yes. After setting it to No, it still works.
  • Raj Pawan Gumdal
    Raj Pawan Gumdal almost 8 years
    For Xcode 7 this worked - "$(OBJROOT)/UninstalledProducts/$(PLATFORM_NAME)/" Source: stackoverflow.com/a/34546680/260665
  • user2002649
    user2002649 over 7 years
    The only thing I could get to work in XCode 7, was a recursive "$(BUILD_ROOT)/../IntermediateBuildFilesPath/UninstalledProd‌​ucts/iphoneos/includ‌​e/"
  • FranticRock
    FranticRock over 6 years
    Does NOT work. Followed instructions to a tee. Same problem: can not find my framework header file. Builds fine, installs fine, on both simulator and side-load to device. Can not archive.
  • Neil Faulkner
    Neil Faulkner over 4 years
    Where do you set this? simply use frameworks - always -
  • Moose
    Moose over 4 years
    It not set anywhere. It's simply an architectural choice. Frameworks are 'modern libraries' which are much easier to link and manage. At the time of this message, swift still not allows to build binary libraries.