[R] DLLs adding in C++ code

Laurence Kell FM CEFAS L.T.Kell at cefas.co.uk
Wed Nov 13 11:47:45 CET 2002

I would like to utilise the power of R by adding my own software as a
package written in C++. As a first step I am trying 
to write a DLL with a simple function that can be called from R. The DLL
contains a C++ routine written and complied in Microsoft Visual C++ Ver 6.0
and is based upon the example in R-Exts

eg I am trying to create the following example function

SEXP out(SEXP x, SEXP y)
   int i, j, nx, ny;
   double tmp;
   SEXP ans;

   nx = length(x); ny = length(y);
   PROTECT(ans = allocMatrix(REALSXP, nx, ny));
   for(i = 0; i < nx; i++) {
    tmp = REAL(x)[i];
    for(j = 0; j < ny; j++)
      REAL(ans)[i + nx*j] = tmp * REAL(y)[j];

I have used tried to generating a lib file that imports the R functions
using the supplied file (from CRAN)

	lib /def:R.exp /out:Rdll.lib

and even used dumpbin and the suppiled R.dll to construct a DEF file by
hand. However, although I have previously 
sucessfully created DLL functions for SPlus I have been unable to do this
using the supplied R code and binaries. I get the Link time error saying
that the R functions can not be found.


error LNK2001: unresolved external symbol "void __stdcall Rf_unprotect(int)"
(?Rf_unprotect@@YGXH at Z)
error LNK2001: unresolved external symbol _REAL at 4
error LNK2001: unresolved external symbol _Rf_protect at 4
error LNK2001: unresolved external symbol _Rf_allocMatrix at 12
error LNK2001: unresolved external symbol _Rf_length at 4

I suspect this is because of way in which the functions are exported is not
standardised between MS and GNU compilers.

I suppose that I can recompile all the R code in VC++ and then I would be
able to use my simple DLL but this would be a 
major exercise and then I couldn't make my own routines easily available
within R.

Has anybody encountered this problem and know of a solution.

Thanks in advance

Laurence Kell

CEFAS Lowestoft Laboratory
Pakefield Road
Lowestoft, NR33 0HT
Tel 	+44 1502 52 42 57
Fax 	+44 1502 52 45 11
e-mail	l.t.kell at cefas.co.uk

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list