Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

printf

macrumors regular
Original poster
Aug 27, 2008
105
0
i have a 3rd party dylib that i'm currently loading using dlopen. this dylib resides in the same folder as my .app. however, i'd like to drop this library in my app's bundle.

this is probably very simple, but coming from a win32 background, this is magic to me.

anyone care to walk me through this?
 

mkelly11

macrumors newbie
Dec 1, 2008
7
0
I'm not too familiar with this myself, but here's what I'd do:

1. In Xcode, add the dylib to your project's resources.
2. Under the build configuration, tell the compiler to link against your dylib.
3. Give it a spin.

I'm not too sure if that'll work right, but it's worth a shot.
 

printf

macrumors regular
Original poster
Aug 27, 2008
105
0
hmmm... i tried putting the dylib in both resources, then frameworks (though i suspect the result is no different), but no luck. getting this:

dyld: Library not loaded: /usr/local/lib/mylib.dylib
Referenced from: /Developer/Projects/Project1/build/Release/Project1.app/Contents/MacOS/Project1
Reason: image not found

what do i need to set my library search paths to? this is too unixy for my head..
 

printf

macrumors regular
Original poster
Aug 27, 2008
105
0
.profile file? bash shell? again, you're speaking gibberish to me :)

it's funny, because i've found two other posts on apple's mailing list asking the same question but there are zero responses. yet, remotely related topics on dynamic libraries, or resource bundles are full of engaged discussions. is this just common knowledge that mac owners are 'born' with, so there's nobody interested in documenting or discussing it? :)

i have another related question. what's the difference of adding the dylib to the resources folder in the left-hand pane in xcode as opposed to right-clicking on the .app in finder, and going into Contents/Resources and manually pasting the dylib that way?
 

Sayer

macrumors 6502a
Jan 4, 2002
981
0
Austin, TX
You should repackage the dylib as a framework and link to it like any other framework instead of relying on dlopen and having the dylib loose in the same folder as the app bundle.

There are threads discussing this including a python-based tool to convert a dylib into a framework bundle for you that I posted on another thread.

It's not something we are born with, we figure it out or dig until we find the answers ourselves, thats the Mac way.

Here's the framework conversion scripts/tools: http://att.macrumors.com/attachment.php?attachmentid=144763&d=1227020243
 

printf

macrumors regular
Original poster
Aug 27, 2008
105
0
You should repackage the dylib as a framework and link to it like any other framework instead of relying on dlopen and having the dylib loose in the same folder as the app bundle.

There are threads discussing this including a python-based tool to convert a dylib into a framework bundle for you that I posted on another thread.

It's not something we are born with, we figure it out or dig until we find the answers ourselves, thats the Mac way.

Here's the framework conversion scripts/tools: http://att.macrumors.com/attachment.php?attachmentid=144763&d=1227020243

thanks for the info sayer. this is a 3rd party dylib, not my code - do you still recommend that route?
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Why are you using dlopen? Can't you just link to the dylib, #include the headers and use it like normal? OS X loads the libraries automatically, unlike Win32 and its ugly manual DLL loading mechanism ;)

I mentioned in the other thread you can use install_name_tool. This lets you set the path to a library for a specific binary. You need to make it relative for your DLL. Check out this article which explains how to use it. You just need to add a new Copy Files Build Phase to your target in Xcode to copy the dylib to your .app, if you haven't already.

You can also use 'otool -L <path>' in Terminal to see the libraries a binary is linked against. Very useful for troubleshooting this stuff.
 

printf

macrumors regular
Original poster
Aug 27, 2008
105
0
Why are you using dlopen? Can't you just link to the dylib, #include the headers and use it like normal? OS X loads the libraries automatically, unlike Win32 and its ugly manual DLL loading mechanism ;)

hi kainjow! i'm using dlopen because it's a concept i'm familiar with. i'm about to release a product and it's critical that i understand the technology i've implemented so that i can hope to support it with any level of competence. :) EVENTUALLY, i hope to have a solid enough understanding of all of these unixy linking concepts to be able to implement them and support them, but i'm not there yet, nor do i have the luxury of taking the time to learn those concepts before the product launch.

soooo, i don't know if this is possible or not, but i was just hoping to 'hide' the lylib inside the .app package bundle and, using some cfbundle api or something to locate it and provide the required path, and load the lib using dlopen.

this will also allow me to use the code that was provided with the 3rd party dylib, which is basically thousands of lines of wrapper code that i'd hate to have to sift through to rip out the library loading functions and make 'static lib' type calls ;)
 

printf

macrumors regular
Original poster
Aug 27, 2008
105
0
hmmm, i'd wager about 1 week, assuming that leopard compatibility testing goes smoothly. :)
 

printf

macrumors regular
Original poster
Aug 27, 2008
105
0
i FINALLY got it working! however, i could only do it by going into the package's Contents/Resources folder through FINDER and manually placing the dylib there.

is it possible to somehow add it through xcode, and switch a flag or something so i don't get this?:
Code:
dyld: Library not loaded: /usr/local/lib/mylib.dylib
Referenced from: /Developer/Projects/Project1/build/Release/Project1.app/Contents/MacOS/Project1
Reason: image not found
 

Cromulent

macrumors 604
Oct 2, 2006
6,817
1,102
The Land of Hope and Glory
i FINALLY got it working! however, i could only do it by going into the package's Contents/Resources folder through FINDER and manually placing the dylib there.

is it possible to somehow add it through xcode, and switch a flag or something so i don't get this?:
Code:
dyld: Library not loaded: /usr/local/lib/mylib.dylib
Referenced from: /Developer/Projects/Project1/build/Release/Project1.app/Contents/MacOS/Project1
Reason: image not found

You can set Xcode to run a shell script on build so that it will copy it there for you every time you build your app.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.