Rev 85953 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/Foreign.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2022 R Core Team% Distributed under GPL 2 or later\name{Foreign}\alias{Foreign}\alias{.C}\alias{.Fortran}\title{Foreign Function Interface}\description{Functions to make calls to compiled code that has been loaded into \R.}\usage{.C(.NAME, \dots, NAOK = FALSE, DUP = TRUE, PACKAGE, ENCODING).Fortran(.NAME, \dots, NAOK = FALSE, DUP = TRUE, PACKAGE, ENCODING)}\arguments{\item{.NAME}{a character string giving the name of a C function orFortran subroutine, or an object of class\code{"\link{NativeSymbolInfo}"}, \code{"\link{RegisteredNativeSymbol}"}or \code{"\link{NativeSymbol}"} referring to such a name.}\item{\dots}{arguments to be passed to the foreign function. Up to 65.}\item{NAOK}{if \code{TRUE} then any \code{\link{NA}} or\code{\link{NaN}} or \code{\link{Inf}} values in the arguments arepassed on to the foreign function. If \code{FALSE}, the presence of\code{NA} or \code{NaN} or \code{Inf} values is regarded as an error.}\item{PACKAGE}{if supplied, confine the search for a character string\code{.NAME} to the DLL given by this argument (plus theconventional extension, \file{.so}, \file{.dll}, \dots).This is intended to add safety for packages, which can ensure byusing this argument that no other package can override their externalsymbols, and also speeds up the search (see \sQuote{Note}).}\item{DUP, ENCODING}{For back-compatibility, accepted but ignored.}}\details{These functions can be used to make calls to compiled C and Fortrancode. Later interfaces are \code{\link{.Call}} and\code{\link{.External}} which are more flexible and have betterperformance.These functions are \link{primitive}, and \code{.NAME} is alwaysmatched to the first argument supplied (which should not be named).The other named arguments follow \code{\dots} and so cannot beabbreviated. For clarity, should avoid using names in the argumentspassed to \code{\dots} that match or partially match \code{.NAME}.}\value{A list similar to the \code{\dots} list of arguments passed in(including any names given to the arguments), but reflecting anychanges made by the C or Fortran code.}\section{Argument types}{The mapping of the types of \R arguments to C or Fortran arguments is\tabular{lll}{\R \tab C \tab Fortran\cr\code{integer} \tab \code{int *} \tab \code{integer}\cr\code{numeric} \tab \code{double *} \tab \code{double precision}\cr-- or -- \tab \code{float *} \tab \code{real}\cr\code{complex} \tab \code{Rcomplex *} \tab \code{double complex}\cr\code{logical} \tab \code{int *} \tab \code{integer} \cr\code{character}\tab \code{char **} \tab [see below]\cr\code{raw} \tab \code{unsigned char *}\tab not allowed\cr\code{list} \tab \code{SEXP *} \tab not allowed\crother \tab \code{SEXP} \tab not allowed\cr}\emph{Note:} The C types corresponding to \code{integer} and\code{logical} are \code{int}, not \code{long} as in S. Thisdifference matters on most 64-bit platforms, where \code{int} is32-bit and \code{long} is 64-bit (but not on 64-bit Windows).\emph{Note:} The Fortran type corresponding to \code{logical} is\code{integer}, not \code{logical}: the difference matters on someFortran compilers.%% gfortran, at least >= 9, uses int_least32_t for LOGICAL.Numeric vectors in \R will be passed as type \code{double *} to C(and as \code{double precision} to Fortran) unless the argument hasattribute \code{Csingle} set to \code{TRUE} (use\code{\link{as.single}} or \code{\link{single}}). This mechanism isonly intended to be used to facilitate the interfacing of existing Cand Fortran code.The C type \code{Rcomplex} is defined in \file{Complex.h} as a\code{typedef struct {double r; double i;}}. It may or may not beequivalent to the C99 \code{double complex} type, depending on thecompiler used.Logical values are sent as \code{0} (\code{FALSE}), \code{1}(\code{TRUE}) or \code{INT_MIN = -2147483648} (\code{NA}, but only if\code{NAOK = TRUE}), and the compiled code should return one of thesethree values: however non-zero values other than \code{INT_MIN} aremapped to \code{TRUE}.Missing (\code{NA}) string values are passed to \code{.C} as the string"NA". As the C \code{char} type can represent all possible bit patternsthere appears to be no way to distinguish missing strings from thestring \code{"NA"}. If this distinction is important use \code{\link{.Call}}.Using a character string with \code{.Fortran} is deprecated and willgive a warning. It passes the first (only) character string of acharacter vector as a C character array to Fortran: that may be usableas \code{character*255} if its true length is passed separately. Onlyup to 255 characters of the string are passed back. (How well thisworks, and even if it works at all, depends on the C and Fortrancompilers and the platform.)Lists, functions or other \R objects can (for historical reasons) bepassed to \code{.C}, but the \code{\link{.Call}} interface is muchpreferred. All inputs apart from atomic vectors should be regarded asread-only, and all apart from vectors (including lists), functions andenvironments are now deprecated.}\section{Fortran symbol names}{All Fortran compilers known to be usable to compile \R map symbol namesto lower case, and so does \code{.Fortran}.Symbol names containing underscores are not valid Fortran 77 (althoughthey are valid in Fortran 9x). Many Fortran 77 compilers will allowthem but may translate them in a different way to names not containingunderscores. Such names will often work with \code{.Fortran} (sincehow they are translated is detected when \R is built and theinformation used by \code{.Fortran}), but portable code should not useFortran names containing underscores.Use \code{.Fortran} with care for compiled Fortran 9x code: it may notwork if the Fortran 9x compiler used differs from the Fortran compilerused when configuring \R, especially if the subroutine name is notlower-case or includes an underscore. The most portable way to callFortran 9x code from \R is to use \code{.C} and the Fortran 2003module \code{iso_c_binding} to provide a C interface to the Fortrancode.}\section{Copying of arguments}{Character vectors are copied before calling the compiled code and tocollect the results. For other atomic vectors the argument is copiedbefore calling the compiled code if it is otherwise used in thecalling code.Non-atomic-vector objects are read-only to the C code and are nevercopied.This behaviour can be changed by setting\code{\link{options}(CBoundsCheck = TRUE)}. In that case raw,logical, integer, double and complex vector arguments are copied bothbefore and after calling the compiled code. The first copy made isextended at each end by guard bytes, and on return it is checked thatthese are unaltered. For \code{.C}, each element of a charactervector uses guard bytes.}\note{If one of these functions is to be used frequently, do specify\code{PACKAGE} (to confine the search to a single DLL) or pass\code{.NAME} as one of the native symbol objects. Searching forsymbols can take a long time, especially when many namespaces are loaded.You may see \code{PACKAGE = "base"} for symbols linked into \R. Donot use this in your own code: such symbols are not part of the APIand may be changed without warning.}\references{Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\emph{The New S Language}.Wadsworth & Brooks/Cole.}\seealso{\code{\link{dyn.load}}, \code{\link{.Call}}.The \sQuote{Writing R Extensions} manual.}\keyword{programming}