[Rd] Qs on calling R from C

From: Kynn Jones <kynnjo_at_gmail.com>
Date: Sat, 06 Jun 2009 17:03:07 -0400


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_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Sat 06 Jun 2009 - 21:12:57 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 06 Jun 2009 - 22:36:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive