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

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

Replies are listed 'Best First'.
Re^2: Why can't I call a C subroutine using Inline-C module?
by almut (Canon) on Mar 05, 2010 at 08:17 UTC
    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

        The reason it works on Windows, is because of the old exports thing that prevents Windows seeing unauthorised Perl APIs.

        Windows doesn't export the I::C subroutines from the DLL, they're all looked up through a single entrypoint (boot_projectName).

        They are look up in the same way under linux, but they are also exported from the .so. And when main is exported, it is special at the linker stage.

        That's probably not the whole story, but I think it is the crux of it.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.