Alex Chen
2007-Jun-01 19:18 UTC
[R] Calling C routine in anther package in C code (R_RegisterCCallable)
Hi,
I want to make use of some C routines from other packages to write extensions
in C.
In "Writing R Extensions", it says there is an experimental interface
to
support this in (or from ?) R 2.4.0.
I had a dummy library containing src/dummy.cpp and R/zzz.R:
====== src/dummy.cpp ===
#include <R.h>
#include <Rinternals.h>
#include <R_ext/Rdynload.h>
extern "C"
{
void dummy(int *a, int *b)
{
printf("dummy\n");
}
}
void R_init_dummy(DllInfo *dll)
{
R_RegisterCCallable("dummy", "dummy", (DL_FUNC)dummy);
}
====== R/zzz.R ========
.First.lib <- function(lib, pkg) {
library.dynam("dummy", pkg, lib)
}
=====================
I can compile this using R 2.5.0 under Linux.
But I got the following error when I tried to load the library
>library(dummy):
Error in dyn.load(x, as.logical(local), as.logical(now)) :
unable to load shared library
'/homes/ychen/Rlibs/dummy/libs/dummy.so':
/homes/ychen/Rlibs/dummy/libs/dummy.so: undefined symbol:
_Z19R_RegisterCCallablePcS_PFPvvE
Error in library(dummy) : .First.lib failed for 'dummy'
Can anyone help me out?
Thanks!
Alex
[[alternative HTML version deleted]]
Douglas Bates
2007-Jun-01 20:07 UTC
[R] Calling C routine in anther package in C code (R_RegisterCCallable)
It looks like your C++ compiler is mangling the name R_RegisterCCallable. Move the extern "C" before the #include <R_ext/Rdynload.h> On 6/1/07, Alex Chen <ychen at insightful.com> wrote:> Hi, > > > > I want to make use of some C routines from other packages to write extensions > in C. > > > > In "Writing R Extensions", it says there is an experimental interface to > support this in (or from ?) R 2.4.0. > > > > I had a dummy library containing src/dummy.cpp and R/zzz.R: > > > > ====== src/dummy.cpp ===> > #include <R.h> > > #include <Rinternals.h> > > #include <R_ext/Rdynload.h> > > > > extern "C" > > { > > void dummy(int *a, int *b) > > { > > printf("dummy\n"); > > } > > } > > > > void R_init_dummy(DllInfo *dll) > > { > > R_RegisterCCallable("dummy", "dummy", (DL_FUNC)dummy); > > } > > > > ====== R/zzz.R ========> > > > .First.lib <- function(lib, pkg) { > > library.dynam("dummy", pkg, lib) > > } > > > > =====================> > > > I can compile this using R 2.5.0 under Linux. > > > > But I got the following error when I tried to load the library > > > > >library(dummy): > > Error in dyn.load(x, as.logical(local), as.logical(now)) : > > unable to load shared library > '/homes/ychen/Rlibs/dummy/libs/dummy.so': > > /homes/ychen/Rlibs/dummy/libs/dummy.so: undefined symbol: > _Z19R_RegisterCCallablePcS_PFPvvE > > Error in library(dummy) : .First.lib failed for 'dummy' > > > > Can anyone help me out? > > > > Thanks! > > > > Alex > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >