[R] Re: help in R calling C function (Lei Liu)
ligges at statistik.uni-dortmund.de
Sun Jun 27 15:50:48 CEST 2004
Zhu Wang wrote:
>>Date: Sun, 27 Jun 2004 03:18:43 -0400
>>From: Lei Liu <liulei at l.imap.itd.umich.edu>
>>Subject: [R] help in R calling C function
>>To: r-help at stat.math.ethz.ch
>>Message-ID: <126.96.36.199.1.20040627030746.014936b8 at l.imap.itd.umich.edu>
>>Content-Type: text/plain; charset="us-ascii"; format=flowed
>>I want to call a C function in R. I have some experience on it, but this
>>time I need to call another C function in the "main" C function. As a
>>simple example, I use the following C code:
>>void main(double *alpha)
>> double test();
>> double beta;
>> beta= *alpha *2 + test(*alpha);
>>double test(double *alpha)
>> double value;
>> value = *alpha *3;
>>I used "gcc -c main.c" and "Rcmd SHLIB main.o" and I got the main.dll file.
>>In R I use the following code to call the C function "main":
> I do not have too much experience using C,
If you don't know, why do you post an answer without checking the
manuals and trying out what you are suggesting? Your answer won't help
and is at least partly incorrect.
> but do you need to compile
> your test.c as well, something like
> gcc -c main.c test.c
Well, if the code is within 2 files, you want to specify
R CMD SHLIB main.c test.c
(instead of 'R CMD SHLIB main')
and main.dll will be made (and linked correctly!). This way you do not
need to bother with any flags for gcc.
> I also suggest you to double check the path is correct.
> Here you probably mean
a=4.2 *is* correct syntax as described in the green book as well as in
the manuals (just a matter of taste whether you really want to use it).
Please check out my former answer pointing out what really went wrong in
the C code ...
> a <- 4.2
> b <- .C('main',as.double(a))
>>But the R system crashed. I know I can only define the function type in C
>>as "void" to be called by R. But what if I want to call another C function
>>in the "main" C function? Thank you for your help!
More information about the R-help