Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Dynamic linking on Mac OS X, a tiny example</p> <p>Steps: </p> <ol> <li>create a library libmylib.dylib containing mymod.o </li> <li>compile and link a "callmymod" which calls it </li> <li>call mymod from callmymod, using DYLD_LIBRARY_PATH and DYLD_PRINT_LIBRARIES</li> </ol> <p>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.</p> <p>(The most important thing in understanding is to make a simplified OVERVIEW for yourself: draw some pictures, run some small examples, explain it to someone else).</p> <p>Background: <a href="https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/OverviewOfDynamicLibraries.html" rel="nofollow noreferrer">apple OverviewOfDynamicLibraries</a>, <a href="http://en.wikipedia.org/wiki/Dynamic_library" rel="nofollow noreferrer">Wikipedia Dynamic_library</a></p> <hr> <p>Step 1, create libmylib.dylib --</p> <pre><code>mymod.c: #include &lt;stdio.h&gt; void mymod( int x ) { printf( "mymod: %d\n", x ); } gcc -c mymod.c # -&gt; mymod.o gcc -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 cmpdylib file libmylib.dylib # Mach-O dynamically linked shared library ppc otool -L libmylib.dylib # versions, refs /usr/lib/libgcc_s.1.dylib </code></pre> <hr> <p>Step 2, compile and link callmymod --</p> <pre><code>callmymod.c: extern void mymod( int x ); int main( int argc, char** argv ) { mymod( 42 ); } gcc -c callmymod.c gcc -v callmymod.o ./libmylib.dylib -o callmymod # == gcc callmymod.o -dynamic -L. -lmylib otool -L callmymod # refs libmylib.dylib nm -gpv callmymod # U undef _mymod: just a reference, not mymod itself </code></pre> <hr> <p>Step 3, run callmymod linking to libmylib.dylib --</p> <pre><code>export DYLD_PRINT_LIBRARIES=1 # see what dyld does, for ALL programs ./callmymod dyld: loaded: libmylib.dylib ... mymod: 42 mv libmylib.dylib /tmp export DYLD_LIBRARY_PATH=/tmp # dir:dir:... ./callmymod dyld: loaded: /tmp/libmylib.dylib ... mymod: 42 unset DYLD_PRINT_LIBRARIES unset DYLD_LIBRARY_PATH </code></pre> <p>That ends one tiny example; hope it helps understand the steps.<br> (If you do this a lot, see <a href="http://en.wikipedia.org/wiki/Libtool" rel="nofollow noreferrer">GNU Libtool</a> which is glibtool on macs, and <a href="http://www.scons.org" rel="nofollow noreferrer">SCons</a>.)<br> cheers<br> -- denis</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload