Hi R Users,
Thanks in advance.
I am using R-2.5.1 on Windows XP.
I am trying to call C code (testCX1.C) from R. testCX1.c calls another C code
(funcC1.c) and returning a value to testCX1.c. I like to have this value in R.
My steps are below:
1. R CMD SHLIB testCX1.c funcC1.c (at command propmt)
2. It creates testCX1.dll with warning (but testCX1.dll works):
testCX1.c:38: warning: implicit declaration of function 'func'
How to get rid off this error ?
What is the best way to call funcC1.c from testCX1.c?
I have provided the codes below:
Once again thank you very much for the time you have given.
Regards,
Debabrata Midya (Deb)
Statistician
NSW Department of Commerce
Sydney, Australia
testCX1.C
--------------
/*
*********************
testCX1.c
*********************
*/
#include <R.h>
#include <Rdefines.h>
#include <Rmath.h>
SEXP testC1(SEXP a)
{
int i, nr;
double *xa, *xw, tmp;
SEXP w;
PROTECT(a = coerceVector(a, REALSXP));
nr = length(a);
printf( "Length : %d \n", nr);
PROTECT(w = allocVector(REALSXP, 1));
xa = REAL(a);
xw = REAL(w);
tmp = 0.0;
for (i = 0; i < nr; i++)
{
tmp += xa[i];
}
// tmp = 0.0;
xw[0] = func(xa);
UNPROTECT(2);
return(w);
}
funcC1.c
------------
/*
*********************
funcC1.c
*********************
*/
#include <R.h>
#include <Rdefines.h>
#include <Rmath.h>
SEXP func(SEXP a)
{
int i, nr = 3;
double *xa, *xw, tmp;
SEXP w;
PROTECT(a = coerceVector(a, REALSXP));
PROTECT(w = allocVector(REALSXP, 1));
xa = REAL(a);
xw = REAL(w);
tmp = 0.0;
for (i = 0; i < nr; i++)
{
tmp += xa[i] * xa[i];
}
xw[0] = tmp;
UNPROTECT(2);
return(w);
}
R script
-----------
dyn.load("testCX1.dll")
xL <- 1:5
xL <- as.double(as.vector(t(xL)))
.Call("testC1",xL)
[1] 55
---------------------------------
[[alternative HTML version deleted]]