Consider the following simple C program:

/*** hello_r.c ***/
#include <Rinternals.h>

SEXP hello() {
 return mkString("Hello, world!\n");
}

int main(void) {
 SEXP x = hello();
 return x == NULL;             /* i.e. 0 on success */
}

This program segfaults:

% myR/bin/R CMD LINK gcc -I./R-2.9.0/src/include -L./myR/lib64/R/lib -lR
hello_r.c -o hello_r > /dev/null
% hello_r
zsh: segmentation fault  hello_r


But if instead of compiling the program as a standalone I make it as a
shared library, like this:

% myR/bin/R CMD SHLIB --preclean hello_r.c > /dev/null

... then the hello() function works fine, when run from within an R session:

> dyn.load(paste("hello_r", .Platform$dynlib.ext, sep = ""))
> .Call("hello")
[1] "Hello, world!\n"


The fact that the same function succeeds when run from within an R session
but segfaults when run from the standalone executable tells me that, in the
standalone case, the main() function needs to do some initialization stuff
on R before one can invoke functions like mkString(), but I have not been
able to find what this initialization should be.
If someone could point me in the right direction I'd appreciate it.

Also, where is the documentation for the call_R function?  It is mentioned
several times in the Writing R Extensions document, but I have not been able
to find the documentation for it.

TIA!

Kynn

        [[alternative HTML version deleted]]

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to