Rev 40559 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
Writing packages for R======================See the Writing R Extensions manual for a full description of howto write a package.Building from a source-code library under Windows=================================================Instructions for installing the toolset and building packages using thestandard methods are in the `R Installation and Administration' manual(which is available in various formats as R-admin.* in the doc/manualdirectory).This file contains instructions for non-standard situations:- Using Microsoft Visual C++- Using Borland C++- Using other compilers and languages- Cross-building packages on LinuxAll the examples given here have worked at some point, but they are nottested for every R release.Using Visual C++================You may if you prefer use Visual C++ to make the DLLs (unless they useFortran source!). The notes here were tested with VC++6.First build the import library Rdll.lib bymake R.explib /def:R.exp /out:Rdll.libor, depending on your version of VC++link /lib /def:R.exp /machine:x86 /out:Rdll.lib(Another, less reliable, way to make R.exp is to use pexports.exe frommingw-utils.)Then you can compile the objects and build the DLL bycl /MT /Ox /D "WIN32" /c *.clink /dll /def:mypkg.def /out:mypkg.dll *.obj Rdll.libwhere you will need to create the .def file by hand listing the entrypoints to be exported. (If there are just a few you can use /exportflags instead.) If the C sources use R header files you will need toarrange for these to be searched, perhaps by including in the cl line/I ..\..\..\includeIf you build a debug version of the DLL in the developmentenvironment, you can debug the DLL code there just by setting theexecutable to be debugged as the full path to the R front-end.Extra care is needed when referencing variables (rather thanfunctions) exported from R.dll. These must be declared__declspec(dllimport) (as in R's own header files).VC++6 lacks some standard functions such as isnan and isfinite. To useR's macros you will need#undef ISNAN#define ISNAN(x) _isnan(x)#undef R_FINITE#define R_FINITE(x) _finite(x)for example. Even then, we have seen examples of IEC60559 arithmeticbeing performed incorrectly.Using Borland C++=================Borland C++5.5 is available as a free download fromhttp://www.borland.com/bcppbuilder/freecompiler/ and as part of C++Builder 5. The following will make convolve.dll from convolve.c (flag-6 optimizes for a Pentium Pro/II/III/4, and -u- removes extra underscores)bcc32 -u- -6 -O2 -WDE convolve.cYou can build an import library for R.dll bymake R.expimplib R.lib R.expand then add R.lib to the bcc32 command line, for example (fromVenables & Ripley's `S Programming')bcc32 -u- -6 -O2 -WDE -I\R\R-2.3.0\src\include VCrndR.c R.libWe believe that when referencing variables (rather than functions)exported from R.dll these must be declared __declspec(dllimport) justas for VC++.Using other compilers and languages===================================To use C++ see the section in the R for Windows FAQ. You can includeC++ code in packages and the supplied Makefiles will compile with g++and link the DLL using g++ (and hence link against libstc++.a). Useof C++ I/O may or may not work, and has been seen to crash R.To use F90 or F95, see `Writing R Extensions'.For other compilers you will need to arrange to produce a DLL withcdecl (also known as _cdecl or __cdecl) linkage. The mingw port (andVC++) uses no `name mangling' at all, so that if for example yourcompiler adds leading or trailing underscores you will need to use thetransformed symbol in the call to .C in your R code. Many compilerscan produce cdecl DLLs by a suitable choice of flags, but if yourscannot you may need to write some `glue' code in C to interface to theDLL.If you use .Fortran this appends an underscore and does no caseconversion at all to the symbol name. It is normally best to use.C with compilers other than g77 and map the name manually if necessary.Care is needed in passing character strings to and from a DLL by .C:they must be equivalent to the C type char** and null-terminated. Noteven the mingw g77 Fortran uses null-terminated strings.WARNING: DLLs made with some compilers reset the FPU in their startupcode (Delphi has been one), and this will cause operations such as0./0. to crash R. You can re-set the FPU to the correct values by acall to the C entry point Rwin_fpset().For some further details and Delphi examples seehttp://www.stats.uwo.ca/faculty/murdoch/software/compilingDLLs/Cross-building packages on Linux================================It is straightforward to build a package on a ix86 (or even x86_64)Linux system, except to build .chm files. You will need aninstallation of R for Windows, either copied from a Windows system orcross-compiled, and a Linux R installation of exactly the same version,built without using subarchitectures.First you need to set up the cross-compilers and tools (see the `RInstallation and Administration' manual) and have them in your path.We will assume that your Linux installation has Perl5, unzip and zip.Edit MkRules to set BUILD=CROSS and the appropriate paths (includingHEADER and R_EXE) as needed.Then packages can be made as natively, for example bycd .../src/gnuwin32make PKGDIR=/mysources RLIB=/R/win/library pkg-mypkgmake PKGDIR=/mysources RLIB=/R/win/library lazyload-mypkgcd /R/win/libraryzip -r9X /dest/mypkg.zip mypkg(Rcmd is a Windows executable, so cannot be used.)If your package has no compiled code it is possible that zipping upthe installed package on Linux will produce an installable package onWindows. (It has always worked for us, but failures have been reported.)People have reported being able to build .chm files with WINE providedthat the Windows itss.dll is used: there is not support for this inthe distributed Makefiles.Feedback========Please send comments and bug reports toR-windows@r-project.org