Rev 82735 | 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.The following content is outdated, but kept here for reference.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 languagesAll the examples given here have worked at some point with 32-bit R,but are mainly of historical interest.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 by (from the sources)make R.explib /def:R.exp /out:Rdll.libor, depending on your version of VC++link /lib /def:R.exp /machine:x86 /out:Rdll.libAnother way to make R.exp is to use pexports.exe from mingw-utils,e.g. pexports R.dll > R.exp.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).For some applications making use of the R API headers you will need tobuild import libraries for Rblas.dll or graphapp.dll and link againstthose.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 libstdc++). Use ofC++ 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 .Cwith compilers other than gfortran and map the name manually ifnecessary.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 used null-terminated strings. (g77 was inGCC 3.x.y, superseded by gfortran.)WARNING: DLLs made with some compilers reset the FPU in their startupcode 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().