Autoconf, Automake, and Libtool: libltdl Dynamic Module

| ### 18.2.5 Dynamic Module Writing a module for use with the libltdl based dynamic module loader is no more involved than before: It must provide the correct entry points, as expected by the simple API I designed -- the ``run'` entry point described in 17.4 A Simple GNU/Linux Module Loader. Here is such a module, ``ltdl-module.c'`:
    #include <stdio.h>
    #include <math.h>

    #define run ltdl_module_LTX_run

    int
    run (const char *argument)
    {
      char *end = NULL;
      long number;

      if (!argument || *argument == '\0')
        {
          fprintf (stderr, "error: invalid argument, \"%s\".\n",
                   argument ? argument : "(null)");
          return -1;
        }

      number = strtol (argument, &end, 0);
      if (end && *end != '\0')
        {
          fprintf (stderr, "warning: trailing garbage \"%s\".\n",
                   end);
        }

      printf ("Square root of %s is %f\n", argument, sqrt (number));

      return 0;
    }
To take full advantage of the new module loader, the module itself must be compiled with Libtool. Otherwise dependent libraries will not have been stored when libltdl tries to load the module on an architecture that doesn't load them natively, or which doesn't have shared libraries at all (see section 18.4 dlpreopen Loading).
    $ libtool --mode=compile gcc -c ltdl-module.c
    rm -f .libs/ltdl-module.lo
    gcc -c ltdl-module.c  -fPIC -DPIC -o .libs/ltdl-module.lo
    gcc -c ltdl-module.c -o ltdl-module.o >/dev/null 2>&1
    mv -f .libs/ltdl-module.lo ltdl-module.lo
    $ libtool --mode=link gcc -g -o ltdl-module.la -rpath `pwd` \
    -no-undefined -module -avoid-version ltdl-module.lo -lm
    rm -fr .libs/ltdl-module.la .libs/ltdl-module.* .libs/ltdl-module.*
    gcc -shared  ltdl-module.lo  -lm -lc  -Wl,-soname \
    -Wl,ltdl-module.so -o .libs/ltdl-module.so
    ar cru .libs/ltdl-module.a  ltdl-module.o
    creating ltdl-module.la
    (cd .libs && rm -f ltdl-module.la && ln -s ../ltdl-module.la \
    ltdl-module.la)
You can see from the interaction below that ``ltdl-loader'` does not load the math library, ``libm'`, and that the shared part of the Libtool module, ``ltdl-module'`, does have a reference to it. The pseudo-library also has a note of the ``libm'` dependency so that libltdl will be able to load it even on architectures that can't do it natively:
    $ libtool --mode=execute ldd ltdl-loader
            libltdl.so.0 => /usr/lib/libltdl.so.0 (0x4001a000)
            libdl.so.2 => /lib/libdl.so.2 (0x4001f000)
            libc.so.6 => /lib/libc.so.6 (0x40023000)
            /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
    $ ldd .libs/ltdl-module.so
            libm.so.6 => /lib/libm.so.6 (0x40008000)
            libc.so.6 => /lib/libc.so.6 (0x40025000)
            /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x80000000)
    $ fgrep depend ltdl-module.la
    # Libraries that this one depends upon.
    dependency_libs=' -lm'
This module is now ready to load from ``ltdl-loader'`:
    $ ltdl-loader ltdl-module 9
    Square root of 9 is 3.000000
            => 0

This document was generated by Gary V. Vaughan on February, 8 2006 using texi2html