[Rd] multiple packages using the same native code.

James Bullard bullard at berkeley.edu
Fri Mar 17 03:53:21 CET 2006


Seth, thanks for the advice. This solution seems like it might work, but 
then all errors occur at runtime rather than at compile time. This seems 
like I am exchanging one evil for another (run time segfaults versus 
code duplication) Lets say we have these three package A, B, and C 
defined more or less like this:

A/src/bar.c
int bar()
{
    foo();
}

B/src/baz.c
int baz()
{
    foo();
}

C/src/foo.c
int foo()
{
    return 1;
}


Now, the only way I can see to do this is to copy foo.c into both src 
directories of package A and B. This is not exactly what anyone wants, 
but rather I'd rather just say that both package A and B depend on 
package C. If I put them in a bundle then can I expect that the src will 
always simultaneously be available? In this way I can easily modify the 
configure script to handle this, but if I have no way to depend on the 
presence of the code (ie. users could download and install packages 
separately even if it's a bundle) then it seems like there is no way to 
generally modify the configure file to do this.


thanks, jim





Seth Falcon wrote:

>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
>
>______________________________________________
>R-devel at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-devel
>
>  
>



More information about the R-devel mailing list