Mac OS X Xcode Libraries

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


Solution 1:

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.

Solution 2:

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

  • Helping Xcode Find Library Headers