Re: Loading a static library
Re: Loading a static library
- Subject: Re: Loading a static library
- From: Rush Manbert <email@hidden>
- Date: Fri, 23 Jul 2010 16:31:41 -0700
On Jul 23, 2010, at 11:33 AM, JonathanF wrote:
> Can someone help me with a newbie linker question?
>
> How do I tell XCode (I'm running XCode 3.2.2) to use the static version of a particular library? I added the file libboost_thread-mt.a under "Link Binary With Libraries", but I still get the following error message:
>
> Running…
> dyld: Library not loaded: libboost_thread-mt.dylib
> Referenced from: /Users/jonathan/Dwelfen/World/Development/Xcode/EpModel/EpModelCo/build/Debug/EpModelCo.app/Contents/MacOS/EpModelCo
> Reason: image not found
>
> I've looked around the XCode forum, and I've found various postings about how to tell XCode to prefer a static library to the corresponding dynamic version, but I've not found anything about the scenario where the loader tries to load the dynamic version even when it doesn't exist.
>
>
> Also, is there some way to tell, either by looking at the build configuration files or with diagnostic output from the loader, exactly which files the loader is trying to load?
Hi Jonathan,
At link time, the linker prefers dylibs to static libe, so if both are present on the library search path and they have identical names, it will pick the dylib. If there is not a dylib present, then the static lib will be chosen. From what you're seeing, I assume this is the case for you. At run time, however, the loader looks in certain places for dylibs, which is probably why it's not finding it.
If you run the app from a console window and you define DYLD_LIBRARY_PATH in that window so it points to the direcory where the dylib is located, then it will find it. From the Apple developer docs:
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 DYLD_FALLBACK_FRAMEWORK_PATH
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 executable
cutable is linked against.
Simple ways to make sure it gets the static library:
1) Separate the libraries into different directories. Only put the directory that contains the static libs on the library search path.
2) Rename the dylibs
Of course, these assume that you have control over the library names and locations. If you just got a prebuilt set of libs, or you installed them in the standard system location, then that's harder to do.
You can link against a specific library by just putting the full path to it, including its full name and extension into one line of the Other Linker Flags setting. I have only done this with static libraries, and never where there was both a static and dynamic lib in the directory, but I would expect it to work. You are telling the linker that you want this specific library, not to search for a library. That's probably the most straightforward way to do it for you. To do this, open the build settings for the target, make sure the "Show" dropdown says "All Settings" and enter "other l" into the search box. That will limit the display and one of them will be "Other Linker Flags". Double click on the right side, which brings up the editing sheet. Click on the plus sign to add a line. Open the Finder and navigate to the library that you want to link. Drag the library name to the new open line in the Xcode edit sheet and it will fill in the path. Or, if you need to use your build settings, or need a relative path within your source code tree, just edit the box directly. Relative paths should be relative to $(SRCROOT).
Oh, and I don't know of a way to tell what the linker did. You can see the command, but if it's loaded up with -Lpath and -lsomelib parameters, then it all depends on how the searches turn out.
- Rush
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden