简体   繁体   中英

Make /usr/local/lib a default library search path for ld on mac os x?

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)

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

在此处输入图片说明

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

在此处输入图片说明

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM