61

(Posting this question for reference purpose, I'll answer immediately)

How to add header search paths to Xcode? Especially when including with this syntax:

include <myheader.h>
  1. Adding path globally to all projects like system headers.
  2. Adding path to only to a specific project.
eonil
  • 83,476
  • 81
  • 317
  • 516

4 Answers4

105

We have two options.

  1. Look at Preferences->Locations->"Custom Paths" in Xcode's preference. A path added here will be a variable which you can add to "Header Search Paths" in project build settings as "$cppheaders", if you saved the custom path with that name.

  2. Set HEADER_SEARCH_PATHS parameter in build settings on project info. I added "${SRCROOT}" here without recursion. This setting works well for most projects.

About 2nd option:

Xcode uses Clang which has GCC compatible command set. GCC has an option -Idir which adds system header searching paths. And this option is accessible via HEADER_SEARCH_PATHS in Xcode project build setting.

However, path string added to this setting should not contain any whitespace characters because the option will be passed to shell command as is.

But, some OS X users (like me) may put their projects on path including whitespace which should be escaped. You can escape it like /Users/my/work/a\ project\ with\ space if you input it manually. You also can escape them with quotes to use environment variable like "${SRCROOT}".

Or just use . to indicate current directory. I saw this trick on Webkit's source code, but I am not sure that current directory will be set to project directory when building it.

The ${SRCROOT} is predefined value by Xcode. This means source directory. You can find more values in Reference document.

PS. Actually you don't have to use braces {}. I get same result with $SRCROOT. If you know the difference, please let me know.

eonil
  • 83,476
  • 81
  • 317
  • 516
  • 3
    Note also that ${SRCROOT} is the directory containing the .xcodeproj directory, *not* the directory where Xcode puts your source files (at least on my Xcode 4.6.3). To get to my source, I had to go one level deeper. – Randall Cook Oct 13 '13 at 04:16
  • 6
    The difference, with or without braces, is that with braces the path can contain white space. – Prof. Falken Dec 31 '14 at 22:29
  • 2
    LOVE YOU FOR THE BACKSLASH TRICK – Tom Roggero Dec 22 '15 at 01:15
  • Is that "do step 1 then step 2", or "do option 1 or option 2"? – jameshfisher Jan 02 '16 at 22:21
  • @jameshfisher These are options. Not steps. You can choose one what you want. If you're not sure what to choose, go option #2. – eonil Apr 18 '16 at 03:41
  • @user124384 Maybe first option has. But seconds option seems still working. – eonil Apr 19 '17 at 00:48
  • Interesting, does not work in Xcode Version 11.3 (11C29). I added and it still forces me to use #include "theheader.h" vs #include . Developing for embedded system and by nature the headers that I need to use as "system header" are not part of macOS. Seems only resort is to put these foreign headers in the only place where Xcode Version 11.3 (11C29) seems to be able to find them... next to stdio.h or such. – Cerniuk Dec 24 '19 at 13:01
14

Follow up to Eonil's answer related to project level settings. With the target selected and the Build Settings tab selected, there may be no listing under Search Paths for Header Search Paths. In this case, you can change to "All" from "Basic" in the search bar and Header Search Paths will show up in the Search Paths section.

Prof Von Lemongargle
  • 3,658
  • 31
  • 29
3

To use quotes just for completeness.

"/Users/my/work/a project with space"/**

If not recursive, remove the /**

Marshall Davis
  • 3,337
  • 5
  • 39
  • 47
  • 4
    Also, be sure to use curly brackets for variables as shown above, rather than the round variety. The latter seems to causes parsing issues with projects that have spaces. – Max MacLeod May 25 '11 at 09:00
3

Though this question has an answer, I resolved it differently when I had the same issue. I had this issue when I copied folders with the option Create Folder references; then the above solution of adding the folder to the build_path worked. But when the folder was added using the Create groups for any added folder option, the headers were picked up automatically.

Ken Keenan
  • 9,818
  • 5
  • 32
  • 49
roshi
  • 127
  • 1
  • 5