Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Inline::C - including an external file and nothing else
Hello.

I'm able to create Perl script that uses Inline C and also links to a precom
piled C library, but I'm trying to do something else that I think should be 
equivalent:

#!/usr/bin/perl -w
use Inline C => <<ENDC;
#include <myCcode.c>
ENDC
test(2);

I.e., if I paste the contents of myCcode.c in place of the #include, I get t
he expected results. I'm trying to get Inline to do that and ignore the fact
 that the code comes from a separate file, but it refuses to recognize the f
unctions in myCcode.c. (Bas
ically, I prefer to avoid compiling the code myself.)

Thanks in advance for any tips, and please forgive the novice. :O)

AndyD


---------------------------------
Looking for last minute shopping deals?  Find them fast with Yahoo! Search.

Report this thread to moderator Post Follow-up to this message
Old Post
second axiom
03-30-08 10:46 AM


Re: Inline::C - including an external file and nothing else
----- Original Message -----
From: "second axiom" <secondaxiom@yahoo.com>
To: <inline@perl.org>
Sent: Sunday, March 30, 2008 3:56 PM
Subject: Inline::C - including an external file and nothing else


> Hello.
>
> I'm able to create Perl script that uses Inline C and also links to a
> precompiled C library, but I'm trying to do something else that I think
> should be equivalent:
>
> #!/usr/bin/perl -w
> use Inline C => <<ENDC;
> #include <myCcode.c>
> ENDC
> test(2);
>
> I.e., if I paste the contents of myCcode.c in place of the #include, I get
> the expected results. I'm trying to get Inline to do that and ignore the
> fact that the code comes from a separate file, but it refuses to recognize
> the functions in myCcode.c. (Basically, I prefer to avoid compiling the
> code myself.)
>

I created a file called 'src/myCcode.c' which contained:

void greet() {
printf("Hello from myCcode.c\n");
}

I then ran the following Inline::C script:

use warnings;
use Inline C => 'src/myCcode.c';
greet();

which, behaved as desired and printed out (after compiling):

Hello from myCcode.c

However, for some reason, it seems that myCcode.c needs to be in a
subdirectory. If it's placed in the same directory as the Inline::C script,
it doesn't work. This is mentioned in 'perldoc Inline-FAQ'.

Cheers,
Rob


Report this thread to moderator Post Follow-up to this message
Old Post
Sisyphus
03-30-08 10:46 AM


Re: Inline::C - including an external file and nothing else

Sisyphus <sisyphus1@optusnet.com.au> wrote:
----- Original Message -----
From: "second axiom"
To:
Sent: Sunday, March 30, 2008 3:56 PM
Subject: Inline::C - including an external file and nothing else


> Hello.
>
> I'm able to create Perl script that uses Inline C and also links to a
> precompiled C library, but I'm trying to do something else that I think
> should be equivalent:
>
> #!/usr/bin/perl -w
> use Inline C => <
> #include
> ENDC
> test(2);
>
> I.e., if I paste the contents of myCcode.c in place of the #include, I get
> the expected results. I'm trying to get Inline to do that and ignore the
> fact that the code comes from a separate file, but it refuses to recognize
> the functions in myCcode.c. (Basically, I prefer to avoid compiling the
> code myself.)
>

I created a file called 'src/myCcode.c' which contained:

void greet() {
printf("Hello from myCcode.c\n");
}

I then ran the following Inline::C script:

use warnings;
use Inline C => 'src/myCcode.c';
greet();

which, behaved as desired and printed out (after compiling):

Hello from myCcode.c

However, for some reason, it seems that myCcode.c needs to be in a
subdirectory. If it's placed in the same directory as the Inline::C script,
it doesn't work. This is mentioned in 'perldoc Inline-FAQ'.

Cheers,
Rob


Hi, Rob.

Thanks for the reply; let me know if I'm breaking any rules of replies.

I had indeed tried your suggestion (use Inline C => 'src/myCcode.c';) and go
tten it to work, but my Inline::C script has additional C code that #include
's myCcode.c. I'm able to call the functions in myCcode.c and access its glo
bal vars from Perl and fro
m Inline::C, but the two are operating on separate instantiations of those v
ariables.

What I'm trying to accomplish seems to be a combination or modification of c
urrent options, a  concatenation of a source file and the Inline::C code I h
ave in the script:

use Inline (C => 'C/myCcode.c', <<ENDC);
[new C code here]
ENDC

OR a concatenation of two source files:

use Inline (C => 'C/myCcode.c', 'C/moreC.c');

(Or both, for that matter.) This would mean that any calls from Perl or from
 the Inline::C code would be operating on the same instantiation of the glob
al variables in C/myCcode.c. This alternative fails, but for another reason:

use Inline C => <<ENDC, AUTO_INCLUDE => '#include "C/myCcode.c"';

OR:

use Inline C => <<ENDC
#include "C/myCcode.c"

In both cases, Inline::C can access the functions and vars in myCcode.c, but
 Perl cannot.

It doesn't matter to me if this is accomplished through eval, symbolic links
, whatever. Based on your reply to a previous question, it sounds like I nee
d to modify C.pm.

Thanks again.

AndyD


---------------------------------
OMG, Sweet deal for Yahoo! users/friends: Get A Month of Blockbuster Total A
ccess, No Cost. W00t

Report this thread to moderator Post Follow-up to this message
Old Post
second axiom
03-31-08 05:13 AM


Re: Inline::C - including an external file and nothing else
Hi,

Actually, I think your code doesn't need to be in a subdirectory. It's
just that IIRC Inline looks for a slash ('/') to know if the specified
scalar is a filename or the source code itself. If you use
'./myCcode.c' it should work.

But in your case, you should be able to just read in the 2 files
yourself and send the code directly to Inline::C like this:

#!/usr/bin/perl
use strict ;

sub get_code {
my $code =3D '' ;
for my $src (@_){
$code .=3D join('', <SRC> ) if open(SRC, $src) ;
}

return $code ;
}

use Inline(
C =3D> get_code(qw(C/myCcode.c C/moreC.c),
) ;

or if your are lazier:

use Inline(
C =3D> scalar `cat C/myCcode.c C/moreC.c`
) ;


Patrick



On Sun, Mar 30, 2008 at 1:41 PM, second axiom <secondaxiom@yahoo.com> wrote=
:
>
>
>  Sisyphus <sisyphus1@optusnet.com.au> wrote:
>  ----- Original Message -----
>  From: "second axiom"
>
> To:
>  Sent: Sunday, March 30, 2008 3:56 PM
>  Subject: Inline::C - including an external file and nothing else
>
> 
k 
> 
get 
he 
nize 
e 
>
>  I created a file called 'src/myCcode.c' which contained:
>
>  void greet() {
>      printf("Hello from myCcode.c\n");
>  }
>
>  I then ran the following Inline::C script:
>
>  use warnings;
>  use Inline C =3D> 'src/myCcode.c';
>  greet();
>
>  which, behaved as desired and printed out (after compiling):
>
>  Hello from myCcode.c
>
>  However, for some reason, it seems that myCcode.c needs to be in a
>  subdirectory. If it's placed in the same directory as the Inline::C scri=
pt,
>  it doesn't work. This is mentioned in 'perldoc Inline-FAQ'.
>
>  Cheers,
>  Rob
>
>
>  Hi, Rob.
>
>  Thanks for the reply; let me know if I'm breaking any rules of replies.
>
>   I had indeed tried your suggestion (use Inline C =3D> 'src/myCcode.c';)=
and gotten it to work, but my Inline::C script has additional C code that =
#include's myCcode.c. I'm able to call the functions in myCcode.c and acces=
s its global vars from Perl and from Inline::C, but the two are operating o=
n separate instantiations of those variables.
>
>  What I'm trying to accomplish seems to be a combination or modification =
of current options, a  concatenation of a source file and the Inline::C cod=
e I have in the script:
>
>   use Inline (C =3D> 'C/myCcode.c', <<ENDC);
>   [new C code here]
>  ENDC
>
>  OR a concatenation of two source files:
>
>  use Inline (C =3D> 'C/myCcode.c', 'C/moreC.c');
>
>  (Or both, for that matter.) This would mean that any calls from Perl or =
from the Inline::C code would be operating on the same instantiation of the=
global variables in C/myCcode.c. This alternative fails, but for another r=
eason:
>
>  use Inline C =3D> <<ENDC, AUTO_INCLUDE =3D> '#include "C/myCcode.c"';
>
>  OR:
>
>  use Inline C =3D> <<ENDC
>  #include "C/myCcode.c"
>
>   In both cases, Inline::C can access the functions and vars in myCcode.c=
, but Perl cannot.
>
>  It doesn't matter to me if this is accomplished through eval, symbolic l=
inks, whatever. Based on your reply to a previous question, it sounds like =
I need to modify C.pm.
>
>   Thanks again.
>
>  AndyD
>
>
>
>  ---------------------------------
>  OMG, Sweet deal for Yahoo! users/friends: Get A Month of Blockbuster Tot=
al Access, No Cost. W00t



--=20
 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D
Patrick LeBoutillier
Laval, Qu=E9bec, Canada

Report this thread to moderator Post Follow-up to this message
Old Post
Patrick LeBoutillier
03-31-08 05:14 AM


Re: Inline::C - including an external file and nothing else
----- Original Message -----
From: "second axiom" <secondaxiom@yahoo.com>
.
.
> What I'm trying to accomplish seems to be a combination or modification of
> current options, a  concatenation of a source file and the Inline::C code
> I have in the script:
>
> use Inline (C => 'C/myCcode.c', <<ENDC);
> [new C code here]
> ENDC
>

I ran the following script:

----------------------------
use warnings;
use Inline C => 'src/myCcode.c';
use Inline C => <<'EOC';
void again() {
printf("Hello again\n");
}

EOC

greet();
again();
----------------------------

It outputs:

Hello from myCcode.c
Hello again

> OR a concatenation of two source files:
>
> use Inline (C => 'C/myCcode.c', 'C/moreC.c');

I found I could achieve that with:

------------------------------------
use warnings;
use Inline C => 'src/myCcode.c';
use Inline C => 'src/moreC.c';

greet();
again();
-----------------------------------

where src/moreC.c contains the again() function. Again, that outputs:

Hello from myCcode.c
Hello again

Does that help ?

(I've yet to check whether Patrick's suggestion works - but I expect it
does.)

Cheers,
Rob





Report this thread to moderator Post Follow-up to this message
Old Post
Sisyphus
03-31-08 05:15 AM


Re: Inline::C - including an external file and nothing else
Rob,

I tested the solutions you suggested and got the expected output, but the tw
o C functions are unable to call each other. If I put them both in a single 
file, then (predictably) they see each other.

Patrick's suggestion (scalar of cat of multiple files) did the trick. Thanks
 to you both.

Andy

----------------------------
use Inline C => 'src/myCcode.c';
use Inline C => <<'EOC';
void again() {
printf("Hello again\n");
}
EOC
greet();
again();
----------------------------
use Inline C => 'src/myCcode.c';
use Inline C => 'src/moreC.c';
greet();
again();




---------------------------------
Like movies? Here's a limited-time offer: Blockbuster Total Access for one m
onth at no cost.

Report this thread to moderator Post Follow-up to this message
Old Post
second axiom
03-31-08 05:16 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL INLINE module archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 10:12 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.