Back: Module Access Functions
Forward: A Simple GNU/Linux Module Loader
FastBack: A Simple GNU/Linux Module Loader
Up: Dynamic Loading
FastForward: Using GNU libltdl
Top: Autoconf, Automake, and Libtool
Contents: Table of Contents
Index: Index
About: About this document

17.3 Finding a Module

When you are writing a program that will load dynamic modules, a major stumbling block is writing the code to find the modules you wish to load. If you are worried about portability (which you must be, or you wouldn't be reading this book!), you can't rely on the default search algorithm of the vendor dlopen function, since it varies from implementation to implementation. You can't even rely on the name of the module, since the module suffix will vary according to the conventions of the target host (though you could insist on a particular suffix for modules you are willing to load).

Unfortunately, this means that you will need to implement your own searching algorithm and always use an absolute pathname when you call dlopen. A widely adopted mechanism is to look for each module in directories listed in an environment variable specific to your application, allowing your users to inform the application of the location of any modules they have written. If a suitable module is not yet found, the application would then default to looking in a list of standard locations -- say, in a subdirectory of the user's home directory, and finally a subdirectory of the application installation tree. For application `foo', you might use `/usr/lib/foo/' -- that is, `$(pkglibdir)/' if you are using Automake.

This algorithm can be further improved:

  • If you try different module suffixes to the named module for every directory in the search path, which will avoid locking your code into a subset of machines that use the otherwise hardcoded module suffix. With this in place you could ask the module loader for module `foomodule', and if it was not found in the first search directory, the module loader could try `', `' and `foomodule.dll' before moving on to the next directory.

  • You might also provide command line options to your application which will preload modules before starting the program proper or to modify the module search path. For example, GNU M4, version 1.5, will have the following dynamic loading options:

$ m4 --help
Usage: m4 [OPTION]... [FILE]...
Dynamic loading features:
  -M, --module-directory=DIRECTORY  add DIRECTORY to the search path
  -m, --load-module=MODULE          load dynamic MODULE from M4MODPATH
Report bugs to <[email protected]>.

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