Code Comments
Programming Forum and web based access to our favorite programming groups.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.
Post Follow-up to this message
----- 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
Post Follow-up to this message
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
Post Follow-up to this messageHi,
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
Post Follow-up to this message
----- 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
Post Follow-up to this messageRob,
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.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.