Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding system header search path to Xcode

(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.
like image 481
eonil Avatar asked Aug 06 '10 22:08

eonil


People also ask

What is Srcroot?

$(SRCROOT) (aka $(SOURCE_ROOT) ) is a path to your location where a . xcodeproj is. It is simple to check, just put it in a field and Xcode gives you a tip. Follow this answer to receive notifications.


2 Answers

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.

    • https://help.apple.com/xcode/mac/11.4/#/deva52afe8a4
  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.

like image 84
eonil Avatar answered Sep 19 '22 18:09

eonil


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.

like image 21
Prof Von Lemongargle Avatar answered Sep 19 '22 18:09

Prof Von Lemongargle