[R] Using C code in R

Steve Lianoglou mailinglist.honeypot at gmail.com
Fri Mar 25 01:34:10 CET 2011


Hi Jaimin,

Please include r-help in your reply -- it's not included by default,
so you'll have to use your email client's "reply all" functionality
when responding to posts coming from the mailing list.

This way (i) more people will be able to help you than just me; and
(ii) if someone runs into the same/similar problem you are having down
the road, a web search might land them on this thread -- anyway:

On Thu, Mar 24, 2011 at 5:55 PM, Jaimin Dave <davejaiminm at gmail.com> wrote:
> Thanks for help...
> I  am sorry i just forgot to declare global variable in previous function
> .But after I declare and compile it is showing following error:
> HelloWorld.c: In function 'sayhello':
> HelloWorld.c:9:2: warning: implicit declaration of function 'Rprintf'
> x86_64-w64-mingw32-gcc -shared -s -static-libgcc -o HelloWorld.dll tmp.def
> Hello
> World.o -LC:/PROGRA~1/R/R-212~1.1/bin/x64 -lR
> HelloWorld.o:HelloWorld.c:(.text+0x44): undefined reference to `sayHello'
> collect2: ld returned 1 exit status
>
> It is saying that undefined reference to function sayHello
> Could you help me on this?

Look at your code again. In your initial declaration of 'sayHello',
you spell it like this:

void sayHello();

Then your implementation of it is:

void sayhello() { ... }

(note the case of the letters)

And inside your `g` function you are calling `sayHello` (which still
hasn't been defined).

Also, I'm pretty sure you shouldn't be getting this warning:

HelloWorld.c:9:2: warning: implicit declaration of function 'Rprintf'

Is your include statement really preceded by `//`? like:

//#include <R.h>

?

-steve


> On Wed, Mar 23, 2011 at 11:58 PM, Steve Lianoglou
> <mailinglist.honeypot at gmail.com> wrote:
>>
>> Hi,
>>
>> On Wed, Mar 23, 2011 at 8:12 PM, Jaimin Dave <davejaiminm at gmail.com>
>> wrote:
>> > Hi,
>> > I am new to R and I want to know how to use C code which contains two
>> > functions one called inside another.I know that how to use C code in R
>> > if it
>> > has only one function but dont know how to do it in above case.
>>
>> There's no special case to consider here.
>>
>> You would call the "outer" C function from R, and let your C function
>> call whatever other C functions it wants, ie:
>>
>> > use the same in R .My C code is as follows.
>> >
>> > //#include <R.h>
>> >
>> > void sayHello();
>> >
>> > void g();
>> >
>> >
>> >
>> > void sayhello() {
>> >
>> > Rprintf("Hello world %d\n",global);
>> >
>> > }
>> >
>> > void g()
>> >
>> > {
>> >
>> > int a=9;
>> >
>> > int b=1;
>> >
>> > Rprintf("Hello world\n %d",(a+b));
>> >
>> > sayHello();
>> >
>> > }
>> >
>> > Help would be greatly appreciated
>>
>> Assuming you set up your R <--> C stuff correctly, you'd just have R
>> call your `g` function.
>>
>> I don't see where you have the `global` variable defined (it's used in
>> your `sayHello` function) so this will fail for other reasons, but
>> assuming all of your C stuff is works, then just having R call `g`
>> correctly will work for you.
>>
>> -steve
>>
>> --
>> Steve Lianoglou
>> Graduate Student: Computational Systems Biology
>>  | Memorial Sloan-Kettering Cancer Center
>>  | Weill Medical College of Cornell University
>> Contact Info: http://cbio.mskcc.org/~lianos/contact
>
>



-- 
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact



More information about the R-help mailing list