| 25857 |
murdoch |
1 |
Writing packages for R
|
|
|
2 |
======================
|
|
|
3 |
|
|
|
4 |
See the Writing R Extensions manual for a full description of how
|
|
|
5 |
to write a package.
|
|
|
6 |
|
|
|
7 |
|
| 9469 |
ripley |
8 |
Building from a source-code library under Windows
|
|
|
9 |
=================================================
|
|
|
10 |
|
| 33027 |
murdoch |
11 |
Instructions for installing the toolset and building packages using the
|
|
|
12 |
standard methods are in the `R Installation and Administration' manual
|
|
|
13 |
(which is available in various formats as R-admin.* in the doc/manual
|
|
|
14 |
directory).
|
| 10545 |
ripley |
15 |
|
| 33027 |
murdoch |
16 |
This file contains instructions for non-standard situations:
|
| 21130 |
ripley |
17 |
|
| 33027 |
murdoch |
18 |
- Using Microsoft Visual C++
|
|
|
19 |
- Using Borland C++
|
|
|
20 |
- Using other compilers and languages
|
| 35733 |
ripley |
21 |
|
| 53374 |
ripley |
22 |
All the examples given here have worked at some point with 32-bit R,
|
|
|
23 |
but are mainly of historical interest.
|
| 37416 |
ripley |
24 |
|
|
|
25 |
|
| 9469 |
ripley |
26 |
Using Visual C++
|
|
|
27 |
================
|
|
|
28 |
|
|
|
29 |
You may if you prefer use Visual C++ to make the DLLs (unless they use
|
| 39939 |
ripley |
30 |
Fortran source!). The notes here were tested with VC++6.
|
| 9469 |
ripley |
31 |
|
| 43404 |
ripley |
32 |
First build the import library Rdll.lib by (from the sources)
|
| 39939 |
ripley |
33 |
|
| 38794 |
ripley |
34 |
make R.exp
|
| 40131 |
ripley |
35 |
|
| 9469 |
ripley |
36 |
lib /def:R.exp /out:Rdll.lib
|
| 40131 |
ripley |
37 |
or, depending on your version of VC++
|
|
|
38 |
link /lib /def:R.exp /machine:x86 /out:Rdll.lib
|
| 9469 |
ripley |
39 |
|
| 43404 |
ripley |
40 |
Another way to make R.exp is to use pexports.exe from mingw-utils,
|
|
|
41 |
e.g. pexports R.dll > R.exp.
|
| 39939 |
ripley |
42 |
|
| 9469 |
ripley |
43 |
Then you can compile the objects and build the DLL by
|
|
|
44 |
|
|
|
45 |
cl /MT /Ox /D "WIN32" /c *.c
|
|
|
46 |
link /dll /def:mypkg.def /out:mypkg.dll *.obj Rdll.lib
|
|
|
47 |
|
|
|
48 |
where you will need to create the .def file by hand listing the entry
|
|
|
49 |
points to be exported. (If there are just a few you can use /export
|
|
|
50 |
flags instead.) If the C sources use R header files you will need to
|
|
|
51 |
arrange for these to be searched, perhaps by including in the cl line
|
|
|
52 |
|
|
|
53 |
/I ..\..\..\include
|
|
|
54 |
|
|
|
55 |
If you build a debug version of the DLL in the development
|
|
|
56 |
environment, you can debug the DLL code there just by setting the
|
|
|
57 |
executable to be debugged as the full path to the R front-end.
|
|
|
58 |
|
| 9940 |
pd |
59 |
Extra care is needed when referencing variables (rather than
|
|
|
60 |
functions) exported from R.dll. These must be declared
|
| 43205 |
ripley |
61 |
__declspec(dllimport) (as in R's own header files).
|
| 9469 |
ripley |
62 |
|
| 43205 |
ripley |
63 |
For some applications making use of the R API headers you will need to
|
|
|
64 |
build import libraries for Rblas.dll or graphapp.dll and link against
|
|
|
65 |
those.
|
|
|
66 |
|
| 40131 |
ripley |
67 |
VC++6 lacks some standard functions such as isnan and isfinite. To use
|
| 29552 |
ripley |
68 |
R's macros you will need
|
| 9940 |
pd |
69 |
|
| 29552 |
ripley |
70 |
#undef ISNAN
|
|
|
71 |
#define ISNAN(x) _isnan(x)
|
|
|
72 |
#undef R_FINITE
|
|
|
73 |
#define R_FINITE(x) _finite(x)
|
|
|
74 |
|
|
|
75 |
for example. Even then, we have seen examples of IEC60559 arithmetic
|
|
|
76 |
being performed incorrectly.
|
|
|
77 |
|
|
|
78 |
|
| 9469 |
ripley |
79 |
Using Borland C++
|
|
|
80 |
=================
|
|
|
81 |
|
|
|
82 |
Borland C++5.5 is available as a free download from
|
|
|
83 |
http://www.borland.com/bcppbuilder/freecompiler/ and as part of C++
|
|
|
84 |
Builder 5. The following will make convolve.dll from convolve.c (flag
|
| 17319 |
ripley |
85 |
-6 optimizes for a Pentium Pro/II/III/4, and -u- removes extra underscores)
|
| 9469 |
ripley |
86 |
|
|
|
87 |
bcc32 -u- -6 -O2 -WDE convolve.c
|
|
|
88 |
|
| 38768 |
ripley |
89 |
You can build an import library for R.dll by
|
| 9469 |
ripley |
90 |
|
| 38794 |
ripley |
91 |
make R.exp
|
|
|
92 |
implib R.lib R.exp
|
| 9469 |
ripley |
93 |
|
|
|
94 |
and then add R.lib to the bcc32 command line, for example (from
|
| 9940 |
pd |
95 |
Venables & Ripley's `S Programming')
|
| 9469 |
ripley |
96 |
|
| 36544 |
ripley |
97 |
bcc32 -u- -6 -O2 -WDE -I\R\R-2.3.0\src\include VCrndR.c R.lib
|
| 9469 |
ripley |
98 |
|
| 10374 |
ripley |
99 |
We believe that when referencing variables (rather than functions)
|
|
|
100 |
exported from R.dll these must be declared __declspec(dllimport) just
|
|
|
101 |
as for VC++.
|
| 9469 |
ripley |
102 |
|
| 9940 |
pd |
103 |
|
| 9469 |
ripley |
104 |
Using other compilers and languages
|
|
|
105 |
===================================
|
|
|
106 |
|
| 9940 |
pd |
107 |
To use C++ see the section in the R for Windows FAQ. You can include
|
| 14462 |
ripley |
108 |
C++ code in packages and the supplied Makefiles will compile with g++
|
| 53374 |
ripley |
109 |
and link the DLL using g++ (and hence link against libstdc++). Use of
|
|
|
110 |
C++ I/O may or may not work, and has been seen to crash R.
|
| 9469 |
ripley |
111 |
|
| 36544 |
ripley |
112 |
To use F90 or F95, see `Writing R Extensions'.
|
|
|
113 |
|
| 9469 |
ripley |
114 |
For other compilers you will need to arrange to produce a DLL with
|
| 51157 |
ripley |
115 |
cdecl (also known as _cdecl or __cdecl) linkage. The MinGW port (and
|
| 14462 |
ripley |
116 |
VC++) uses no `name mangling' at all, so that if for example your
|
| 9469 |
ripley |
117 |
compiler adds leading or trailing underscores you will need to use the
|
|
|
118 |
transformed symbol in the call to .C in your R code. Many compilers
|
|
|
119 |
can produce cdecl DLLs by a suitable choice of flags, but if yours
|
|
|
120 |
cannot you may need to write some `glue' code in C to interface to the
|
|
|
121 |
DLL.
|
|
|
122 |
|
|
|
123 |
If you use .Fortran this appends an underscore and does no case
|
| 53374 |
ripley |
124 |
conversion at all to the symbol name. It is normally best to use .C
|
|
|
125 |
with compilers other than gfortran and map the name manually if
|
|
|
126 |
necessary.
|
| 9469 |
ripley |
127 |
|
|
|
128 |
Care is needed in passing character strings to and from a DLL by .C:
|
|
|
129 |
they must be equivalent to the C type char** and null-terminated. Not
|
| 51157 |
ripley |
130 |
even the MinGW g77 Fortran used null-terminated strings. (g77 was in
|
|
|
131 |
GCC 3.x.y, superseded by gfortran.)
|
| 9469 |
ripley |
132 |
|
| 10374 |
ripley |
133 |
WARNING: DLLs made with some compilers reset the FPU in their startup
|
| 73403 |
murdoch |
134 |
code and this will cause operations such as
|
| 14462 |
ripley |
135 |
0./0. to crash R. You can re-set the FPU to the correct values by a
|
|
|
136 |
call to the C entry point Rwin_fpset().
|
| 9469 |
ripley |
137 |
|