including dylib in application bundle (carbon)

Discussion in 'Mac Programming' started by printf, Dec 1, 2008.

  1. printf macrumors regular

    Joined:
    Aug 27, 2008
    #1
    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?
     
  2. mkelly11 macrumors newbie

    Joined:
    Dec 1, 2008
    #2
    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.
     
  3. printf thread starter macrumors regular

    Joined:
    Aug 27, 2008
    #3
    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..
     
  4. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #4
    Set the following paths in your .profile file (assuming you are using the bash shell).

    LIBRARY_PATH
    LD_LIBRARY_PATH
    DYLD_LIBRARY_PATH
     
  5. printf thread starter macrumors regular

    Joined:
    Aug 27, 2008
    #5
    .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?
     
  6. Sayer macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #6
    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
     
  7. printf thread starter macrumors regular

    Joined:
    Aug 27, 2008
    #7
    thanks for the info sayer. this is a 3rd party dylib, not my code - do you still recommend that route?
     
  8. Soulstorm macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
  9. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #9
    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.
     
  10. printf thread starter macrumors regular

    Joined:
    Aug 27, 2008
    #10
    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 ;)
     
  11. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #11
    Understandable :)

    How close are you to being done with the port?
     
  12. printf thread starter macrumors regular

    Joined:
    Aug 27, 2008
    #12
    hmmm, i'd wager about 1 week, assuming that leopard compatibility testing goes smoothly. :)
     
  13. printf thread starter macrumors regular

    Joined:
    Aug 27, 2008
    #13
    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
    
     
  14. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #14
    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.
     
  15. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #15
    Yes. Use a Copy Files Build Phase.
     

Share This Page