[Rd] multiple packages using the same native code.

Seth Falcon sfalcon at fhcrc.org
Thu Mar 16 02:06:21 CET 2006


Hi Jim,

James Bullard <bullard at berkeley.edu> writes:
> I would like to construct two packages (A, B) which utilize a number of 
> common C functions. The most straightforward way to do this is just copy 
> the relevant .c and .h files from one src directory to the next, but 
> this is tedious especially in the face of multiple developers and
> changes.

I'm not sure I understand what you are after.  One possible solution
would be to create a third package 'C' that contains the common C
code.  This would allow you to call C function defined in 'C' from the
C code in 'A' or 'B'.

Using a .onLoad hook and getNativeSymbolInfo(), you can pass C
function pointers to the code in packages A and B.

Suppose in 'C' you have a C function foo() that is registered in the
usual manner so that it can be called by .Call or .C.

Then in 'A' you could have (all untested, sorry, but hopefully it
sketches the idea for you):

A/src/A.c

   static DL_FUNC C_foo;

   void init_funcs_from_C(SEXP foo_info) {
       C_foo = R_ExternalPtrAddr(foo_info);
   }

   void bar(int *x) {
       ...
       z = C_foo();
       ...
   }


A/R/zzz.R

   .onLoad <- function(libname, pkgname) {
       foo_info <- getNativeSymbolInfo("foo", PACKAGE="C")
       .Call("init_funcs_from_C", foo_info$address)
   }


+ seth



More information about the R-devel mailing list