Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Why can't I call a C subroutine using Inline-C module?

by ZJ.Mike.2009 (Scribe)
on Mar 05, 2010 at 06:55 UTC ( [id://826887]=perlquestion: print w/replies, xml ) Need Help??

ZJ.Mike.2009 has asked for the wisdom of the Perl Monks concerning the following question:

Run the following minimalized code:
use Inline 'C'; main(); __END__ __C__ int main(int argc, char *argv[]) { return 0; }
and I will receive this: Can't locate auto/main/main.al in @INC

What's going on here? I'm running ActivePerl 5.10.0 and my Inline module version is 0.46. Thanks :)

Replies are listed 'Best First'.
Re: Why can't I call a C subroutine using Inline-C module?
by syphilis (Archbishop) on Mar 05, 2010 at 07:44 UTC
    I don't think Inline::C can handle the char * argv[] argument. This works fine:
    use Inline 'C'; main(123); __END__ __C__ int main(int argc) { return 0; }
    And so does:
    use Inline 'C'; main(123, 'string'); __END__ __C__ int main(int argc, char *argv) { return 0; }
    Note that I'm providing arguments when I call main() as per the definition of main() - otherwise an (expected) error occurs complaining about my usage.

    Cheers,
    Rob


    UPDATE: For a good intro to Inline::C capabilities, see perldoc Inline::C-Cookbook
      This works fine: ...

      Interesting.  It doesn't work here (on Linux, though), independently of whether I declare/pass arguments to main or not:

      #!/usr/bin/perl use Inline 'C'; main(); __END__ __C__ int main() { printf("foo\n"); return 0; }
      $ ./826910.pl Segmentation fault

      My guess would be that this is because main is special (the entry point of an executable).  If you rename the XS routine to something else like main2, however, it prints "foo" as expected.

        My guess would be that this is because main is special (the entry point of an executable).

        Yes - I was a little surprised that the 'main' name didn't cause problems on Windows. Of course, I haven't tested it out extensively but there's certainly no problem for me (on Win32) with the code you just posted.

        I've only just now given it a run on linux. Won't build for me - I get all sorts of bizarre errors that don't even warrant thinking about. (Naming the function "main2" works fine.)

        Cheers,
        Rob
Re: Why can't I call a C subroutine using Inline-C module?
by Anonymous Monk on Mar 05, 2010 at 07:54 UTC
    Examine the generated module/XS and generated C code and you can see exactly what is going on.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://826887]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-19 12:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found