6

Tired of having to change my header and library search paths in every project to be able to link libraries from homebrew in /usr/local/lib and /usr/local/include.

Is there a way to make Xcode recognize those two search paths by default? ld states it uses those search paths by default but it seems Xcode does not.

Also, is there a way to add my libraries from /usr/local/lib to this dialog without having to select add other?

add library dialog

2
  • I could help with GCC, but it seems that recent Xcode is based on LLVM / Clang instead. – Dmitry Grigoryev Apr 3 '15 at 18:03
  • @DmitryGrigoryev Well, Xcode lets you decided whether to compile in GNU (GCC) mode or CLANG mode in a project's settings, but that is not relevant to my question as my question is about changing Xcode's settings themselves. As a hint to others more knowledgeable in this area, there is probably a way to do this by editing some of Xcode's files, maybe some plist file? Not sure. – Francisco Aguilera Apr 3 '15 at 23:41
4

If you want to add default include and search paths that persist across all projects, you need to use:

For include paths:

CPATH
C_INCLUDE_PATH
CPLUS_INCLUDE_PATH
OBJC_INCLUDE_PATH

And for library paths:

LIBRARY_PATH

In order for Xcode and other GUI applications in OS X (tested on 10.10) to be able to access these environment variables, you need to set variables using:

/bin/launchctl setenv LIBRARY_PATH /usr/local/lib
/bin/launchctl setenv CPATH /usr/local/include

But these are not permanent. In order to have these variables across restarts, you need to create a startup script. See this page for an example.

This is my personal applescript file which I have saved as an Application in iCloud (so it survives reformats) and have set as a startup item in system preferences->users and groups->login items.

set ENV_LIBRARY_PATH to "/bin/launchctl setenv LIBRARY_PATH /usr/local/lib;"
set ENV_CPATH to "/bin/launchctl setenv CPATH /usr/local/include;"
do shell script ENV_LIBRARY_PATH & ENV_CPATH

As for adding them to the dialog, there are two ways which i have found:

  1. The quick and dirty way: sudo ln -s /usr/local/lib/ /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/lib

    This will add all of your lib files from /usr/local/lib to the dialog under the same OS X 10.10 "folder" in the dialog.

  2. The second way involves creating your own .sdk directory with a usr subdirectory in /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs, then copying over the SDKSettings.plist from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/SDKSettings.plist to your .sdk directory, editing it using Xcode, and symlinking your /usr/local/bin, /usr/local/include /usr/local/lib to your mydir.sdk/usr/ directory.

1

Is there a way to make Xcode recognize those two search paths by default?

"Can you please tell me how to add include path and library path in XCode project?"

You're looking for "Header Search Path" and "Library Search Paths."

Use the handy search field at the top to find them.

Also, make sure the "Show" pop-up is set to "All Settings".

If it's set to one of the other values, then you won't necessarily see the setting you're looking for.

enter image description here

Source How to add include path and library path in XCode project


Further reading

1
  • My question asks for setting default search paths (that are always searched regardless of per-project settings). See my question, and answer below for the solution. – Francisco Aguilera Apr 6 '15 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.