How do I create a dynamic library (dylib) with Xcode? How do I create a dynamic library (dylib) with Xcode? xcode xcode

How do I create a dynamic library (dylib) with Xcode?


Dynamic linking on Mac OS X, a tiny example

Steps:

  1. create a library libmylib.dylib containing mymod.o
  2. compile and link a "callmymod" which calls it
  3. call mymod from callmymod, using DYLD_LIBRARY_PATH and DYLD_PRINT_LIBRARIES

Problem: you "just" want to create a library for other modules to use.However there's a daunting pile of programs -- gcc, ld, macosx libtool, dyld --with zillions of options, some well-rotted compost, and differences between MacOSX and Linux.There are tons of man pages (I count 7679 + 1358 + 228 + 226 lines in 10.4.11 ppc)but not much in the way of examples, or programs with a "tell me what you're doing" mode.

(The most important thing in understanding is to make a simplifiedOVERVIEW for yourself: draw some pictures, run some small examples,explain it to someone else).

Background: apple OverviewOfDynamicLibraries,Wikipedia Dynamic_library


Step 1, create libmylib.dylib --

mymod.c:    #include <stdio.h>    void mymod( int x )    {        printf( "mymod: %d\n", x );    }gcc -c mymod.c  # -> mymod.ogcc -dynamiclib -current_version 1.0  mymod.o  -o libmylib.dylib    # calls libtool with many options -- see man libtool    # -compatibility_version is used by dyld, see also cmpdylibfile libmylib.dylib  # Mach-O dynamically linked shared library ppcotool -L libmylib.dylib  # versions, refs /usr/lib/libgcc_s.1.dylib

Step 2, compile and link callmymod --

callmymod.c:    extern void mymod( int x );    int main( int argc, char** argv )    {        mymod( 42 );    }gcc -c callmymod.cgcc -v callmymod.o ./libmylib.dylib -o callmymod    # == gcc callmymod.o -dynamic -L. -lmylibotool -L callmymod  # refs libmylib.dylibnm -gpv callmymod  # U undef _mymod: just a reference, not mymod itself

Step 3, run callmymod linking to libmylib.dylib --

export DYLD_PRINT_LIBRARIES=1  # see what dyld does, for ALL programs./callmymod    dyld: loaded: libmylib.dylib ...    mymod: 42mv libmylib.dylib /tmpexport DYLD_LIBRARY_PATH=/tmp  # dir:dir:..../callmymod    dyld: loaded: /tmp/libmylib.dylib ...    mymod: 42unset DYLD_PRINT_LIBRARIESunset DYLD_LIBRARY_PATH

That ends one tiny example; hope it helps understand the steps.
(If you do this a lot, see GNU Libtoolwhich is glibtool on macs,and SCons.)


You probably need to ensure that the dynamic library you build has an exported symbols file that lists what should be exported from the library. It's just a flat list of the symbols, one per line, to export.

Also, when your dynamic library is built, it gets an install name embedded within it which is, by default, the path at which it is built. Subsequently anything that links against it will look for it at the specified path first and only afterwards search a (small) set of default paths described under DYLD_FALLBACK_LIBRARY_PATH in the dyld(1) man page.

If you're going to put this library next to your executables, you should adjust its install name to reference that. Just doing a Google search for "install name" should turn up a ton of information on doing that.


Unfortunately, in my experience Apple's documentation is antiquated, redundant and missing a LOT of common information that you would normally need.

I wrote a bunch of stuff on this on my website where I had to get FMOD (Sound API) to work with my cross platform game that we developed at uni. Its a weird process and I'm surprised that Apple do not add more info on their developer docs.

Unfortunately, as "evil" as Microsoft are, they actually do a much better job of looking after their devs with documentation ( this is coming from an Apple evangelist ).

I think basically, what you are not doing is AFTER you have compiled your .app Bundle. You then need to run a command on the executable binary /MyApp.app/contents/MacOS/MyApp in order to change where the executable looks for its library file. You must create a new build phase that can run a script. I won't explain this process again, I have already done it in depth here:

http://brockwoolf.com/blog/how-to-use-dynamic-libraries-in-xcode-31-using-fmod

Hope this helps.