Go to the first, previous, next, last section, table of contents.
It can sometimes be confusing to discuss dynamic linking, because the term is used to refer to two different concepts:
dlopen
,(7) which load
arbitrary, user-specified modules at runtime. This type of dynamic
linking is explicitly controlled by the application.
To mitigate confusion, this manual refers to the second type of dynamic linking as dlopening a module.
The main benefit to dlopening object modules is the ability to access compiled object code to extend your program, rather than using an interpreted language. In fact, dlopen calls are frequently used in language interpreters to provide an efficient way to extend the language.
As of version 1.4.2, libtool provides support for dlopened modules. However, you should indicate that your package is willing to use such support, by using the macro `AC_LIBTOOL_DLOPEN' in `configure.in'. If this macro is not used (or it is used after `AC_PROG_LIBTOOL'), libtool will assume no dlopening mechanism is available, and will try to simulate it.
This chapter discusses how you as a dlopen application developer might use libtool to generate dlopen-accessible modules.
On some operating systems, a program symbol must be specially declared
in order to be dynamically resolved with the dlsym
(or
equivalent) function.
Libtool provides the `-export-dynamic' and `-module' link flags (see section Link mode), which do this declaration. You need to use these flags if you are linking an application program that dlopens other modules or a libtool library that will also be dlopened.
For example, if we wanted to build a shared library, `libhello', that would later be dlopened by an application, we would add `-module' to the other link flags:
burger$ libtool gcc -module -o libhello.la foo.lo \ hello.lo -rpath /usr/local/lib -lm burger$
If symbols from your executable are needed to satisfy unresolved references in a library you want to dlopen you will have to use the flag `-export-dynamic'. You should use `-export-dynamic' while linking the executable that calls dlopen:
burger$ libtool gcc -export-dynamic -o hell-dlopener main.o burger$
Libtool provides special support for dlopening libtool object and
libtool library files, so that their symbols can be resolved even
on platforms without any dlopen
and dlsym
functions.
Consider the following alternative ways of loading code into your program, in order of increasing "laziness":
Libtool emulates `-dlopen' on static platforms by linking objects into the program at compile time, and creating data structures that represent the program's symbol table.
In order to use this feature, you must declare the objects you want your application to dlopen by using the `-dlopen' or `-dlpreopen' flags when you link your program (see section Link mode).
"fprintf"
. The address attribute is a
generic pointer to the appropriate object, such as &fprintf
.
0
, followed by all symbols exported from this file.
For the executable itself the special name @PROGRAM@ is used.
The last element has a name and address of 0
.
Some compilers may allow identifiers which are not valid in ANSI C, such as dollar signs. Libtool only recognizes valid ANSI C symbols (an initial ASCII letter or underscore, followed by zero or more ASCII letters, digits, and underscores), so non-ANSI symbols will not appear in lt_preloaded_symbols.
After a library has been linked with `-module', it can be dlopened. Unfortunately, because of the variation in library names, your package needs to determine the correct file to dlopen.
The most straightforward and flexible implementation is to determine the name at runtime, by finding the installed `.la' file, and searching it for the following lines:
# The name that we can dlopen
.
dlname='dlname'
If dlname is empty, then the library cannot be dlopened. Otherwise, it gives the dlname of the library. So, if the library was installed as `/usr/local/lib/libhello.la', and the dlname was `libhello.so.3', then `/usr/local/lib/libhello.so.3' should be dlopened.
If your program uses this approach, then it should search the
directories listed in the LD_LIBRARY_PATH
(8) environment variable, as well as
the directory where libraries will eventually be installed. Searching
this variable (or equivalent) will guarantee that your program can find
its dlopened modules, even before installation, provided you have linked
them using libtool.
The following problems are not solved by using libtool's dlopen support:
dlopen
family, which do package-specific tricks when dlopening
is unsupported or not available on a given platform.
dlopen
family of functions. Some platforms do not even use the same function
names (notably HP-UX, with its shl_load
family).
dlopen
.
Go to the first, previous, next, last section, table of contents.