Rev 41061 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{dyn.load}\alias{dyn.load}\alias{dyn.unload}\alias{is.loaded}\title{Foreign Function Interface}\description{Load or unload shared libraries, and test whether a C function orFortran subroutine is available.}\usage{dyn.load(x, local = TRUE, now = TRUE)dyn.unload(x)is.loaded(symbol, PACKAGE = "", type = "")}\arguments{\item{x}{a character string giving the pathname to a shared library orDLL.}\item{local}{a logical value controlling whether the symbols in theshared library are stored in their own local table and not sharedacross shared libraries,or added to the global symbol table. Whether this has any effect issystem-dependent.#ifdef windowsIt is ignored on Windows.#endif}\item{now}{a logical controlling whether all symbols are resolved (andrelocated) immediately the library is loaded or deferred until theyare used. This control is useful for developers testing whether alibrary is complete and has all the necessary symbols, and for usersto ignore missing symbols. Whether this has any effect is system-dependent.#ifdef windowsIt is ignored on Windows.#endif}\item{symbol}{a character string giving a symbol name.}\item{PACKAGE}{if supplied, confine the search for the \code{name} tothe DLL given by this argument (plus the conventional extension,\code{.so}, \code{.sl}, \code{.dll}, \dots). This is intended toadd safety for packages, which can ensure by using this argumentthat no other package can override their external symbols. Use\code{PACKAGE="base"} for symbols linked in to \R. This is used in thesame way as in \code{\link{.C}}, \code{\link{.Call}},\code{\link{.Fortran}} and \code{\link{.External}} functions}\item{type}{The type of symbol to look for: can be any (\code{""}, thedefault), \code{"Fortran"}, \code{"Call"} or \code{"External"}.}}\details{See\sQuote{See Also} and the \emph{Writing R Extensions} and\emph{R Installation and Administration} manuals for how tocreate and installa suitable shared library. Note that unlike some versions ofS-PLUS, \code{dyn.load} does not load an object (\file{.o}) file but ashared library or DLL.Unfortunately a very few platforms (Compaq Tru64) do not handle the\code{PACKAGE} argument correctly, and may incorrectly find symbolslinked into \R.The additional arguments to \code{dyn.load} mirror the differentaspects of the mode argument to the dlopen() routine on UNIXsystems. They are available so that users can exercise greater controlover the loading process for an individual library. In general, thedefaults values are appropriate and you should override them only ifthere is good reason and you understand the implications.#ifdef unixThe \code{local} argument allows one to control whether the symbols inthe DLL being attached are visible to other DLLs. While maintainingthe symbols in their own name space is good practice, the ability toshare symbols across related \dQuote{chapters} is useful in manycases. Additionally, on certain platforms and versions of anoperating system, certain libraries must have their symbols loadedglobally to successfully resolve all symbols.One should be careful of the potential side-effect of using lazyloading via the \code{now} argument as \code{FALSE}. If a routine iscalled that has a missing symbol, the process will terminateimmediately and unsaved session variables will be lost. The intendeduse is for library developers to call specify a value \code{TRUE} tocheck that all symbols are actually resolved and for regular users toall with \code{FALSE} so that missing symbols can be ignored and theavailable ones can be called.The initial motivation for adding these was to avoid such terminationin the \code{_init()} routines of the Java virtual machine library.However, symbols loaded locally may not be (read probably) availableto other DLLs. Those added to the global table are available to allother elements of the application and so can be shared across twodifferent DLLs.Some systems do not provide (explicit) support for local/global andlazy/eager symbol resolution. This can be the source of subtle bugs.One can arrange to have warning messages emitted when unsupportedoptions are used. This is done by setting either of the options\code{verbose} or \code{warn} to be non-zero via the\code{\link{options}} function. Currently, we know of only 2platforms that do not provide a value for local load (RTLD\_LOCAL).These are IRIX6.4 and unpatched versions of Solaris 2.5.1.There is a short discussion of these additional arguments with someexample code available at\url{http://cm.bell-labs.com/stat/duncan/R/dynload}.#endif#ifdef windowsExternal code must not change the floating point control word, butmany DLLs do so. Common changes are to set it to use 53 bitprecision instead of R's default 64 bit precision, or to unmasksome exceptions. \code{dyn.load} detects such changes,and restores R's control word to its default value of hex 8001F.This may cause the DLL to malfunction; if so, it should be rewrittento save and restore the control word itself. If \code{warn.FPU}is set to \code{TRUE} using the \code{\link{options}} function,a warning will be printed. (If the warning saysthat the control word was changed from some other value than 8001F,please report the circumstances to the Windows maintainers:that probably indicates an internal bug.)#endif}\value{The function \code{dyn.load} is used for its side effect which linksthe specified shared library to the executing \R image. Calls to\code{.C}, \code{.Call}, \code{.Fortran} and \code{.External} canthen be used to execute compiled C functions or Fortran subroutinescontained in the library. The return value of \code{dyn.load} isan object of class \code{DLLInfo}. See \code{\link{getLoadedDLLs}}for information about this class.The function \code{dyn.unload} unlinks the shared library.\code{is.loaded} checks if the symbol name is loaded and henceavailable for use in \code{.C} or \code{.Fortran} or \code{.Call} or\code{.External}: it requires the name you would give to \code{.C} etcand \strong{not} (as in S) that remapped by defunct functions\code{\link{symbol.C}} or \code{\link{symbol.For}}. It will succeedif any one of the four calling functions would succeed in using theentry point unless \code{type} is specified. (See\code{\link{.Fortran}} for how Fortran symbols are mapped.)}\note{The creation of shared libraries and the runtime linking of them intoexecuting programs is very platform dependent. In recent years therehas been some simplification in the process because the C subroutinecall \code{dlopen} has become the standard for doing this under UNIX.Under UNIX \code{dyn.load} uses the \code{dlopen} mechanism and shouldwork on all platforms which support it. On Windows it uses thestandard mechanisms for loading 32-bit DLLs.The original code for loading DLLs in UNIX was provided by HeinerSchwarte. The compatibility code for HP-UX was provided by LukeTierney.}\section{Warning}{Do not use \code{dyn.unload} on a shared object loaded by\code{\link{library.dynam}}: use \code{\link{library.dynam.unload}}.}\references{Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\emph{The New S Language}.Wadsworth \& Brooks/Cole.}\seealso{\code{\link{library.dynam}} to be used inside a package's\code{\link{.First.lib}} initialization.#ifdef unix\code{\link{SHLIB}} for how to create suitable shared objects.#endif#ifdef windows\code{\link{SHLIB}} for how to create suitable DLLs.#endif\code{\link{.C}},\code{\link{.Fortran}},\code{\link{.External}},\code{\link{.Call}}.}\examples{is.loaded("hcass2") #-> probably TRUE, as stats is loadedis.loaded("supsmu") # Fortran entry point in statsis.loaded("supsmu", "stats", "Fortran")is.loaded("PDF", type = "External")}\keyword{interface}