[R] question about dyn.load()
xt_wang@cs.concordia.ca
xt_wang at cs.concordia.ca
Thu May 13 02:48:26 CEST 2004
Hello, everybody,
I met a big problem. What I want is to use c code in R. I have load a test
code "conv.c" in R and run it successfully. However, in my real code, I use
matlab c library (Matrix Inverse function). After I use command "R CMD SHLIB"
to share library, I can not use "dyn.load" to load the object in
R. "undefined symbol: mclCleanupProtectedItems" always be mentioned.
My question is that whether I can compile, load and use a c code in R which
includes other library. If I can, How can I do? Could you help me find out
where is the error during the whole operation? If I can not load a c code with
other library in R, can I load a c code with R library in R ( which include
matrix inverse function)? If so, where can I find the R library with matrix
inverse function that has a c interface?
I will appreciate very much if somebody can help me resolve this problem.
Thanks in advance!
Yours,
Maggie
The whole successful and failed process are as follows:
[credsim at confsys ~/src]$ R CMD SHLIB ~/src/conv.c
make: `/home/credsim/src/conv.so' is up to date.
[credsim at confsys ~/src]$ R
R : Copyright 2003, The R Foundation for Statistical Computing
Version 1.8.1 (2003-11-21), ISBN 3-900051-00-3
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for a HTML browser interface to help.
Type 'q()' to quit R.
> dyn.load("./conv.so")
> "%+%" <- function(a,b)
+ .C("conv", as.double(a), as.integer(length(a)),
+ as.double(b), as.integer(length(b)),
+ ab=double(length(a)+length(b)-1))$ab
> u <-rep(1,5)
> u %+% u
[1] 1 2 3 4 5 4 3 2 1
"conv.c"
void conv (double *a, int *na, double *b, int *nb, double *ab)
{
int i, j, nab=*na+*nb-1;
for (i=0;i<nab;i++)
ab[i]=0.0;
for (i=0;i<*na;i++)
for (j=0;j<*nb;j++)
ab[i+j]+=a[i]*b[j];
}
The new operation process with matlab c library :
[credsim at confsys ~/src]$ R CMD SHLIB ~/src/conv.c
gcc -I/usr/local/lib/R/include -I/usr/local/include -D__NO_MATH_INLINES -
mieee-fp -fPIC -g -O2 -c /home/credsim/src/conv.c -o /home/credsim/src/conv.o
gcc -shared -L/usr/local/lib -
o /home/credsim/src/conv.so /home/credsim/src/conv.o
[credsim at confsys ~/src]$ R
R : Copyright 2003, The R Foundation for Statistical Computing
Version 1.8.1 (2003-11-21), ISBN 3-900051-00-3
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for a HTML browser interface to help.
Type 'q()' to quit R.
[Previously saved workspace restored]
> dyn.load("./conv.so")
Error in dyn.load(x, as.logical(local), as.logical(now)) :
unable to load shared library "/b2/home/credsim/src/./conv.so":
/b2/home/credsim/src/./conv.so: undefined symbol: mclCleanupProtectedItems
>
new code "conv.c"
#include "/a3/matlab/extern/include/matlab.h"
#include <math.h>
void conv (double *a, int *na, double *b, int *nb, double *ab)
{
int i, j, nab=*na+*nb-1;
for (i=0;i<nab;i++)
ab[i]=0.0;
for (i=0;i<*na;i++)
for (j=0;j<*nb;j++)
ab[i+j]+=a[i]*b[j];
}
More information about the R-help
mailing list