[R] building a Win32 DLL with R objects?

Jeff D. Hamann jeff_hamann at hamanndonald.com
Tue May 20 23:27:11 CEST 2003


I've been attemping to create a test dll that contains R objects (actually I
simply copied the code from the "Writing R extensions") and got it to build
the dll using rcmd shlib main.c (okay, it's simple, but effective). Here's
the info so far:

this is the contents of the main.c file (not there's no WinMain()). Do we
put it in or does the script do it when we compile?

/* this is a test for the r package to access r objects from a c lib */

#include "R.h"
#include "Rdefines.h"

SEXP test_function( SEXP a, SEXP b )
{

   int     na;
   int     nb;
   int     nab;
   double  *xa;
   double  *xb;
   double  *xab;

   SEXP ab;

   PROTECT( a = AS_NUMERIC( a ) );
   PROTECT( b = AS_NUMERIC( b ) );

   warning( "you're now in test_function\n" );

   na = LENGTH( a );
   nb = LENGTH( b );
   nab = na + nb - 1;

   PROTECT( nab = AS_NUMERIC( nab ) );

   xa = NUMERIC_POINTER( a );
   xb = NUMERIC_POINTER( b );
   xab = NUMERIC_POINTER( ab );


   UNPROTECT( 3 );

   return ab;

}

I built the dll using the "rcmd shlib main.c" and a dll pops out in the end.
Yeah for me! Now, I tried to call my function using:

## this is a simple r program to demonstrate how to
## call openfvs functions....
dyn.load("c:/rdll_test/main.dll" )
x <- c( 1, 2.3568, 3.14159 )
y <- c( -568.89234, 4, 3234.23424 )
conv <- function( a, b ) .Call( "test_function", a, b )

and I'm not sure the DLL actually getting loaded? I've been able to step
through other DLL projects I call from R, but none that use the R libraries.
When I run the script from the terminal (using rterm.exe), R just sits there
as if stuck in a loop and nothing happens. I would like to be able to step
through this using the visual studio debugger but haven't been able to
figure out how to build R DLLs using something other than the RCMD SHLIB.

WHen I try to compile a project, similar to my other projects I've been
calling from R, I get the following linker errors:

Linking...
main.obj : error LNK2001: unresolved external symbol _Rf_unprotect
main.obj : error LNK2001: unresolved external symbol _REAL
main.obj : error LNK2001: unresolved external symbol _LENGTH
main.obj : error LNK2001: unresolved external symbol _Rf_warning
main.obj : error LNK2001: unresolved external symbol _Rf_protect
main.obj : error LNK2001: unresolved external symbol _Rf_coerceVector
Debug/rdll_test.dll : fatal error LNK1120: 6 unresolved externals
Error executing link.exe.

rdll_test.dll - 7 error(s), 6 warning(s)

And I can't find the lib file I should be including. Is there one or am I
barking up the wrong tree?

Jeff.




More information about the R-help mailing list