6

I have XCode installed, but for some reason, /usr/local/lib is not amongst the default library search paths:

gcc -Xlinker -v

gives me:

@(#)PROGRAM:ld  PROJECT:ld64-224.1
configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 armv6m armv7m armv7em
Library search paths:
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib
Framework search paths:
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/

This is unfortunate since /usr/local/lib is a fairly canonical location for installed libraries and and there is no /etc/ld.so.conf+ldconfig on mac os x to modify the default library search paths. So without using -L/usr/local/lib this results in a linker error. Is there any other, non-runtime option than setting the environment variable DYLD_LIBRARY_PATH?

EDIT: Setting the DYLD_LIBRARY_PATH env variable did nothing for me. I had to set the LIBRARY_PATH env variable instead to be able to link libraries installed under /usr/local/lib with gcc.

Was there an option about this when installing XCode? (it's a work computer, haven't installed it myself)

armando.sano
  • 345
  • 1
  • 3
  • 9
  • No option that I saw when I installed Xcode. Also, neither `DYLD_LIBRARY_PATH` nor `LIBRARY_PATH` nor `LD_LIBRARY_PATH` as seen [here](http://stackoverflow.com/questions/17889799/libraries-in-usr-local-lib-not-found) worked for me. Still searching for a solution. – Francisco Aguilera Mar 11 '15 at 04:14
  • 1
    What does `gcc -Xlinker -v` give you? I added this to my `~/.bash_profile`: `if [ -z "${LIBRARY_PATH}" ] #\n then #\n export LIBRARY_PATH="/usr/lib:/usr/local/lib" #\n else #\n export LIBRARY_PATH="LIBRARY_PATH:/usr/lib:/usr/local/lib" #\n fi` (showing newlines with `#\n`). After this, `gcc -Xlinker -v` also lists `/usr/local/lib` (open a new terminal window for `.bash_profile` to take effect) – armando.sano Mar 12 '15 at 05:32
  • `gcc -Xlinker -v @(#)PROGRAM:ld PROJECT:ld64-241.9 configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 x86_64h armv6m armv7m armv7em Library search paths: /usr/lib /usr/local/lib Framework search paths: /Library/Frameworks/ /System/Library/Frameworks/ Undefined symbols for architecture x86_64: "_main", referenced from: implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)` – Francisco Aguilera Mar 12 '15 at 07:54
  • Also, man ld states: `Search paths ld maintains a list of directories to search for a library or framework to use. The default library search path is /usr/lib then /usr/local/lib. The -F option will add a new framework search path. The -Z option will remove the standard search paths. The -syslibroot option will prepend a prefix to all search paths.` – Francisco Aguilera Mar 12 '15 at 07:57
  • are your environment variables correctly set up? try `echo $LIBRARY_PATH` in a terminal. I know, the `man` page of `ld` does say this, but it's not correct – armando.sano Mar 13 '15 at 09:38
  • sorry missed that the library search path was listed correctly. For me that is all that was needed to make the linker have the right paths when compiling. – armando.sano Mar 13 '15 at 09:47
  • No $LIBRARY_PATH variable is set up on mine. However, xcode does state that it searches /usr/local/lib and so does ld. So that is strange that you had to set yours up like that. – Francisco Aguilera Mar 13 '15 at 10:01
  • Anyways, I set up my bash_profile to export that $LIBRARY_PATH just in case... – Francisco Aguilera Mar 13 '15 at 10:04

2 Answers2

4

To add a temporary library to my project using Xcode I did the following:

enter image description here

To add a temporary include path to my XCode library search paths I had to do the following:

enter image description here

If you want to add default include and search paths 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.

Community
  • 1
  • 1
Francisco Aguilera
  • 3,099
  • 6
  • 31
  • 57
  • Your `gcc -Xlinker -v` does list the right library search path, did you do this in XCode before listing the result of `gcc -Xlinker -v`? Or I suspect your environment variables are correctly set. For me that was all what was needed. – armando.sano Mar 13 '15 at 09:42
  • Does it now work for projects that do not use XCode i.e. is it a global setting, or restricted to the project? (and do you still have your env variables LIBRARY_PATH set up?) – armando.sano Mar 13 '15 at 09:50
  • very useful for me regarding the default include and search paths! just put it in my .zshrc file – CharMstr Apr 19 '21 at 22:32
1

Environment variables starting with DYLD_ like DYLD_LIBRARY_PATH are specific to Apple's dynamic link editor called dyld. The manual pages state:

DYLD_LIBRARY_PATH This is a colon separated list of directories that contain libraries. The dynamic linker searches these directories before it searches the default locations for libraries. It allows you to test new versions of existing libraries. For each library that a program uses, the dynamic linker looks for it in each directory in DYLD_LIBRARY_PATH in turn. If it still can't find the library, it then searches DYLD_FALL- BACK_FRAMEWORK_PATH and DYLD_FALLBACK_LIBRARY_PATH in turn. Use the -L option to otool(1). to discover the frameworks and shared libraries that the exe- cutable is linked against.

Please note that the DYLD_FALLBACK_LIBRARY_PATH already contains a reference to standard /usr/local/lib by default.

DYLD_FALLBACK_LIBRARY_PATH This is a colon separated list of directories that contain libraries. It is used as the default location for libraries not found in their install path. By default, it is set to $(HOME)/lib:/usr/local/lib:/lib:/usr/lib.

Xcode has Project-wide or Target-specific Build Settings such as "Library Search Paths" where you would define paths to non-standard locations that you need to let the linker know about.

Apart from copying and adding *.dylib files to your Xcode project you need to "make install" those to one of these known library locations. Otherwise the O/S loader (launchd process) cannot use them during actual launch or run-time of your app image.

See: https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/UsingDynamicLibraries.html

Please note that C++ as well as Object-C have their challenges regarding Dynamic Libraries.

Anthony
  • 101
  • 4