tags:

views:

101

answers:

2

I have a piece of code like this (Perl file):

print "9 + 16 = ", add(9, 16), "\n";
print "9 - 16 = ", subtract(9, 16), "\n";

C code also,

#include<stdio.h>

main ()
{
 int x = 9;
 int y = 16;
 printf(" add() is %d\n", add(x,y));
 printf(" sub() is %d\n", subtract(x,y));
//  return 0; 
}
int add(int x, int y) 
{
 return x + y;
}

int subtract(int x, int y) 
{
 return x - y;
} 

How can I run this C code with perl using Inline::C? I tried but i am not exactly getting.

+4  A: 

try:

use Inline 'C';

print "9 + 16 = ", add(9, 16), "\n";
print "9 - 16 = ", subtract(9, 16), "\n";

__END__
__C__

int add(int x, int y) {
 return x + y;
}

int subtract(int x, int y) {
 return x - y;
} 
codaddict
+8  A: 
  • Have a look at Inline::C-Cookbook - A Cornucopia of Inline C Recipes, you will get lot of examples using Inline with C.
  • See Inline - Write Perl subroutines in other programming languages, you will come to know how to use Inline and you will get C examples too.
Nikhil Jain