66

I am trying to use a C library in an Objective-C Xcode project.

The libraries directory structure is as follows:

-- include/
    |-- config.h
    |-- lib/
    |    |-- file1.h
    |    |-- file2.h
    |    |-- file3.h

The library's docs say to include file1.h, and file1.h includes file2.h and file3.h.

I am getting "file not found" errors for the includes of file2.h and file3.h`. They are included by file1.h in the following manner:

#include <lib/file1.h>
#include <lib/file2.h>

I read here that these angle-brackets instruct the preprocessor to search for include files along the path specified by the INCLUDE environment variable, as opposed to searching in the same directory as the file that contains the #include.

So I added the INCLUDE environment variable in Xcode by going to Product->Edit Scheme.. and set it to /the-whole-path-to/include/ however, I am still getting the file not found errors.

The files are successfully included if I change file1.h to include them like this:

#include "file2.h"

but I'd rather not do that for every file in the library.

How can I fix this?

RyanM
  • 4,474
  • 4
  • 37
  • 44
  • Also see [Xcode Build Setting Reference](https://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html) on Apple developer. – jww Apr 27 '15 at 06:58

7 Answers7

64

In version 5.0.2 of XCode, the best way to accomplish this is probably to add it to the target's "Search Paths" pane. Locating this was (for me) incredibly unintuitive. Here's how I got to it, for those as confused as I:

In the left column, click on the Project Navigator icon (looks like a folder). Then, click on the project entry. This should show a bunch of settings in the main pane. At the top of this pane, click on "Build Settings. This shows a bunch of entries... including one called Search Paths... but you can't add a search path here! This made me gnash my teeth for quite a while, until I figured out that the name of the project at the top of this pane was a pull-down; choose the target from this pull-down, and you should now be able to double click on "Header Search Paths" and perform the needed edit.

Oh, the joy of crazy GUIs.

John Clements
  • 16,895
  • 3
  • 37
  • 52
  • 4
    I don't have any problem adding a search path for the project.. no need to switch to the target: http://i.imgur.com/Tcs36ec.png – Rag Jan 29 '14 at 20:31
  • 1
    @BrianGordon your pic specifically shows target being changed to project. – Anton K May 13 '14 at 00:09
  • 1
    @AntonK Right, it's on project. Not target. The blue icon means project. JohnClements was saying that it has to be on target for that option to appear. – Rag May 15 '14 at 10:42
  • "This made me gnash my teeth for quite a while, until I figured out that the name of the project at the top of this pane was a pull-down;" helps me. Thanks – mxi1 Oct 16 '15 at 05:36
53

Figured it out.

All you have to do is add the -I flag to your build setting under "Other C Flags"

So in your target's build setting search for "Other C Flags" and add -I/path-to-include/

Here's a screenshot: enter image description here

RyanM
  • 4,474
  • 4
  • 37
  • 44
  • In your case, what was the pathToInclude. I ask this because my path is over my xcode project and I have tried with -I/../../folder_to_include and it didn't work. Thanks – xarly Jan 16 '14 at 20:43
  • 1
    See comments below; it's probably better to let XCode know what you're doing by adding it to the Search paths, rather than sticking in a -I flag. – John Clements Jan 16 '14 at 22:38
  • @John Clements, thanks, finally I have used your solution, with that path and it works!, and youre right it's cleaner. Thanks – xarly Jan 17 '14 at 20:36
  • I could not use path like -I$(JAVA_HOME)/include , how can I achieve this? – dg_no_9 Feb 05 '14 at 07:27
  • Why is this solution better than setting the *Header Search Path*? – trojanfoe Apr 27 '15 at 06:58
  • Adding the `-I` to `Other C Flags` has the same effect as using `HEADER_SEARCH_PATHS (Header Search Paths)`. See [Xcode Build Settings Reference](https://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html) on Apple developer. – jww Apr 27 '15 at 06:59
17

Try this:

1 - select your project file in the left Xcode pane
2 - make sure your project is selected in the middle Xcode pane
3 - select "Build Settings" at the top of the middle Xcode pane
4 - be sure "All" & "Combined" are selected just beneath "Build Settings"
5 - type header in the search field just below "Build Settings"

You should see the search path fields ready for editing in the middle pane.

russes
  • 1,110
  • 11
  • 17
12

I solved this in Xcode 5.0.1 using the project Build Settings (as John and Ian noted above, but I cannot comment due to <50 rep).

New info:

When adding includes to User Header Search Paths, I also had to change Always Search User Paths to Yes.

When adding includes to (non-User) Header Search Paths, Always Search User Paths is not required.

Matt
  • 149
  • 1
  • 7
4

Although this works, it is probably better to put it under the "Search Paths" tab instead.

geometrian
  • 14,775
  • 10
  • 56
  • 132
  • 2
    Care to provide directions? – Slipp D. Thompson Sep 30 '13 at 19:27
  • Instead of putting it under "Apple LLVM compiler 4.1 - Language", put it under "Search Paths". There's one specifically for includes. – geometrian Oct 06 '13 at 20:05
  • Answers should stand on their own. Yours seems to refer an existing answer, but as the order in which they are displayed changes all the time, this is a terrible idea. Frankly, a comment underneath the existing answer would be appropriate for what you have to say. – Pascal Cuoq Nov 29 '13 at 14:43
3

Either you can use "Other C Flags" or use "HEADER_SEARCH_PATHS", to specify the include paths, to look for header for your executable.

parasrish
  • 3,864
  • 26
  • 32
0

In 2021, Xcode v. 12.4, the solution seems to be:

Project->Targets->General->"Scan All Source Files For Includes"-> set to "Yes"

This worked for me.

However, this might backfire if you have multiple versions of a specific .h file, probably not a good practice but it's possible if you have lots of sub-directories with their own mini-projects and similarly named include files.

Blisterpeanuts
  • 844
  • 1
  • 10
  • 16